Cache, browser cache

Source: Internet
Author: User

Cache, browser cache

 

During normal development, Redis cache is used for medium and large projects, but in some small projects, runtime cache is used. net framework, namespace System. runtime. caching.

The bottom layer is not very good. I have never studied the cache. I only think that it is enough to achieve the expected goal. Therefore, I usually gradually explore it.

Recently, a management system used by the traffic police has been developed to reduce the number of reads to the database, and the data of each user must be isolated. This requires caching. There is a question during usage: whether the runtime cache copies and saves objects, or modifies the object's memory policy.

I am very busy at ordinary times. Today, I just wrote a test to verify my conjecture. No one has ever said or talked about this knowledge. Now I want to share it with other students who may have the same questions.

 

First, paste the test class I wrote. If the result is inaccurate, point out:

// Cache class public class CacheEntity {public Guid {get; set;} public int State {get; set;} public DateTime CachedOn {get; set ;} public string Desc {get; set;} public override string ToString () {return $ "Object [{this. guid}] State: [{State}] CachedOn: [{CachedOn}] Desc: [{Desc}]. ";}}

 

// Obtain the object protected virtual CacheEntity GetTestCacheEntity () {return new CacheEntity {Guid = Guid. newGuid (), CachedOn = DateTime. now, Desc = "initialization", State = 1 };}// obtain the tested Cache Policy protected virtual ICacheManager GetTestCacheManager () {return new MemoryCacheManager ();}

 

The next step is the test method.

/// <Summary> /// verify the conjecture that cached data is used to redefine the storage policy of the object in the memory. /// not copying a copy for caching, but destroying objects in the memory delayed. /// </summary> [Test] public void Valid_mind () {var originalEntity = GetTestCacheEntity (); var cacheManager = offline (); var cacheKey = "CacheEntity"; cacheManager. set (cacheKey, originalEntity, 5); cacheManager. isSet (cacheKey ). shouldBeTrue (); Console. writeLine ($ "{DateTime. now}: Get cache... "); var cacheEntity = cacheManager. get <CacheEntity> (cacheKey); cacheEntity. guid. shouldEqual (originalEntity. guid); cacheEntity. state. shouldEqual (originalEntity. state); cacheEntity. cachedOn. shouldEqual (originalEntity. cachedOn); cacheEntity. desc. shouldEqual (originalEntity. desc); Console. writeLine (cacheEntity); Console. writeLine ($ "{DateTime. now}: Entity has been cache. "); Console. writeLine (); Console. writeLine ($ "-----------------------------------"); Console. writeLine (); Console. writeLine ($ "{DateTime. now}: Change Entity's data, and valid again. "); cacheEntity. guid = Guid. newGuid (); cacheEntity. state = 2; cacheEntity. cachedOn = DateTime. now; cacheEntity. desc = "data changed"; Console. writeLine ($ "{DateTime. now}: Get cache again... "); cacheEntity = cacheManager. get <CacheEntity> (cacheKey); Console. writeLine (cacheEntity); Console. writeLine ($ "{DateTime. now}: Test ends. ");}

In the test method, only the cache is set once, and others read the cache.

The test result is as follows:

Conclusion:

The runtime cache changes the object's memory storage policy, so that the object is not recycled by GC and can be used again in the following process.

Related Article

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.