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

Source: Internet
Author: User

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 generally more than the average, and radis as well as efficient read and write and distributed advantages, the previous blog post " the configuration and use of memcached under Windows has been described for its configuration and use on Windows.

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 the httpruntime.cache.

The code is as follows:

 1  public  interface   Icachewriter  2  3  void  Set (string  key, object   value, DateTime exp);  4  void  Set (string  Key, object   value);  5  object  Get (string   key);  6 } 
Add the memcached server address under the appsettings node in the configuration file. For example:
<add key="memcachedserver" value="127.0.0.1:11211" />
Create a new Memcachedwriter class with the following code:
1         //single-case mode2         Private Static ReadOnlymemcachedclient client;3         StaticMemcachedwriter ()4         {           5 6             string[] Servers = configurationmanager.appsettings["Memcachedserver"]. Split (New Char[] {','}, stringsplitoptions.removeemptyentries);7 8             //initializing the socket pool9Sockiopool pool =sockiopool.getinstance ();Ten pool. Setservers (servers); OnePool. Initconnections =1; APool. Minconnections =1; -Pool. MaxConnections =3; -Pool. Socketconnecttimeout = +;//socket connection timed out, idle for how many milliseconds after the socket is destroyed thePool. Sockettimeout = the; -Pool. Maintenancesleep = -;//Maintain thread rest time -Pool. Failover =true;//Fail-over transfer (a backup operation mode) -Pool. Nagle =false;//whether to start the socket with the Nagle algorithm +Pool. Initialize ();//Apply settings and initialize the socket pool -  +             //Create memcached Client AClient =Newmemcachedclient (); atClient. EnableCompression =false;//whether to compress -  -         } -          Public voidSet (stringKeyObjectvalue, DateTime exp) -         { - client. Set (key, Value, exp); in         } -  to          Public voidSet (stringKeyObjectvalue) +         { - client. Set (key, value); the         } *  $          Public ObjectGet (stringkey)Panax Notoginseng         { -             returnclient. Get (key); the}

This allows you to add and modify servers through configuration.

With the interface class and the implementation class, the next step is to use spring.net to implement its factory.

Create a new Cachehelper class with the following code:
1       Public classCachehelper2     {3          Public StaticIcachewriter Cachewriter {Get;Set; }4 5         StaticCachehelper ()6         { 7             //if the property is static, and you want it to have injected value, you must create an instance before you can inject8             //when a static method is called, the spring container is not required to create an instance, so the attribute Cachewriter does not inject an instance9             //force the Spring container to create an instance of the property for us in the static constructor of the class, because the property is static, so you only need to create it once .Ten  OneIapplicationcontext CTX =Contextregistry.getcontext (); ACTx. GetObject ("Cachehelper"); -  -         } the          Public Static voidWritecache (stringKeyObjectvalue,datetime exp) -         { - Cachewriter.set (key, Value, exp); -         } +          Public Static voidWritecache (stringKeyObjectvalue) -         { + Cachewriter.set (key, value); A         } at  -          Public Static ObjectGetCache (stringkey) -         { -             returnCachewriter.get (key); -         } -}
public static icachewriter Cachewriter {get; set;}

This attribute is the injection point of the spring.net.

It is 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:

1 <Objectsxmlns= "Http://www.springframework.net">2   <!--cachewriter injection in Cachehelper, Cachewriter is a singleton -3   <Objectname= "Cachehelper"type= "Myoa_common.cachehelper, Myoa_common"Singleton= "false">4     < Propertyname= "Cachewriter"ref= "Memcachedwriter" />5   </Object>6   <Objectname= "Memcachedwriter"type= "Myoa_common.memcachedwriter, Myoa_common"Singleton= "true">7     8   </Object>9 </Objects>

If we want to use Httpruntime.cache instead of memcached for cache reading and writing, we just need to modify

<object name= "Memcachedwriter" type= "Myoa_common.memcachedwriter, Myoa_common"  singleton= "true" >

The use of spring.net and interfaces enhances the flexibility of our programs.

OK, let's test the code on the controller:
1      Public classTestcontroller:controller2     {3         //GET:/test/4          Publicactionresult Test ()5         {6CacheHelper.CacheWriter.Set ("Test","Test Success");7             returnView ();8         }9 Ten [HttpPost] One          Publicactionresult Test (formcollection form) A         { -             stringValue = (string) CacheHelper.CacheWriter.Get ("Test"); -             returnContent (value); the         } -  -}

"Test succeeded" is displayed on the webpage, that is, the distributed cache is complete!

The purpose of this article is to exchange and share experiences with your friends, and if you find anything wrong with this article or if you have any better suggestions, you are welcome to ask! You can also reply to or view my other posts if you have any knowledge of what is described in this article.

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

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.