-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Memcached v0.1.0 重构缓存,添加MemcachedObjectCacheStrategy
- Loading branch information
Showing
5 changed files
with
219 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
210 changes: 210 additions & 0 deletions
210
....Cache/Senparc.Weixin.Cache.Memcached/ObjectCacheStrategy/MemcachedObjectCacheStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.