Skip to content

Commit

Permalink
Memcached v0.1.0 重构缓存,添加MemcachedObjectCacheStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreySu committed Oct 24, 2016
1 parent 7739157 commit 238f2f2
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,15 @@

namespace Senparc.Weixin.Cache.Memcached
{
public class MemcachedContainerStrategy : BaseCacheStrategy, IContainerCacheStrategy
public class MemcachedContainerStrategy : MemcachedObjectCacheStrategy, IContainerCacheStrategy
{
internal MemcachedClient _cache;
private MemcachedClientConfiguration _config;
private static Dictionary<string, int> _serverlist;// = SiteConfig.MemcachedAddresss; TODO:全局注册配置

/// <summary>
/// 注册列表
/// </summary>
/// <param name="serverlist">Key:服务器地址(通常为IP),Value:端口</param>
public static void RegisterServerList(Dictionary<string, int> serverlist)
{
_serverlist = serverlist;
}

#region 单例

/// <summary>
/// LocalCacheStrategy的构造函数
/// </summary>
MemcachedContainerStrategy()
{
_config = GetMemcachedClientConfiguration();
_cache = new MemcachedClient(_config);
}

//静态LocalCacheStrategy
Expand All @@ -78,75 +63,6 @@ static Nested()

#endregion

#region 配置

private static MemcachedClientConfiguration GetMemcachedClientConfiguration()
{
//每次都要新建
var config = new MemcachedClientConfiguration();
foreach (var server in _serverlist)
{
config.Servers.Add(new IPEndPoint(IPAddress.Parse(server.Key), server.Value));
}
config.Protocol = MemcachedProtocol.Binary;

return config;
}

static MemcachedContainerStrategy()
{
// //初始化memcache服务器池
//SockIOPool pool = SockIOPool.GetInstance();
////设置Memcache池连接点服务器端。
//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();

//cache = new MemcachedClient();
//cache.EnableCompression = false;
try
{
//config.Authentication.Type = typeof(PlainTextAuthenticator);
//config.Authentication.Parameters["userName"] = "username";
//config.Authentication.Parameters["password"] = "password";
//config.Authentication.Parameters["zone"] = "zone";//domain? ——Jeffrey 2015.10.20
DateTime dt1 = DateTime.Now;
var config = GetMemcachedClientConfiguration();
var cache = new MemcachedClient(config);

var testKey = Guid.NewGuid().ToString();
var testValue = Guid.NewGuid().ToString();
cache.Store(StoreMode.Set, testKey, testValue);
var storeValue = cache.Get(testKey);
if (storeValue as string != testValue)
{
throw new Exception("MemcachedStrategy失效,没有计入缓存!");
}
cache.Remove(testKey);
DateTime dt2 = DateTime.Now;

WeixinTrace.Log(string.Format("MemcachedStrategy正常启用,启动及测试耗时:{0}ms", (dt2 - dt1).TotalMilliseconds));
}
catch (Exception ex)
{
//TODO:记录是同日志
WeixinTrace.Log(string.Format("MemcachedStrategy静态构造函数异常:{0}", ex.Message));
}
}

#endregion

#region IContainerCacheStrategy 成员

Expand All @@ -157,24 +73,12 @@ public void InsertToCache(string key, IBaseContainerBag value)//TODO:添加Timeo
return;
}

var cacheKey = GetFinalKey(key);

//TODO:加了绝对过期时间就会立即失效(再次获取后为null),memcache低版本的bug
_cache.Store(StoreMode.Set, cacheKey, value, DateTime.Now.AddDays(1));

#if DEBUG
value = _cache.Get(cacheKey) as IBaseContainerBag;
#endif
base.InsertToCache(key, value);
}

public void RemoveFromCache(string key, bool isFullKey = false)
{
if (string.IsNullOrEmpty(key))
{
return;
}
var cacheKey = GetFinalKey(key, isFullKey);
_cache.Remove(cacheKey);
base.RemoveFromCache(key, isFullKey);
}

public IBaseContainerBag Get(string key, bool isFullKey = false)
Expand All @@ -200,13 +104,7 @@ public IDictionary<string, IBaseContainerBag> GetAll()

public bool CheckExisted(string key, bool isFullKey = false)
{
var cacheKey = GetFinalKey(key, isFullKey);
object value;
if (_cache.TryGet(cacheKey, out value))
{
return true;
}
return false;
return base.CheckExisted(key, isFullKey);
}

public long GetCount()
Expand All @@ -216,8 +114,7 @@ public long GetCount()

public void Update(string key, IBaseContainerBag value, bool isFullKey = false)
{
var cacheKey = GetFinalKey(key, isFullKey);
_cache.Store(StoreMode.Set, cacheKey, value, DateTime.Now.AddDays(1));
base.Update(key, value, isFullKey);
}

public void UpdateContainerBag(string key, IBaseContainerBag containerBag, bool isFullKey = false)
Expand All @@ -232,9 +129,5 @@ public void UpdateContainerBag(string key, IBaseContainerBag containerBag, bool

#endregion

public override ICacheLock BeginCacheLock(string resourceName, string key, int retryCount = 0, TimeSpan retryDelay = new TimeSpan())
{
return new MemcachedCacheLock(this, resourceName, key, retryCount, retryDelay).LockNow();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace Senparc.Weixin.Cache.Memcached
{
public class MemcachedCacheLock : BaseCacheLock
{
private MemcachedContainerStrategy _mamcachedStrategy;
public MemcachedCacheLock(MemcachedContainerStrategy strategy, string resourceName, string key, int retryCount, TimeSpan retryDelay)
private MemcachedObjectCacheStrategy _mamcachedStrategy;
public MemcachedCacheLock(MemcachedObjectCacheStrategy strategy, string resourceName, string key, int retryCount, TimeSpan retryDelay)
: base(strategy, resourceName, key, retryCount, retryDelay)
{
_mamcachedStrategy = strategy;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;

namespace Senparc.Weixin.Cache.Memcached
{
public class MemcachedObjectCacheStrategy : BaseCacheStrategy, IObjectCacheStrategy
{
internal MemcachedClient _cache;
private MemcachedClientConfiguration _config;
private static Dictionary<string, int> _serverlist;// = SiteConfig.MemcachedAddresss; TODO:全局注册配置

/// <summary>
/// 注册列表
/// </summary>
/// <param name="serverlist">Key:服务器地址(通常为IP),Value:端口</param>
public static void RegisterServerList(Dictionary<string, int> serverlist)
{
_serverlist = serverlist;
}

#region 单例

/// <summary>
/// LocalCacheStrategy的构造函数
/// </summary>
public MemcachedObjectCacheStrategy()
{
_config = GetMemcachedClientConfiguration();
_cache = new MemcachedClient(_config);
}

//静态LocalCacheStrategy
public static IObjectCacheStrategy Instance
{
get
{
return Nested.instance;//返回Nested类中的静态成员instance
}
}

class Nested
{
static Nested()
{
}
//将instance设为一个初始化的LocalCacheStrategy新实例
internal static readonly MemcachedObjectCacheStrategy instance = new MemcachedObjectCacheStrategy();
}

#endregion

#region 配置

private static MemcachedClientConfiguration GetMemcachedClientConfiguration()
{
//每次都要新建
var config = new MemcachedClientConfiguration();
foreach (var server in _serverlist)
{
config.Servers.Add(new IPEndPoint(IPAddress.Parse(server.Key), server.Value));
}
config.Protocol = MemcachedProtocol.Binary;

return config;
}

static MemcachedObjectCacheStrategy()
{
// //初始化memcache服务器池
//SockIOPool pool = SockIOPool.GetInstance();
////设置Memcache池连接点服务器端。
//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();

//cache = new MemcachedClient();
//cache.EnableCompression = false;
try
{
//config.Authentication.Type = typeof(PlainTextAuthenticator);
//config.Authentication.Parameters["userName"] = "username";
//config.Authentication.Parameters["password"] = "password";
//config.Authentication.Parameters["zone"] = "zone";//domain? ——Jeffrey 2015.10.20
DateTime dt1 = DateTime.Now;
var config = GetMemcachedClientConfiguration();
var cache = new MemcachedClient(config);

var testKey = Guid.NewGuid().ToString();
var testValue = Guid.NewGuid().ToString();
cache.Store(StoreMode.Set, testKey, testValue);
var storeValue = cache.Get(testKey);
if (storeValue as string != testValue)
{
throw new Exception("MemcachedStrategy失效,没有计入缓存!");
}
cache.Remove(testKey);
DateTime dt2 = DateTime.Now;

WeixinTrace.Log(string.Format("MemcachedStrategy正常启用,启动及测试耗时:{0}ms", (dt2 - dt1).TotalMilliseconds));
}
catch (Exception ex)
{
//TODO:记录是同日志
WeixinTrace.Log(string.Format("MemcachedStrategy静态构造函数异常:{0}", ex.Message));
}
}

#endregion


#region IContainerCacheStrategy 成员

public IContainerCacheStrategy ContainerCacheStrategy
{
get { return MemcachedContainerStrategy.Instance; }
}

public void InsertToCache(string key, object value)//TODO:添加Timeout参数
{
if (string.IsNullOrEmpty(key) || value == null)
{
return;
}

var cacheKey = GetFinalKey(key);

//TODO:加了绝对过期时间就会立即失效(再次获取后为null),memcache低版本的bug
_cache.Store(StoreMode.Set, cacheKey, value, DateTime.Now.AddDays(1));

#if DEBUG
value = _cache.Get(cacheKey) as IBaseContainerBag;
#endif
}

public void RemoveFromCache(string key, bool isFullKey = false)
{
if (string.IsNullOrEmpty(key))
{
return;
}
var cacheKey = GetFinalKey(key, isFullKey);
_cache.Remove(cacheKey);
}

public object Get(string key, bool isFullKey = false)
{
if (string.IsNullOrEmpty(key))
{
return null;
}

var cacheKey = GetFinalKey(key, isFullKey);
return _cache.Get<object>(cacheKey);
}

public IDictionary<string, object> GetAll()
{
throw new NotImplementedException();
}

public bool CheckExisted(string key, bool isFullKey = false)
{
var cacheKey = GetFinalKey(key, isFullKey);
object value;
if (_cache.TryGet(cacheKey, out value))
{
return true;
}
return false;
}

public long GetCount()
{
throw new NotImplementedException();//TODO:需要定义二级缓存键,从池中获取
}

public void Update(string key, object value, bool isFullKey = false)
{
var cacheKey = GetFinalKey(key, isFullKey);
_cache.Store(StoreMode.Set, cacheKey, value, DateTime.Now.AddDays(1));
}

#endregion


public override ICacheLock BeginCacheLock(string resourceName, string key, int retryCount = 0, TimeSpan retryDelay = new TimeSpan())
{
return new MemcachedCacheLock(this, resourceName, key, retryCount, retryDelay).LockNow();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<ItemGroup>
<Compile Include="ContainerCacheStrategy\MemcachedContainerStrategy.cs" />
<Compile Include="MamcachedCacheLock.cs" />
<Compile Include="ObjectCacheStrategy\MemcachedObjectCacheStrategy.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Loading

0 comments on commit 238f2f2

Please sign in to comment.