This program records
- Introduced
- Icachemanager
- ICache
- Itypedcache
- Configuration
Introduced
The ABP provides 1 cached abstractions. This cache abstraction is used internally. Although the default implementation uses MemoryCache, it can be switched to other caches.
Icachemanager
The interface that provides the cache is Icachemanager. We can inject and use him. For example:
public class testappservice:applicationservice{ private readonly icachemanager _cachemanager; Public Testappservice (Icachemanager CacheManager) { _cachemanager = CacheManager; } Public Item GetItem (int id) { //try to get from cache return _cachemanager . GetCache ("Mycache") . Get (ID. ToString (), () = Getfromdatabase (ID)) as Item; } Public Item getfromdatabase (int id) { //... retrieve Item from database }}
in the example above, we inject Icachemanager and get 1 of the names Mycache the cache.
WARNING:GetCache Method
Do not use the GetCache method in your constructor. If your class is transient, this may release the cache.
ICache
Icachemanager. The GetCache method returns 1 Icache. Each cache is singleton. Created on the first request, and then always using the same cache instance. So, we share the cache in different classes.
ICache. The get method has 2 parameters:
- Key: The only string in the cache.
- Factory: On the given key, the corresponding item is not found. Factory method should create and return the corresponding item. If found in the cache, the action is not invoked.
ICache interface also have methods like getordefault, Set, Remove and Clear. There is also async versions of all methods.
The ICache interface also provides methods such as Getordefault, Set, Remove , and Clear . The async version of all methods is also available.
Itypedcache
The ICache interface is the value of string key,object. Itypedcache Packaged Icache provides type-safe, generic cache. To convert Icache to Itypedcache, we need to use the astyped extension method:
Itypedcache<int, item> Mycache = _cachemanager.getcache ("Mycache"). Astyped<int, item> ();
Then we use the Get method, there is no need to do manual conversion.
Configuration
The default cache expire time is 60min. This is the sliding expiration mode. So when you don't use 1 item for more than 60 minutes, it's automatically removed from the cache. You can configure all caches or 1 caches.
Configuration for all cachesConfiguration.Caching.ConfigureAll (cache =>{ cache. Defaultslidingexpiretime = timespan.fromhours (2);}); /configuration for a specific cacheConfiguration.Caching.Configure ("Mycache", Cache =>{ cache. Defaultslidingexpiretime = timespan.fromhours (8);});
This code should be placed in the module's preinitialize method. As in the code above, Mycache will have 8 hours of expire time, while the other caches have 2 hours.
Your configuration action will only be called once on the first request. In addition to expire time, you can also freely configure and initialize Icache.
[Architect] ABP (modern ASP. NET Template Development Framework) (9) Caching