Cache policy
IRequestPolicy<CachePolicy> evaluates on each request type whether to cache or not. It must be provided on service construction to support caching.
// Choose cache policy (Yes)
IRequestPolicy<CachePolicy> cachePolicy = CachePolicies.DefaultYes;
// Create service with cache
IService service = Services.Create((ServiceHandlers.Instance), cachePolicy);
Cache Policies
CachePolicy is enumeration of three possible values.
Key | Description |
---|---|
CachePolicy.ToCache | Request type is to be cached. |
CachePolicy.ToNotCache | Request type is not to be cached. |
CachePolicy.Unassigned | No assignment for request type. If final evaluation result is Unassigned, then policy reverts to ToNotCache. |
CachePolicies is a facade that contains various cache policy rules:
Key | Applied Type | Evaluation Result |
---|---|---|
CachePolicies.Default | UseInterface Others |
→ ToCache → ToNotCache |
CachePolicies.DefaultYes | IRequestNotToBeCached Others |
→ ToNotCache → ToCache |
CachePolicies.ToCache | All types → ToCache | |
CachePolicies.ToNotCache | All types → ToNotCache | |
CachePolicies.UseInterface | IRequestToBeCached IRequestNotToBeCached Others |
→ ToCache → ToNotCache → Unassigned. |
CachePolicies.Cache<T> | T | → ToCache |
CachePolicies.Cache(Type requestType) | requestType | → ToCache |
CachePolicies.DontCache<T> | T | → ToNotCache |
CachePolicies.DontCache(Type requestType) | requestType | → ToNotCache |
CachePolicies.List(enumr) | Evaluates all rules in enumr | |
CachePolicies.Map(enumr) | Creates lookup table from enumr. | |
CachePolicies.Func(func) | Invokes func. |
Custom cache Policy
Custom policy can be created by appending rules with extension methods. Rules are evaluated in the order of appending.
// Create cache policy
IRequestPolicy<CachePolicy> cachePolicy =
CachePolicies
.DontCache<string>()
.DontCache<Type>()
.UseInterface()
.CacheRest();
.Coalesce() convertes preceding constant based rules into one lookup-table.
// Create cache policy
IRequestPolicy<CachePolicy> cachePolicy =
CachePolicies
.Cache<string>()
.Cache<int>()
.Cache<uint>()
.DontCache<long>()
.DontCache<ulong>()
.DontCache<bool>()
.Coalesce()
.UseInterface()
.DontCacheRest();
.Func() applies a function to evaluate caching.
// Create cache policy
IRequestPolicy<CachePolicy> cachePolicy =
CachePolicies
.Func(t => t.Name.EndsWith("Cached") ? CachePolicy.ToCache : CachePolicy.Unassigned)
.UseInterface()
.DontCacheRest();
There are extension methods that can be used for continuing cache policies.
Key | Applied Type | Evaluation Result |
---|---|---|
policy.Cache<T>() | T | → ToCache |
policy.Cache(Type requestType) | requestType | → ToCache |
policy.DontCache<T>() | T | → ToNotCache |
policy.DontCache(Type requestType) | requestType | → ToNotCache |
policy.UseInterface() | IRequestToBeCachedUseInterface IRequestNotToBeCached Others |
→ ToCache → ToNotCache → Unassigned |
policy.CacheRest() | All types | → ToCache |
policy.DontCacheRest() | All types | → ToNotCache |
policy.Coalesce() | Coaleses preceding rules into a lookup-table. |
Disconnecting cache entries
IEntryProviderBase.TryDisconnect() and .TryDisconnectAll() disconnects results from cache.
// Create splitter
Action<IQuery<string, string[]>> splitter = q => q.Response.SetValue(q.Request.Split(","));
// Create service with cache
IService<string, string[]> service = Services.Create<string, string[]>(splitter, CachePolicies.ToCache);
// Create request
string request = "car,bike,mountain";
// Get-and-cache result
string[] words = service.GetRequired(request);
// Get cache
(service as IEntryProviderBase).TryGetEntryCache(out IEntryCacheBase? cache);
// Disconnect specific request
cache!.TryDisconnect(request);
// Disconnect all cache lines
cache!.TryDisconnectAll();
// Create result again
string[] wordsAgain = service.GetRequired(request);
// Test if we got different instances (expect to get false)
WriteLine(Object.ReferenceEquals(words, wordsAgain));
Full Example
Full example
using System;
using Avalanche.Service;
using static System.Console;
public class service_cache
{
public static void Run()
{
{
// <01>
// Choose cache policy (Yes)
IRequestPolicy<CachePolicy> cachePolicy = CachePolicies.DefaultYes;
// Create service with cache
IService service = Services.Create((ServiceHandlers.Instance), cachePolicy);
// </01>
}
{
// <02>
// Create cache policy
IRequestPolicy<CachePolicy> cachePolicy =
CachePolicies
.DontCache<string>()
.DontCache<Type>()
.UseInterface()
.CacheRest();
// </02>
}
{
// <03>
// Create cache policy
IRequestPolicy<CachePolicy> cachePolicy =
CachePolicies
.Cache<string>()
.Cache<int>()
.Cache<uint>()
.DontCache<long>()
.DontCache<ulong>()
.DontCache<bool>()
.Coalesce()
.UseInterface()
.DontCacheRest();
// </03>
}
{
// <04>
// Create cache policy
IRequestPolicy<CachePolicy> cachePolicy =
CachePolicies
.Func(t => t.Name.EndsWith("Cached") ? CachePolicy.ToCache : CachePolicy.Unassigned)
.UseInterface()
.DontCacheRest();
// </04>
}
{
// <05>
// Create splitter
Action<IQuery<string, string[]>> splitter = q => q.Response.SetValue(q.Request.Split(","));
// Create service with cache
IService<string, string[]> service = Services.Create<string, string[]>(splitter, CachePolicies.ToCache);
// Create request
string request = "car,bike,mountain";
// Get-and-cache result
string[] words = service.GetRequired(request);
// Get cache
(service as IEntryProviderBase).TryGetEntryCache(out IEntryCacheBase? cache);
// Disconnect specific request
cache!.TryDisconnect(request);
// Disconnect all cache lines
cache!.TryDisconnectAll();
// Create result again
string[] wordsAgain = service.GetRequired(request);
// Test if we got different instances (expect to get false)
WriteLine(Object.ReferenceEquals(words, wordsAgain));
// </05>
}
// CachePolicies facade
{
var x = CachePolicies.ToCache;
x = CachePolicies.ToNotCache;
x = CachePolicies.Default;
x = CachePolicies.DefaultYes;
x = CachePolicies.UseInterface;
x = x.Cache(typeof(string));
}
}
}