Cache ~ 7. redis implements data set caching Based on method signatures (controllable updates and distributed data caching)

Source: Internet
Author: User

Returned directory

This article is the sixth Microsoft. practices. enterpriselibrary. caching implements method-based signature-based data set caching (controllable updates, web-side data caching). In fact, there is enterpriselibrary. caching is only a way to achieve cache persistence. redis, as a mature distributed storage middleware, is more handy in implementing this dataset cache function and meets the design rules of large websites. (When multiple Web servers are used (the web server implements load balancing and reverse proxy), enterpriselibrary. caching seems useless. In this case, the distributed cache can be used to easily deploy the cache server to a third-party server to solve the above problems)

One standard, multiple implementations, and the true meaning of object-oriented: polymorphism. If you ask me what the interface is for, this article will tell you the answer: Depending on the situation, use different persistence methods to store data.

The following is the cache standard interface icacheprovider.

/// <Summary> /// indicates that the type of the interface implemented is the type that can provide the cache mechanism for the application. /// This can have multiple implementation mechanisms /// </Summary> Public interface icacheprovider {# Region Methods /// <summary> /// add an object to the cache. /// </Summary> /// <Param name = "key"> the cached key value, which is usually the name of the method using the cache mechanism. </Param> // <Param name = "valkey"> the key value of the cached value, which is usually generated by the parameter value of the method using the cache mechanism. </Param> /// <Param name = "value"> specifies the object to be cached. </Param> void add (string key, string valkey, object value); // <summary> // update an object to the cache. /// </Summary> /// <Param name = "key"> the cached key value, which is usually the name of the method using the cache mechanism. </Param> // <Param name = "valkey"> the key value of the cached value, which is usually generated by the parameter value of the method using the cache mechanism. </Param> /// <Param name = "value"> specifies the object to be cached. </Param> void put (string key, string valkey, object value); // <summary> // read the object from the cache. /// </Summary> /// <Param name = "key"> the cached key value, which is usually the name of the method using the cache mechanism. </Param> // <Param name = "valkey"> the key value of the cached value, which is usually generated by the parameter value of the method using the cache mechanism. </Param> /// <returns> the cached object. </Returns> Object get (string key, string valkey); // <summary> /// remove an object from the cache. /// </Summary> /// <Param name = "key"> the cached key value, which is usually the name of the method using the cache mechanism. </Param> void remove (string key); // <summary> // obtain a <see CREF = "Boolean"/> value, indicates whether the cache with the specified key value exists. /// </Summary> /// <Param name = "key"> specifies the key value. </Param> /// <returns> If the cache exists, true is returned. Otherwise, false is returned. </Returns> bool exists (string key); // <summary> // obtain a <see CREF = "Boolean"/> value, indicates whether the cache with the specified key value and cache key exist. /// </Summary> /// <Param name = "key"> specifies the key value. </Param> /// <Param name = "valkey"> cache value Key. </Param> /// <returns> If the cache exists, true is returned. Otherwise, false is returned. </Returns> bool exists (string key, string valkey); # endregion}

This time, we use redis for persistence. Let's look at a rediscacheprovider code, which changes the dictionary <string, Object> type to dictionary <string for compatibility, byte []> type. This design avoids many errors, because we know that data will be serialized during transmission, while compatibility,

The best method for security and performance is binary. Therefore, we use it to store data.

/// <Summary> // use redis for cache persistence // </Summary> internal class rediscacheprovider: icacheprovider, idisposable {private readonly iredisclient _ cachemanager = redis. client. redismanager. getclient (); static byte [] serialize (Object Data) {binaryformatter formatter = new binaryformatter (); memorystream REMs = new memorystream (); formatter. serialize (REMs, data); Return rems. getbuffer ();} static obje CT deserialize (byte [] data) {binaryformatter formatter = new binaryformatter (); memorystream REMs = new memorystream (data); Data = NULL; return formatter. deserialize (REMs);} public void add (string key, string valkey, object Value) {byte [] bytevalue = serialize (value); Using (var tbl = _ cachemanager. gettypedclient <dictionary <string, byte []> () {dictionary <string, byte []> dict = NULL; If (TBL. c Ontainskey (key) {dict = (Dictionary <string, byte []>) TBL. lists [Key] [0]; dict [valkey] = bytevalue;} else {dict = new dictionary <string, byte []> (); dict. add (valkey, bytevalue);} remove (key); TBL. lists [Key]. add (dict) ;}} public void put (string key, string valkey, object Value) {Add (Key, valkey, value);} public object get (string key, string valkey) {using (var tbl = _ cachemanager. gettypedclien T <dictionary <string, byte []> () {If (TBL. containskey (key) {dictionary <string, byte []> dict = (Dictionary <string, byte []>) TBL. lists [Key] [0]; If (dict! = NULL & dict. containskey (valkey) return deserialize (dict [valkey]); else return NULL ;}return null ;}public void remove (string key) {using (var tbl = _ cachemanager. gettypedclient <dictionary <string, byte []> () {TBL. lists [Key]. removeall () ;}} public bool exists (string key) {using (var tbl = _ cachemanager. gettypedclient <dictionary <string, byte []> () {return TBL. containskey (key) ;}} public bool exists (string key, string valkey) {using (var tbl = _ cachemanager. gettypedclient <dictionary <string, byte []> () {return TBL. containskey (key) & (system. collections. generic. dictionary <string, byte []>) TBL. lists [Key] [0]). containskey (valkey) ;}} public void dispose () {_ cachemanager. dispose ();}}

As a matter of fact, our redis method signature storage is complete. With the help of the previous article, you can design your own cache system. Here, let's talk about it again, the cache system uses a design pattern similar to a policy pattern. It can implement multiple policies for a complete function, but only exposes one standard object to the outside.

All functions are implemented usingInternalAs a modifier to hide the outside world.

Returned directory

Cache ~ 7. redis implements data set caching Based on method signatures (controllable updates and distributed data caching)

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.