Using spring.net technology to create switchable distributed cache read-write classes

Source: Internet
Author: User
Tags memcached

Using spring.net technology to create switchable memcached distributed cache read-write classes

  Memcached is a high-performance distributed memory object cache system, because the work in memory, read and write rate than the database is not more general, and Radis as well as efficient read and write and distributed advantages, the previous blog "memcached in Windows configuration and use" The configuration and use of it on Windows has been described.   New Icachewriter class--cachewriter interface, to achieve through the configuration file can switch cache read and write mode, for example, the cache read and write can also be done through Httpruntime.cache. The code below:  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} The replication code adds the Memcached server address under the appsettings node in the configuration file. For example: <add key= "Memcachedserver" value= "127.0.0.1:11211"/> New Memcachedwriter class, code as follows: Copy code  1        /Singleton mode  2         private static readonly memcachedclient client; 3   &NBS P     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 ();             pool. Setservers (servers)             pool. Initconnections = 1;12             pool. Minconnections = 1;13             pool. MaxConnections = 3;14             pool. Socketconnecttimeout = 1000;//socket Connection timed out, idle for a few milliseconds after destroying socket15             pool. Sockettimeout = 3000;16             pool. Maintenancesleep = 30;//Maintenance thread rest time             pool. Failover = true;//Fail-over (a backup operation mode)    18             pool. Nagle = false;//is started with Nagle algorithm socket 19           &NBSp Pool. Initialize ();//Apply settings and initialize socket pool 20 21            //create memcached client     nbsp       client = new Memcachedclient (), +             client. EnableCompression = false;//is compressed 24 25        }26         public void Set (s Tring key, object value, DateTime exp)         {$             client. Set (key, Value, exp),        }30 31         public void set (string key, OB Ject value)         {+/            client. Set (key, value),        }35 36         public object Get (string key) PNs &NB Sp       {$             return client. Get (key),        }         Copy the code so that the server can be added and modified by configuration. &nbsP With the interface class and the implementation class, the next step is to use spring.net to implement its factory.   New Cachehelper class, code as follows: Copy code  1 public class cachehelper 2     { 3       &NB Sp public static icachewriter Cachewriter {get; set;}  4  5         static cachehelper ()   6         {&NBSP;&NB Sp;7            //If it is a static property and you want it to have injected value, you must create an instance before you can inject  8         &NB Sp  //static method call, the spring container does not need to create an instance, so the attribute Cachewriter does not inject an instance  9            // Forcing the spring container to create an instance of a property for us in the static constructor of the class, because the property is static, so you only need to create it once 10 11             Iapplicationcontext CTX = Contextregistry.getcontext ()             CTX. GetObject ("Cachehelper"), 13 14        }15         public static void Writeca Che (string key,object value,datetime exp)         {+     &nbsP       Cachewriter.set (key, Value, exp),        }19         Publi c static void Writecache (string key, Object value)         {+           &N Bsp Cachewriter.set (key, value),        }23 24         public static object Ge Tcache (string key)         {            return Cachewriter.get (key) ;        }28    } copy code public static Icachewriter Cachewriter {get; set;} This attribute is the injection point of the spring.net.   It's important to note that Because Spring.net is injected only after the class has the first instance, only static fields can be called in static methods, static methods and static fields are created when the program starts running, and Cachehelper does not have the first instance, so the static field cachewriter is not injected and requires manual Instantiate Cachehelper and let Cachewriter be injected.   Add configuration information about Cachehelper and cachewriter in 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= "Myoa_common.cachehelper, Myoa_common"  singleton= "false" >4     <property Name= "Cachewriter" ref= "Memcachedwriter"/>5   </object>6   <object name= "Memcachedwriter" type= "Myoa_common.memcachedwriter, Myoa_common"  singleton= "true" >7     8   </object>9 </objects> Copy code if we want to use Httpruntime.cache instead of memcached for cache read and write, we just need to modify  <object name= "Memcachedwriter" Type= "Myoa_common.memcachedwriter, Myoa_common"  singleton= "true" >  uses spring.net and interfaces to enhance the flexibility of our program.   OK, let's test the code on the controller: copy code  1     public class testcontroller:controller 2     { 3 & nbsp      //GET:/test/ 4         public ActionResult Test ()  5     &NB Sp   { 6             CacheHelper.CacheWriter.Set ("Test", "tested successfully"),  7   &nbs P         return View ();  8        } 9 10         [httppost]11         public Actionr Esult Test (formcollection form)         {+             String value = (string) CacheHelper.CacheWriter.Get ("Test"),             return Content (value),     & nbsp  }16 17    }

Using spring.net technology to create a switchable distributed cache read-write class

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.