Use Spring. Net technology to create a switchable distributed cache read/write class, spring.net read/write

Source: Internet
Author: User

Use Spring. Net technology to create a switchable distributed cache read/write class, spring.net read/write

Use Spring. Net technology to create a switchable Memcached distributed cache read/write class

Memcached is a high-performance distributed memory object cache system. Because it is working in the memory, the read/write rate is not generally higher than the database. It has the same advantages of efficient read/write and distributed as Radis, the configuration and use of Memcached in Windows has been introduced in the previous blog. Create an ICacheWriter class-CacheWriter interface to switch the cache read/write mode through the configuration file. For example, cache read/write can also be performed through httpruntime. cache. The Code is as follows: Copy code 1 public interface ICacheWriter2 {3 void Set (string key, object value, DateTime exp); 4 void Set (string key, object value ); 5 object Get (string key); 6} copy the code and add the memcached server address under the deleetasknode in the configuration file. For example, <add key = "memcachedServer" value = "127.0.0.1: 11211"/> Create a MemcachedWriter class. The Code is as follows: Copy code 1 // Singleton Mode 2 private static readonly MemcachedClient client; 3 static MemcachedWriter () 4 {5 6 string [] servers = ConfigurationManager. appSettings ["memcachedServer"]. split (new char [] {','}, StringSplitOptions. removeEmptyEntries); 7 8 // initialize socket pool 9 SockIOPool pool = SockIOPool. getInstance (); 10 pool. setServers (serv Ers); 11 pool. initConnections = 1; 12 pool. minConnections = 1; 13 pool. maxConnections = 3; 14 pool. socketConnectTimeout = 1000; // The socket connection times out. The socket15 pool is destroyed after several milliseconds of idle time. socketTimeout = 3000; 16 pool. maintenanceSleep = 30; // maintain the thread rest time 17 pool. failover = true; // Failover (a backup operation mode) 18 pool. nagle = false; // whether to use the nagle algorithm to start the socket 19 pool. initialize (); // set the application and Initialize the socket pool 20 21 // create the memcached client 22 client = new MemcachedClient (); 23 Client. enableCompression = false; // whether to compress 24 25} 26 public void Set (string key, object value, DateTime exp) 27 {28 client. set (key, value, exp); 29} 30 31 public void Set (string key, object value) 32 {33 client. set (key, value); 34} 35 36 public object Get (string key) 37 {38 return client. get (key); 39} copy the code so that you can add and modify the server through configuration. With the interface class and implementation class, we will use Spring. Net to implement its factory. Create a new CacheHelper class with the following code: Copy code 1 public class CacheHelper 2 {3 public static ICacheWriter CacheWriter {get; set;} 4 5 static CacheHelper () 6 {7 // if it is a static attribute and you want it to have a injected value, you must first create an instance before injecting 8 // when the static method is called, no need to create instances for Spring containers, so attribute CacheWriter does not inject instance 9 // force Spring containers to create an attribute instance for us in the static constructor of the class, because the property is static, you only need to create it once. 10 11 IApplicationContext ctx = ContextRegistry. getContext (); 12 ctx. getObject ("CacheHelper"); 13 14} 15 public s Tatic void WriteCache (string key, object value, DateTime exp) 16 {17 CacheWriter. set (key, value, exp); 18} 19 public static void WriteCache (string key, object value) 20 {21 CacheWriter. set (key, value); 22} 23 24 public static object GetCache (string key) 25 {26 return CacheWriter. get (key); 27} 28} copy the code public static ICacheWriter CacheWriter {get; set;}. This attribute is Spring.. Net injection point. It should be noted that because of Spring.. Net will only be injected after the class has the first instance, and only static fields can be called in static methods, the static method and static field have been created when the program starts running. At this time, CacheHelper does not have the First Instance. Therefore, the static field CacheWriter is not injected and you need to manually instantiate CacheHelper, causes CacheWriter to be injected. Add the configuration information about CacheHelper and CacheWriter to the Spring node of the configuration file: Copy code 1 <objects xmlns =" http://www.springframework.net "> 2 <! -- CacheWriter injection in CacheHelper. CacheWriter is a singleton --> 3 <object name = "CacheHelper" type = "mya_common.cachehelper, mya_common "singleton =" false "> 4 <property name =" CacheWriter "ref =" MemcachedWriter "/> 5 </object> 6 <object name =" MemcachedWriter "type =" mya_common.memcachedwriter ", mya_common "singleton =" true "> 7 8 </object> 9 </objects> copy the Code if we want to use httpruntime. instead of using Memcached for cache read/write, you only need to modify <object name = "Memca ChedWriter "type =" mya_common.memcachedwriter, mya_common "singleton =" true "> using Spring. Net and interfaces enhances the flexibility of our programs. Okay, let's Test the code on the Controller: Copy code 1 public class TestController: Controller 2 {3 // GET:/Test/4 public ActionResult Test () 5 {6 CacheHelper. cacheWriter. set ("test", "Test succeeded"); 7 return View (); 8} 9 10 [HttpPost] 11 public ActionResult test (FormCollection form) 12 {13 string value = (string) CacheHelper. cacheWriter. get ("test"); 14 return Content (value); 15} 16 17}

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.