cache

Caching in nyxx is managed automatically with internal wrappers around existing Dart features. Nyxx features a Cache<T, S> interface which is the base class for all caches used in the library. Like any other aspect of nyxx, cache handling can be altered to fit your needs.


You can pass an instance of CacheOptions when creating your Nyxx instance to alter caching behavior across the whole bot:

/// Options for configuring cache. Allows to specify where and which entities should be cached and preserved in cache
class CacheOptions {
  /// Defines in which locations members will be cached
  CachePolicyLocation memberCachePolicyLocation = CachePolicyLocation();

  /// Defines which members are preserved in cache
  CachePolicy<IMember> memberCachePolicy = MemberCachePolicy.def;

  /// Defines where channel entities are preserved cache. Defaults to [CachePolicyLocation] with additional objectConstructor set to true
  CachePolicyLocation channelCachePolicyLocation = CachePolicyLocation()..objectConstructor = true;

  /// Defines which channel entities are preserved in cache.
  CachePolicy<IChannel> channelCachePolicy = ChannelCachePolicy.def;

  /// Defines in which places user can be cached
  CachePolicyLocation userCachePolicyLocation = CachePolicyLocation();

  /// Defines in which locations members will be cached
  CachePolicyLocation messageCachePolicyLocation = CachePolicyLocation();

  /// Defines which members are preserved in cache
  CachePolicy<IMessage> messageCachePolicy = MessageCachePolicy.def;
}
}

For now there are a few things you can do:

  1. Alter when objects are added to cache (from websocket events or HTTPS requests);
  2. Control which objects are added to cache (by specifying a predicate to choose which objects to cache)

CachePolicyLocation

This setting allows to specify when object are added to the cache:


As an example, this code would only cache Member objects from websocket events and http API requests:

Future<void> main() async {
  final cacheOptions = CacheOptions()
    ..memberCachePolicyLocation = (
        CachePolicyLocation()
          ..event = true
          ..http = true
    );
  
  final bot = NyxxFactory.createNyxxWebsocket("token", 10, cacheOptions: cacheOptions);
}

CachePolicy

Another option to restrict caching is to specify a CachePolicy which is a class that wraps a callback executed when trying to cache objects.

CachePolicy can be freely composed with provided base methods or extended by the end developer:

/// Convenience method to concatenate other policy
CachePolicy<T> or(CachePolicy<T> other);

/// Convenience method to require other policy
CachePolicy<T> and(CachePolicy<T> other);

/// Composes a policy by concatenating multiple other policies from list
static CachePolicy<S> any<S extends SnowflakeEntity>(List<CachePolicy<S>> policies);

For example, this code would only cache Member objects who have roles:

Future<void> main() async {
  final cacheOptions = CacheOptions()
    ..memberCachePolicy = CachePolicy<Member>((member) => member.roles.isNotEmpty);
  
  final bot = NyxxFactory.createNyxxWebsocket("token", 10, cacheOptions: cacheOptions);
}