Memcached is powerful, needless to say! My application here is just the tip of the iceberg. Are some basic applications.
You can download it from the official memcached website. The latest version is 1.4.15. I downloaded the stable Windows Version 1.4.14.
After downloading the file, decompress it and copy the file in memcached to the directory you want to create.
Then install it in DOS! Some basic parameters can be set in DOS or in projects, such as the maximum number of connections, the minimum number of connections, and the timeout time. (my VM does not work anymore! FK. The specific parameter settings are not mentioned !). For more information about the parameters, see a list on the Internet under "Examples.
-P listening port
-L connected IP address. The default value is local
-D start: Start the memcached service.
-D restart: restart the memcached service.
-D stop | Shutdown the running memcached Service
-D install the memcached Service
-D uninstall memcached Service
-U runs as the identity (only valid when running as root)
-MB maximum memory usage, in MB. The default value is 64 MB.
-An error is returned when M memory is used up, instead of deleting items.
-C: Maximum number of simultaneous connections. The default value is 1024.
-F block size growth factor, default value: 1.25
-N: minimum allocation space. The default value of key + value + flags is 48.
-H Show Help
After the mvc3.0 project is set up, the global. asax file is automatically generated, a global file of the project! Here we set the memcached startup parameter!
Public static void cachepool () {string poolname = "default"; string [] serverlist = {"127.0.0.1: 11211"};-> to deploy distributed architecture, append an IP address next to it! For example, string [] serverlist = {"192.168.1.1: 11211", "192.168.1.2: 11211"}; sockiopool pool = sockiopool. getinstance (poolname); pool. setservers (serverlist); // sets the Server LIST // sets the pool for Server Load balancer between servers. setweights (New int [] {1});-> distributed deployment is an important parameter, which is equivalent to setting the Primary and Secondary cache values! // Set the pool for the socket pool. initconnections = 5; // connection pool created during initialization. minconnections = 5; // pool of minimum connections. maxconnections = 2000; // maximum number of connections // maximum idle time of the connection. The value below is set to 6 hours (unit: MS). If the maximum idle time is exceeded, the connection will be released from the pool. maxidle = 1000*60*60*6; // The communication timeout time. Set it to 3 seconds (in MS ),. net version does not implement pool. sockettimeout = 1000*3; // timeout value of the socket connection. The following settings indicate that the connection does not time out, that is, the pool is always in the connection status. socketconnecttimeout = 0; pool. nagle = false; // whether to use the nalgle Algorithm for TCP/IP communication ,. net version does not implement // The interval activation time of the maintenance thread, the following is set to 60 seconds (unit: S), set to 0 indicates that the maintenance thread pool is not enabled. maintenancesleep = 60; // maximum time of a single socket task. After this time, the socket is forcibly interrupted (the current task fails. maxbusy = 1000*10; pool. failover = true; pool. initialize ();} protected void application_start () // call {cachepool ();}
OK. The basic settings are complete. Next, we need to download two DLL files. Used to integrate with net. One is memcached. clientlibrary and memcachedproviders!
Next, we will write a cache operation class datacache.
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. web; using memcached. clientlibrary; using system. security. cryptography; namespace common {// <summary> // memcached cache operation class /// </Summary> public class datacache {public static void setmc (Object OBJ, string cachename) {string poolname = "default"; cachename = getmd5 (cachename); // client instance memcachedclient MC = New memcached. clientlibrary. memcachedclient (); Mc. poolname = poolname; // if no poolname exists during pool instantiation, this row does not need to be used. MC. enablecompression = true; Mc. compressionthreshold = 10240; If (OBJ! = NULL) MC. set (cachename, OBJ, datetime. now. addhours (3);} public static void setmc (Object OBJ, string cachename, int minutes) {string poolname = "default"; cachename = getmd5 (cachename ); // client instance memcachedclient MC = new memcached. clientlibrary. memcachedclient (); MC. poolname = poolname; // if no poolname is available during pool instantiation, this row does not need to be used. MC. compressionthreshold = 10240; If (OBJ! = NULL) MC. set (cachename, OBJ, datetime. now. addminutes (minutes);} public static object getmc (string cachename) {string poolname = "default"; cachename = getmd5 (cachename); // client instance memcachedclient MC = new memcached. clientlibrary. memcachedclient (); MC. poolname = poolname; // if no poolname is available during pool instantiation, this row does not need to be used. MC. enablecompression = true; Return (object) MC. get (cachename);} public static void updatemc (Object OBJ, string cachename) {string poolname = "default"; cachename = getmd5 (cachename ); // client instance memcachedclient MC = new memcached. clientlibrary. memcachedclient (); MC. poolname = poolname; // if no poolname is available during pool instantiation, this row does not need to be used. MC. Delete (cachename); If (OBJ! = NULL) {MC. enablecompression = true; MC. set (cachename, OBJ) ;}} public static void delmc (string cachename) {string poolname = "default"; cachename = getmd5 (cachename ); // client instance memcachedclient MC = new memcached. clientlibrary. memcachedclient (); MC. poolname = poolname; // if no poolname is available during pool instantiation, this row does not need to be used. MC. delete (cachename );} /// <summary> /// cache MD5 /// </Summary> /// <Param name = "str"> </param> /// <returns> </returns> Public static string getmd5 (string Str) {int size = Common. pagebase. memcachedmd5size; If (size = 16) {// 16-bit md5cryptoserviceprovider MD5 = new md5cryptoserviceprovider (); STR = bitconverter. tostring (md5.computehash (system. text. utf8encoding. default. getbytes (STR), 4, 8); STR = Str. replace ("-", "");} else {// 32-bit byte [] B = system. text. encoding. default. getbytes (STR); B = new md5cryptoserviceprovider (). computehash (B); For (INT I = 0; I <B. length; I ++) {STR + = B [I]. tostring ("X "). padleft (2, '0') ;}} return STR ;}}}
OK. You can see these two parameters. Sets the memcached cache compression ratio. A single item of memcached can store up to 1 MB of data.
mc.EnableCompression = true;
There are two reasons for the MD5 encryption of the cache key: 1. memcached imposes a limit on the key length. If the length exceeds the limit, the current item remains invalid. 2. security issues.
The basic settings have been completed. Next we can use datacache to cache data.
/// <Summary> /// get model by code /// </Summary> /// <Param name = "ordercode"> </param> /// <returns> </returns> Public orderinfo orderinfomodelbyordercode (string ordercode) {string cachekey = "orderinfomodelbyordercode _" + ordercode; object objmodel = Common. datacache. getmc (cachekey); If (objmodel = NULL) {try {orderinfo ordermodel = Dal. orderinfomodelbyordercode (ordercode); objmodel = ordermod El; If (objmodel! = NULL) {int cachetime = Common. pagebase. memcachedtime; // memcached automatic timeout time, for example, set 10. the current cache item will automatically expire after 10 minutes. If a new request is made, add common again. datacache. setmc (objmodel, cachekey, cachetime) ;}} catch (exception ex) {}} return (orderinfo) objmodel ;}
Here, the basic application is complete.