/// <summary> /// 获取本地缓存的标识 /// </summary> public static CacheIdentify GetCacheIdentify(string cacheName) { CacheIdentify cacheId = new CacheIdentify(); if (_localCache.Contains(cacheName)) { CacheObject co = _localCache.GetData(cacheName) as CacheObject; cacheId.ServerIdentify = co.ServerIdentify; cacheId.cachename = co.cachename; cacheId.identify = co.identify; cacheId.keytimestamps = new Dictionary <string, double>(); foreach (var cd in co.cacheValue) { cacheId.keytimestamps.Add(cd.key, cd.timestamp); } } return(cacheId); }
/// <summary> /// 比较后不同的标识 /// </summary> /// <returns></returns> public static CacheIdentify CompareCache(CacheIdentify _cacheId) { CacheIdentify cacheId = new CacheIdentify(); cacheId.ServerIdentify = _cacheId.ServerIdentify; cacheId.cachename = _cacheId.cachename; cacheId.identify = _cacheId.identify; cacheId.keytimestamps = new Dictionary <string, double>(); //自己跟自己比较返回空 if (_cacheId.ServerIdentify == WcfServerManage.Identify) { return(cacheId); } if (_localCache.Contains(_cacheId.cachename)) { CacheObject co = _localCache.GetData(_cacheId.cachename) as CacheObject; if (_cacheId.identify != co.identify) { //循环判断待同步的新增和修改,本地时间搓小于远程时间搓就修改 foreach (var t in _cacheId.keytimestamps) { //新增的 if (co.cacheValue.FindIndex(x => (x.key == t.Key)) == -1) { cacheId.keytimestamps.Add(t); } //修改的 if (co.cacheValue.FindIndex(x => (x.key == t.Key && t.Value > x.timestamp)) > -1) { cacheId.keytimestamps.Add(t); } } //循环判断本地的删除,本地时间搓小于远程identify的就会删除 //删除是打删除标记,所以不存在移除,都进入修改列表 } return(cacheId); } else { return(_cacheId); } }
/// <summary> /// 同步指定缓存给上级中间件 /// </summary> /// <param name="cacheName"></param> public static void SyncCache(string cacheName) { if (WcfServerManage.superclient != null) { CacheIdentify cacheId = GetCacheIdentify(cacheName); if (cacheId.keytimestamps != null && cacheId.keytimestamps.Count > 0) { cacheId = WcfServerManage.superclient.DistributedCacheSyncIdentify(cacheId); } CacheObject cache = GetStayLocalCache(cacheId); if (cache.cacheValue != null && cache.cacheValue.Count > 0) { //缓存同步到上级中间件 WcfServerManage.superclient.DistributedCacheSync(cache); //缓存还要同步到所有下级中间件 DistributedCacheManage.SyncCache(cache); } } }
/// <summary> /// 获取待同步的缓存 /// </summary> /// <returns></returns> public static CacheObject GetStayLocalCache(CacheIdentify identify) { CacheObject _co = new CacheObject(); if (_localCache.Contains(identify.cachename)) { CacheObject co = _localCache.GetData(identify.cachename) as CacheObject; _co.ServerIdentify = co.ServerIdentify; _co.cachename = co.cachename; _co.identify = co.identify; _co.cacheValue = new List <CacheData>(); foreach (var kt in identify.keytimestamps) { CacheData cd = co.cacheValue.Find(x => x.timestamp == kt.Value); if (cd != null) { _co.cacheValue.Add(cd); } } } return(_co); }
public ServerController.CacheIdentify DistributedCacheSyncIdentify(ServerController.CacheIdentify cacheId) { return(EFWCoreLib.WcfFrame.ServerController.DistributedCacheManage.CompareCache(cacheId)); }