1、在服務器上註冊服務緩存
2、啓動服務:services.msc服務器
3、客戶端建立服務接口app
object Get(string key); List<string> GetKeys(); List<object> GetValues(); void Set(string key, object value); void Set(string key, object value, DateTime expiration); void Set(string key, object value, TimeSpan expiration); bool Exists(string key); void Remove(string key); void RemoveAll(); }
4、客戶端建立接口實現memcached
引入Memcached.ClientLibrary,版本爲1.0.0.0,還須要引入了log4net,版本爲1.2.10.0,其它版本會報異常。spa
public class MemcachedCache : ICache { private static readonly SockIOPool pool = SockIOPool.GetInstance("Project"); private static MemcachedClient client; private static ILog log = LogManager.GetLogger("memcached-log"); static MemcachedCache() { var config = ConfigurationManager.AppSettings["memcached-server"]; if (string.IsNullOrWhiteSpace(config)) { log.Info("沒有配置Memcached服務器IP!"); return; } try { string[] serverList = config.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); pool.SetServers(serverList); pool.Initialize(); client = new MemcachedClient(); client.PoolName = "Project"; client.EnableCompression = false; } catch (Exception e) { log.Info(string.Format("Memcached初始化異常:{0}", e.Message)); return; } log.Info("Memchached客戶端初始化完成!"); } public object Get(string key) { return client.Get(key); } public List<string> GetKeys() { var keys = new List<string>(); //IDictionaryEnumerator de = client.ke(); //while (de.MoveNext()) //{ // keys.Add(de.Key.ToString()); //} return keys; } public List<object> GetValues() { var values = new List<object>(); //IDictionaryEnumerator de = client.GetEnumerator(); //while (de.MoveNext()) //{ // values.Add(de.Value); //} return values; } public void Set(string key, object value) { client.Set(key, value); } public void Set(string key, object value, DateTime expiration) { client.Set(key, value, expiration); } public void Set(string key, object value, TimeSpan expiration) { client.Set(key, value, DateTime.Now.Add(expiration)); } public bool Exists(string key) { return client.KeyExists(key); } public void Remove(string key) { if (Exists(key)) { client.Delete(key); } } public void RemoveAll() { foreach (var item in GetKeys()) { Remove(item); } } }
5、客戶端配置code
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <appSettings> <!--服務器IP端口列表,這裏的端口號是memcached默認監控的端口號--> <add key="memcached-server" value="192.0.0.1:11211;192.0.0.2:11211;"/> </appSettings> <log4net> <root> <level value="ALL" /> <appender-ref ref="RollingLogFileAppender" /> </root> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="./log/aaa-" /> <param name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups" value="10" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyyMMdd".log"" /> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="[%d] [%-5t] [%-5p] -- %m%n" /> </layout> </appender> </log4net>
6、客戶端寫入獲取緩存代碼orm
//Autofac獲取實現server
IContainer container = ContainerInit.GetContainer(); var cache = container.Resolve<ICache>(); cache.Set("MyName", "James"); cache.Set("MyAge", 30); var value = cache.Get("MyAge");