Cache-run-time memory cache

Source: Internet
Author: User

In peacetime, when developing programs, medium-sized projects use the Redis cache, but in some smaller projects, run-time caches are used, provided by the. NET FRAMEWORK, and namespace System.Runtime.Caching.

Under the foundation is not very good, to the cache this piece never delve into, only think can achieve the desired goal is enough, so usually basically is gradually groping.

Recently, the development of a traffic police internal use of the management system, the need to reduce the read database, and each user's data must be isolated. This requires a cache. In the process of use, there is a question whether the runtime cache is a copy of the object or the memory policy of the object.

Usually busy, today just a bit of time, wrote a test to verify my guess, no one has ever said or where to say this piece of knowledge, now share to other students who may have the same doubts.

First put down the test class I wrote, if there is any place that will lead to inaccurate results, please indicate:

//Classes for caching         Public classcacheentity { PublicGUID GUID {Get;Set; }  Public intState {Get;Set; }  PublicDateTime Cachedon {Get;Set; }  Public stringDesc {Get;Set; }  Public Override stringToString () {return$"Object[{this. GUID}] State: [{State}] Cachedon:[{cachedon}] Desc:[{desc}]."; }        }

//get objects that are about to be cached       protected Virtualcacheentity gettestcacheentity () {return Newcacheentity {Guid=Guid.NewGuid (), Cachedon=DateTime.Now, Desc="Initialize", State=1            }; }              //get the cache policy used by the test        protected VirtualIcachemanager Gettestcachemanager () {return NewMemorycachemanager (); }

The next step is the test method.

/// <summary>        ///validating the conjecture, caching the data, is redefining the object's saving policy in memory. ///instead of copying a copy for caching, it delays destroying objects in memory. /// </summary>[Test] Public voidValid_mind () {varOriginalentity =gettestcacheentity (); varCacheManager =Gettestcachemanager (); varCacheKey ="cacheentity"; Cachemanager.set (CacheKey, originalentity,5); Cachemanager.isset (CacheKey).            Shouldbetrue (); Console.WriteLine ($"{DateTime.Now}: Get cache ..."); varCacheentity = 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 has 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, the cache is set only once, and the others are read.

Here are the test results:

The conclusion is:

The runtime cache changes the object's memory storage policy, which prevents the object from being reclaimed by GC and can be used again in the next procedure.

Cache-run-time memory cache

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.