Caching of WOLF Entities
WOLF server really doesn't like if you request entities from it a lot - it might even temporarily suspend your bot's accounts if you spam it too much.
And to be honest - even if it didn't mind, requesting data from it introduces network overhead.
To avoid these issues, default IWolfClientCache implementation automatically caches following WOLF entities: WolfUser, WolfGroup, WolfCharm and WolfAchievement. It will also handle updating the cache when server sends update events.
Cached entities have lifetime of current connection. That means, as soon as you disconnect, all caches will automatically be cleaned. This happens regardless of the disconnection reason - caches will be purged when you disconnect manually, or when WOLF server disconnects you after an hour.
Accessing caches
Both WolfClient and HostedWolfClient hide access to their caches by default. They can however be accessed in three ways:
Using Sender utility
All extension methods provided by Wolfringo.Utilities, such as GetUserAsync or GetGroupAsync, will automatically retrieve entities from cache by casting the client to @TehGM.Wolfringo.WolfCharm.IWolfClientCacheAccessor (see below). Whenever you request an entity and it's already cached, the cached version will be used, and no request to the server will be made.
Casting to IWolfClientCacheAccessor
IWolfClientCacheAccessor is an interface that both WolfClient and HostedWolfClient implement. This interface provides access to the cached entities.
You can simply cast the client to IWolfClientCacheAccessor and then use any of the interface's methods.
IWolfClientCacheAccessor cacheAccessor = (IWolfClientCacheAccessor)_client;
WolfUser cachedUser = cacheAccessor.GetCachedUser(1234);
Dependency Injection
WolfClientBuilder automatically registers IWolfClientCache in its service provider. If you use Wolfringo.Hosting or register commands using WolfClientBuilder.WithCommands
method, commands will automatically inherit all services. Therefore you will be able to retrieve IWolfClientCache by simply injecting it to your command handlers.
See Dependency Injection for more details.
Disabling cache
Wolfringo's default clients allow you to disable caches individually if you wish to do so.
Warning
While Wolfringo does give an option to disable caching, it is still recommended to keep them enabled to avoid issues mentioned at the beginning of this guide.
Without Wolfringo.Hosting
In normal bot, you can configure caching by calling WolfClientBuilder.WithDefaultCaching
and providing your options.
_client = new WolfClientBuilder()
.WithDefaultCaching(new WolfCacheOptions()
{
options.UsersCachingEnabled = false; // disable users caching
options.GroupsCachingEnabled = false; // disable groups caching
options.CharmsCachingEnabled = false; // disable charms caching
options.AchievementsCachingEnabled = false; // disable achievements caching
})
.Build();
Tip
WolfCacheOptions is defined in TehGM.Wolfringo.Caching
namespace.
With Wolfringo.Hosting
In a bot using Wolfringo.Hosting, you can disable caches using HostedWolfClientOptions. You can do this in 2 ways.
- Configure the client in ConfigureServices:
services.AddWolfClient() // ... other configuration ... .ConfigureCaching(options => { options.UsersCachingEnabled = false; // disable users caching options.GroupsCachingEnabled = false; // disable groups caching options.CharmsCachingEnabled = false; // disable charms caching options.AchievementsCachingEnabled = false; // disable achievements caching });
- Update your appsettings.json:
ConfigureServices:
appsettings.json:services.Configure<WolfCacheOptions>(context.Configuration.GetSection("WolfClient:Caching"));
"WolfClient": { "Caching": { "UsersCachingEnabled": false, "GroupsCachingEnabled": false, "CharmsCachingEnabled": false, "AchievementsCachingEnabled": false } }
Custom Cache
It is possible to customize or completely replace the built-in caching solution. This can be useful if you want to add support for new types or switch to Redis, or anything else.
See Cusomizing Client Cache guide for more details.