-
Notifications
You must be signed in to change notification settings - Fork 1
3. Configuration
The caching policy is defined per entity. When using cache for a particular entity, its policy is retrieved using entity type as a key. If it is needed to have multiple caching policies, named caching policies should be used. To define a new caching policy
new CacheBuilder();
or invoke AddFluentCaching
AddFluentCaching(cacheBuilder => {/* your configuration here */});
cachebuilder.For<User>(u => {/* your configuration here */});
If one of the entity properties should be used as a key, call
cacheBuilder => cachebuilder.For<User>(
u => u.UseAsKey(s => $"User:{s.Id}"));
If a static string should be used as a key, call
cacheBuilder => cachebuilder.For<User>(
u => u.UseAsKey("CurrentUser"));
If the entity class name should be used as a key, call
cacheBuilder => cachebuilder.For<User>(
u => u.UseClassNameAsKey());
If the entity class full name should be used as a key, call
cacheBuilder => cachebuilder.For<User>(
u => u.UseClassFullNameAsKey());
In case the key is composite, one of CombinedWith methods may be used with the same options.
cacheBuilder => cachebuilder.For<User>(
u => u.UseAsKey(u => u.FirstName)
.CombinedWith(u => u.LastName));
cacheBuilder => cachebuilder.For<User>(
u => u.UseAsKey(u => u.FirstName)
.And().SetExpirationTimeoutTo(5).Minutes.And(6).Seconds
.With().AbsoluteExpiration());
In case there is no expiration timeout, SetInfiniteExpirationTimeout should be used, in the case configuring expiration type is not required.
cacheBuilder => cachebuilder.For<User>(
u => u.UseAsKey(u => u.FirstName)
.And().SetInfiniteExpirationTimeout());
Currently memory and distributed cache (Redis) storage are supported. Also, it is possible to use custom storage implementation.
cacheBuilder => cachebuilder.For<User>(
u => u.UseAsKey(u => u.FirstName)
.And().StoreInMemory());
Or
cacheBuilder => cachebuilder.For<User>(
u => u.UseAsKey(u => u.FirstName)
.And().StoreInDistributedCache());
Or
cacheBuilder => cachebuilder.For<User>(
u => u.UseAsKey(u => u.FirstName)
.And().StoreIn(new CustomStorageImplementation()));
In the examples above, the storage implementation is configured for a specific entity, but it can also be configured globally. cacheBuilder => cachebuilder.SetInMemoryAsDefaultCache();
Or In the examples above, the storage implementation is configured for a specific entity, but it can also be configured globally. cacheBuilder => cachebuilder.SetDistributedAsDefaultCache();
Or In the examples above, the storage implementation is configured for a specific entity, but it can also be configured globally. cacheBuilder => cachebuilder.SetGenericCache(new CustomStorageImplementation());