1. Configuration
<! -- Memcached configuration -->
<Add key = "MemcachedServerlist" value = "Wagner. 0.0.1: 11211"/>
<Add key = "InitConnections" value = "3"/>
<Add key = "MinConnections" value = "3"/>
<Add key = "MaxConnections" value = "5"/>
<Add key = "SocketConnectTimeout" value = "1000"/>
<Add key = "SocketTimeout" value = "3000"/>
<Add key = "MaintenanceSleep" value = "30"/>
<Add key = "Failover" value = "true"/>
<Add key = "Nagle" value = "false"/>
<Add key = "CacheTimeout" value = "10"/>
2. memcached management class
/*
* Memcache management class
* Author: domo
* Date: 2012-04-17
*/
# Region reference
Using System;
Using System. Configuration;
Using Memcached. ClientLibrary;
# Endregion
Namespace bases. cache
{
/// <Summary>
/// Memcache management class
/// </Summary>
Public class MemcacheManager
{
/// <Summary>
/// Define the Memcache Client
/// </Summary>
Internal MemcachedClient Mc = new MemcachedClient {EnableCompression = false };
/// <Summary>
/// Construct initialization parameters
/// </Summary>
Public MemcacheManager ()
{
Var serverlist = ConfigurationManager. receivettings ["MemcachedServerlist"]. Split (',');
Var initConnections = ConfigurationManager. deleettings ["InitConnections"];
Var minConnections = ConfigurationManager. receivettings ["MinConnections"];
Var maxConnections = ConfigurationManager. etettings ["MaxConnections"];
Var socketConnectTimeout = ConfigurationManager. etetout ["SocketConnectTimeout"];
Var socketTimeout = ConfigurationManager. etettimeout ["SocketTimeout"];
Var maintenanceSleep = ConfigurationManager. receivettings ["MaintenanceSleep"];
Var failover = ConfigurationManager. receivettings ["Failover"];
Var nagle = ConfigurationManager. receivettings ["Nagle"];
Var pool = SockIOPool. GetInstance ();
Pool. SetServers (serverlist );
Pool. InitConnections = Convert. ToInt32 (initConnections );
Pool. MinConnections = Convert. ToInt32 (minConnections );
Pool. MaxConnections = Convert. ToInt32 (maxConnections );
Pool. SocketConnectTimeout = Convert. ToInt32 (socketConnectTimeout );
Pool. SocketTimeout = Convert. ToInt32 (socketTimeout );
Pool. MaintenanceSleep = Convert. ToInt64 (maintenanceSleep );
Pool. Failover = Convert. ToBoolean (failover );
Pool. Nagle = Convert. ToBoolean (nagle );
Pool. Initialize ();
}
/// <Summary>
/// Add cache
/// </Summary>
/// <Param name = "key"> </param>
/// <Param name = "value"> </param>
Public void Add (string key, object value)
{
Var cacheTimeout = ConfigurationManager. deleettimeout ["CacheTimeout"];
If (! Mc. KeyExists (key ))
Mc. Set (key, value, DateTime. Now. AddMinutes (Convert. ToDouble (cacheTimeout )));
}
/// <Summary>
/// Remove a cache
/// </Summary>
/// <Param name = "key"> </param>
Public void Delete (string key)
{
Mc. Delete (key );
}
/// <Summary>
/// Obtain the value of a cache
/// </Summary>
/// <Param name = "key"> </param>
/// <Returns> </returns>
Public object GetValue (string key)
{
Return Mc. KeyExists (key )? Mc. Get (key): null;
}
/// <Summary>
/// Clear all the caches
/// </Summary>
Public void Flush ()
{
Mc. FlushAll ();
}
/// <Summary>
/// Disable sockets
/// </Summary>
Public void Shutdown ()
{
SockIOPool. GetInstance (). Shutdown ();
}
}
}
3. Use
MemcacheManager memcacheManager = new MemcacheManager ();
If (memcacheManager. GetValue ("test") = null)
MemcacheManager. Add ("test", "my value ");