Catel help manual-catel. Core: (2) cache Processing

Source: Internet
Author: User

 

Cache is used to provide application execution efficiency, and most of the application's spending is used for data acquisition. These processes are mainly moved to the network or stored on the hard disk, but some data will not change, you do not need to synchronize data every time.

Therefore, to improve the execution efficiency of applications, catel uses

Catchstorage <tkey, tvalue> class. Note that the first generic parameter represents the type of the keyword, and the second generic parameter represents the type of the value to be stored, it is like dictonary <tkey, tvalue>. However, catchestorage is not just a dictionary. This class allows you to use a statement to obtain data and store the data in the cache. You can use the termination policy when you need it.

1. initialize memory storage

You can use the following statement to initialize memory storage:

private readonly CacheStorage<string, Person> _personCache = new CacheStorage<string, Person>(storeNullValues: true);

 

2. Use the following statement to obtain and store data in the cache:
var person = _personCache.GetFromCacheOrFetch(Id, () => service.FindPersonById(Id));

When this statement is executed multiple times with the same key, the statement will be returned from the memory instead of from the service call. This service is only executed for the first time. This item can be manually removed or automatically removed using an expiration policy.

3. Use the cache expiration Policy

The cache expiration Policy deletes the cached storage project. When the project expires, a policy signal is sent to the cache storage, and the cache storage automatically removes the project.

You can create an expiration policy for this catchstorage during construction.

CacheStorage<string, Person> _personCache = new CacheStorage<string, Person>(() => ExpirationPolicy.Duration(TimeSpan.FromMinutes(5)), true);

If no storage policy is specified, the default expiration policy is set.

4. Create an expiration Policy

Catel comes with an expiration Policy:

Expiration Policy Type Description Initialization code example
Absoluteexpirationpolicy Time-based The cache will expire after the expiration time is reached. Use the absolute time ExpirationPolicy.Absolute(new DateTime(21, 12, 2012))
Durationexpirationpolicy Time-based The timespan method is used to define an actual segment of the current time, which expires after the time period expires. ExpirationPolicy.Duration(TimeSpan.FromMinutes(5))
Slidingexpirationpolicy Time-based
The timespan method is used to define an actual segment of the current time. It expires after the time period expires. However, when it is re-returned, the time is recalculated. ExpirationPolicy.Sliding(TimeSpan.FromMinutes(5))
Customexpirationpolicy Custom Cache expiration is determined by the function. The following example shows how to use the custom expiration policy to create a silding policy.

var startDateTime = DateTime.Now;

var duration = TimeSpan.FromMinutes(5);

ExpirationPolicy.Custom(() => DateTime.Now > startDateTime.Add(duration), () => startDateTime = DateTime.Now);

Compositeexpirationpolicy Custom A combination of multiple expiration policies can expire when any policy expires.

new CompositeExpirationPolicy().Add(ExpirationPolicy.Sliding(

TimeSpan.FromMinutes(5))).Add(ExpirationPolicy.Custom(()=>...))

2.5 implement your own expiration Policy

If customexpiationpolicy is not enough, you can create an expiration policy by yourself to make the cache project expire after an event is triggered, or after reading the cache project, to reset the expiration policy (such as slidingexpirationpolicy), you can use the following code template to implement this project:

public class MyExpirationPolicy : ExpirationCachePolicy{   public MyExpirationPolicy():base(true)   {   }    public override bool IsExpired   {      get      {         // Add your custom expiration code to detect if the item expires      }   }    public override void OnReset()   {      // Add your custom code to reset the policy if the item is read.   }}

 

Note: There is a parameter in the constructor. The policy is reset. If you call the constructor to use false, the onreset method will not be called.

Catel help manual-catel. Core: (2) cache Processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.