Client Cache
As explained in Caching Guide, WolfClient caches WOLF entities in memory to avoid excessive network calls. The default implementation scans all sent and received messages, and alters its internal caches accordingly. Caching Guide shows how to selectively disabling caches for specific entities if needed.
You can check the default implementation on GitHub: WolfClientCache.cs.
Extending Existing Cache
If you wish to extend current cache (like add support for custom Message or Response types), but not replace the default caching logic, you can achieve this by creating a new class that inherits from WolfClientCache - this class declares its methods for reading sent and received messages as virtual
, so they can easily be extended. Additionally it has protected collections for cached entities, and public flags whether they're enabled:
- UsersCache - flag IsUsersCachingEnabled
- GroupsCache - flag IsGroupsCachingEnabled
- CharmsCache - flag IsCharmsCachingEnabled
- AchievementsCache - flag IsAchievementsCachingEnabled
You can use all these features to easily add new caching logic
public class MyCustomWolfClient : WolfClientCache
{
// override this method to handle sent message, and server's response
public override async Task HandleMessageSentAsync(IWolfClient client, IWolfMessage message,
IWolfResponse response, SerializedMessageData rawResponse, CancellationToken cancellationToken = default)
{
// make sure to call base method to ensure default logic runs
await base.HandleMessageSentAsync(client, message, response, rawResponse, cancellationToken);
if (base.IsUsersCachingEnabled && response is MyCustomResponse customResponse)
{
// do something
}
}
// override this method to handle received events and messages
public override async Task HandleMessageReceivedAsync(IWolfClient client, IWolfMessage message,
SerializedMessageData rawMessage, CancellationToken cancellationToken = default)
{
// make sure to call base method to ensure default logic runs
await base.HandleMessageReceivedAsync(client, message, rawMessage, cancellationToken);
if (base.IsUsersCachingEnabled && message is MyCustomMessage customMessage)
{
// do something
}
}
}
Custom Client Cache
If you need more customization of entity caching (for example, you want to cache using Redis), you can create a new class implementing IWolfClientCache interface. This abstraction requires a few components:
- OnMessageSentAsync() - invoked when IWolfClient has sent a message and has received a server response. You can use it to cache entities sent by the server in a response. Note: if the response indicated an error, this method will not be called.
- OnMessageReceivedAsync() - invoked when IWolfClient has received a new event or message from the server. You can use it to cache entities sent by the server.
- OnConnectingAsync() - invoked before IWolfClient establishes connection to the server.
- OnDisconnected() - invoked when IWolfClient has disconnected from the server.
- A few methods to retrieve specific entities from the cache - see IWolfClientCacheAccessor.
Once your custom class is finished, you need to register it with WolfClientBuilder as explained in Introduction.