Memcached distributed cache Quick Start and memcached Quick Start
1. From Single-host to distributed
The first step towards distribution is to solve the problem of sharing login information among multiple machines. • For example, there are three machines in a Web application cluster, one of which is logged on, and the other two are shared in the logon status? • Solution 1: sessions outside the AspNet process. • Solution 2: Use a database to store the current logon status. • Solution 3: Memcache (best performance, similar to Redis and NoSql)
2. Why Memcached?
• High concurrency database access pain points: deadlocks! • Disk I/O pain: • Multi-client shared cache • Net + Memory> IO • Perfect read/write performance 1 s: Read requests. Write: 10 W • super simple Cluster construction Cluster • Open Source • No master/Slave assignment function, nor Disaster Tolerance and other functions, simply solving performance problems. (Redis supports disaster tolerance and backs up memory data to disks) • low learning costs, easy to get started • Rich success stories
III. Basic principles of Memcached:• Location: it is placed on the server side and communicated through Socket.
• Data: Key-Value Pair Storage
• Memory processing algorithms:
• In essence, it is a large hash table. The key cannot exceed 255 characters.
• Memory Model: Memcache pre-partitions disposable memory space (Slab). Each partition is further divided into multiple chunks with a size of 1 MB, but in the same partition: the block length (bytes) is fixed.
• Insert data: Find the blocks that are suitable for your own length and then insert them, which will result in a waste of memory.
• Data insertion rules: idle> expired> Minimum Access
• Inert deletion: it does not provide a mechanism for monitoring data to expire, but is inert. When a key data is queried, It is discarded if it expires.
• Cluster construction principle: the Memcache server does not provide cluster functions, but implements cluster configuration through the client driver.
• How the client implements the cluster: first, the client configures the ip address and port list of Multiple Cluster machines. Then, before writing the data, the client driver first performs hash processing on the key to obtain the hash value, then performs remainder on the total number of machines, and then selects the machine corresponding to the remainder.
Other Instructions: the key can contain a maximum of 255 characters, and the item memory block can contain a maximum of 1 MB. Of course, the key/item should not be too large, the maximum expiration time is 30 days for Memcache to pre-partition the disposable memory space (Slab). Each partition is then divided into multiple chunks, but in the same partition: the block length (bytes) is fixed. After a record is deleted from Memcache, the allocated memory (that is, Chunk) will not be released, but will be reused, in this way, the problem of memory fragmentation is completely solved. Memcache adopts the "inert" method to deal with the record's timeout problem. Consistent hash processing: bytes. the basic principle of CAS Memcached added the CAS protocol in version 1.2.4. It is similar to the CAS (Compare and Set) atomic operation in Java concurrent packages, it is used to handle the concurrency problem of the change process of the same item by multiple threads. the basic principle is very simple. In short, it is the "version number ". each stored data object has a version number. in Memcached, each key is associated with a 64-bit long unique value, indicating the version number of the value corresponding to the key. this value is generated by Memcached. It starts from 1 and the same Memcached will not be repeated. In either case, the value of this version is added, that is, add and update, while the value of the deleted item version is not reduced. we can understand from the following example: If CAS is not used, there are the following scenarios: Step 1, A retrieves the Data Object X; Step 2, B retrieves the Data Object X; step 3: B Modify the Data Object X and put it into the cache. Step 4: A modify the Data Object X and put it into the cache. We can find that Data Writing conflicts will occur in step 4. If the CAS protocol is used, the following scenario is used. Step 1, A retrieves the Data Object X and obtains the CAS-ID1; Step 2, B retrieves the Data Object X and obtains the CAS-ID2; step 3, B modifies the Data Object X, before writing to the cache, check that the CAS-ID is consistent with the CAS-ID for the data in the cache space. If the result is consistent, the modified X with a CAS-ID2 is written to the cache. Step 4, A modify data object Y, check that the CAS-ID is consistent with the CAS-ID of the data in the cache space before writing to the cache. If the result is "inconsistent", the write is rejected and the storage fails to be returned.View Code
Use Cases:
Content that is frequently accessed but rarely changed in the cache:
Such as: category, disabled words (matching with regular expressions ),
string str = string.Join(list.ToArray()); //aa|bb|ccRegex.IsMatch(msg,str);
4. Use Memcache in Windows
• Download the Memcache service program: Click to download> • copy the service program to a directory on a disk, for example, D: \ Memcached • Install the service: cmd → D: press enter to go to drive D d: \ Memcached press enter to go to drive d memcacheddirectory memcached.exe-D install to install memcachedservice memcached.exe-d start to start memcached Service (open the service monitoring window to check whether the service is started. Restart, stop close the service) • check whether the service is successfully installed: 1. First confirm that the telnet client is installed. If not, install it first. Installation tutorial> connect to the Memcache Console: telnet Server IP address 11211, for example, telnet 127.0.0.1 11211 (tested locally. If it is another server, change it to the server address. Port 11211 is fixed .) Enter cmd command to check: stats to check the current service status. If the preceding information is displayed, the installation is successful. • Uninstall the service: Memcached.exe-d uninstall (disable the service first) • problem: Install the service in win8. Unable to start this program because MSVCR71.dll is lost in the computer. Try to reinstall the program to solve this problem. Download dll address: http://www.dll-files.com/dllindex/dll-files.shtml? Msvcr71
Other Instructions:
Install Memcache.exe as a Windows Service: Memcache.exe-d install start Memcache service: Memcache.exe-d start Memcache Service (windows Command): net start "Memcache Server" Stop Memcache Service (windows Command ): net stop "Memcache Server" connect to Memcache Console: telnet ServerIP 11211 print current Memcache Server Status: stats print current Memcache Server Items (record) Statistics: stats items prints the statistical information of the current Memcache server Slab (partition) and Chunk (Block): stats slabs prints the KEY list in the specified Slab (which can be used to traverse items, but is inefficient. Use it with caution !): Stats cachedump SlabId Limit_num. Result: ITEM KeyName [ValueByteLength B; LastAccessTime s]. It is worth noting that the LastAccessTime does not record the expiration time, but the last get time, and will not automatically extend the expiry (expiration time) after the get ). Add new record: add KeyName 0 0 ValueByteLength [Press enter] ValueContent delete record: delete KeyName add or update record: set KeyName 0 0 ValueByteLength [Press enter] ValueContent update record: replace KeyName 0 0 ValueByteLength [Press enter] ValueContent value: get key reference: http://www.cnblogs.com/lost-1987/articles/3069460.htmlhttp://wenku.baidu.com/view/e30db586ec3a87c24028c401.html You can also visually monitor the running status of Memcached. http://livebookmark.net/journal/2008/05/21/memcachephp-stats-like-apcphp/ Find an ImagePath string under HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ memcached Server, which is exactly the string of the Service Execution path. Double-click the string, add-l 192.168.1.135-m 45-p 12345 (access ip: 192.168.1.135 using 45 m memory, 12345 as the port) to the end, and then start the service.View Code
V. Application
1. Download The. NET version memcached client API component and place it in the self-built Lib folder.
2. Add these dll references (Commons. dll, ICSharpCode. SharpZipLib. dll, log4net. dll)
3. The code for encapsulating MemcacheHelper is as follows:
Public class MemcacheHelper {private static readonly MemcachedClient mc = null; static MemcacheHelper () // The static constructor runs only once {string [] serverlist = {"127.0.0.1: 11211 ", "10.0.0.132: 11211"}; // IP address and port number of the Memcache server. Use a local machine to test the IP address and port number. (Multiple server information can be stored. Invalid servers are automatically ignored. The specific server to which the server is stored will be automatically selected based on the internal hash algorithm. We do not need to consider it .) // Initialize the SockIOPool pool = SockIOPool. getInstance (); pool. setServers (serverlist); pool. initConnections = 3; pool. minConnections = 3; pool. maxConnections = 5; pool. socketConnectTimeout = 1000; pool. socketTimeout = 3000; pool. maintenanceSleep = 30; pool. failover = true; pool. nagle = false; pool. initialize (); // obtain the client instance (which can be considered a telnet or socket Client) MemcachedClient mc = new MemcachedClient (); mc. enableCompression = false ;} /// <summary> /// store data to Memcache /// </summary> /// <param name = "key"> </param> // <param name = "value"> </param> public static void Set (string key, object value) {mc. set (key, value);} public static void Set (string key, object value, DateTime time) {mc. set (key, value, time ); // absolute expiration time} // <summary> // obtain data in Memcache /// </summary> /// <param name = "key"> </param >/// <returns> </returns> public static object Get (string key) {return mc. get (key );} /// <summary> /// Delete /// </summary> /// <param name = "key"> </param> /// <returns> </ returns> public static bool Delete (string key) {if (mc. keyExists (key) {return mc. delete (key) ;}return false ;}}
Call:
MemcacheHelper.Set("test","myValue");
6. Download source code: Click to download>
Test: