예제 #1
0
        /// <summary>
        /// 设置缓存,提供给服务控制器调用
        /// </summary>
        /// <param name="cacheName"></param>
        /// <param name="key"></param>
        /// <param name="value">Json字符串</param>
        public static void SetCache(string cacheName, string key, string value)
        {
            lock (_localCache)
            {
                if (_localCache.Contains(cacheName))
                {
                    CacheObject co = _localCache.GetData(cacheName) as CacheObject;

                    co.identify = DateTimeToTimestamp(DateTime.Now);
                    if (co.cacheValue.FindIndex(x => x.key.Equals(key)) == -1)
                    {
                        CacheData cd = new CacheData();
                        cd.timestamp = DateTimeToTimestamp(DateTime.Now);
                        cd.key       = key;
                        cd.value     = value;
                        co.cacheValue.Add(cd);
                    }
                    else
                    {
                        CacheData _cd = co.cacheValue.Find(x => x.key.Equals(key));
                        co.cacheValue.Remove(_cd);
                        CacheData cd = new CacheData();
                        cd.timestamp = DateTimeToTimestamp(DateTime.Now);
                        cd.key       = key;
                        cd.value     = value;
                        co.cacheValue.Add(cd);
                    }
                }
                else
                {
                    CacheObject co = new CacheObject();
                    co.ServerIdentify = WcfGlobal.Identify;
                    co.cachename      = cacheName;
                    co.identify       = DateTimeToTimestamp(DateTime.Now);
                    co.cacheValue     = new List <CacheData>();
                    CacheData cd = new CacheData();
                    cd.timestamp = DateTimeToTimestamp(DateTime.Now);
                    cd.key       = key;
                    cd.value     = value;
                    co.cacheValue.Add(cd);
                    _localCache.Add(cacheName, co);
                    if (_cacheNameList.Contains(cacheName) == false)
                    {
                        _cacheNameList.Add(cacheName);
                    }
                }

                PublishServiceManage.SendNotify("DistributedCache");//订阅服务发送通知
            }
        }
예제 #2
0
        /// <summary>
        /// 移除缓存,提供给服务控制器调用
        /// </summary>
        /// <param name="cacheName"></param>
        /// <param name="key"></param>
        public static void RemoveCache(string cacheName, string key)
        {
            lock (_localCache)
            {
                if (_localCache.Contains(cacheName))
                {
                    CacheObject co = _localCache.GetData(cacheName) as CacheObject;
                    if (co.cacheValue.FindIndex(x => x.key == key) > -1)
                    {
                        CacheData cd = co.cacheValue.Find(x => x.key == key);
                        cd.deleteflag = true;//缓存移除
                        cd.timestamp  = DateTimeToTimestamp(DateTime.Now);
                        co.identify   = DateTimeToTimestamp(DateTime.Now);

                        PublishServiceManage.SendNotify("DistributedCache");//订阅服务发送通知
                    }
                }
            }
        }