Пример #1
0
        public string LockStringGet(ScdCacheKey cacheKey)
        {
            string lockName  = "lockKey_" + cacheKey.ToString();
            string lockToken = Guid.NewGuid().ToString();

            if (AcquireLock(lockName, lockToken))
            {
                try
                {
                    var rValue = _db.StringGet(cacheKey.ToString());
                    if (!rValue.HasValue)
                    {
                        return(null);
                    }
                    var result = Deserialize <string>(rValue);
                    return(result);
                }
                finally
                {
                    _db.LockRelease(lockName, lockToken);
                }
            }
            else
            {
                return(null);
            }
        }
Пример #2
0
        public void Set(ScdCacheKey key, object data, double cacheTime)
        {
            // 此处写入文件日志计算Redis的key以及操作对应的次数
            var entryBytes = data == null ? new byte[0] : Serialize(data);

            XFileLoger.WriteLogEx("RedisNullCacheSet_" + key, CommonHelper.ConvertBytes(entryBytes.Length));
            var expiresIn = TimeSpan.FromMinutes(cacheTime);

            _db.StringSet(key.ToString(), entryBytes, expiresIn);
        }
Пример #3
0
        public void Remove(ScdCacheKey key)
        {
            var items = GetItems();

            if (items == null)
            {
                return;
            }

            items.Remove(key.ToString());
        }
Пример #4
0
        public bool IsSet(ScdCacheKey key)
        {
            var items = GetItems();

            if (items == null)
            {
                return(false);
            }

            return(items[key.ToString()] != null);
        }
Пример #5
0
        public TResult Get <TResult>(ScdCacheKey key)
        {
            var items = GetItems();

            if (items == null)
            {
                return(default(TResult));
            }

            return((TResult)items[key.ToString()]);
        }
Пример #6
0
 public void AddCache(ScdCacheKey key, object value, string dependencyFile)
 {
     if (!IsSet(key))
     {
         CacheItemPolicy policy   = new CacheItemPolicy();
         List <string>   filePath = new List <string>();
         filePath.Add(dependencyFile);
         policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePath));
         Cache.Set(key.ToString(), value, policy);
     }
 }
Пример #7
0
        public void Set(ScdCacheKey key, object data, double cacheTime)
        {
            if (data == null)
            {
                return;
            }

            var policy = new CacheItemPolicy();

            policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);
            Cache.Add(new CacheItem(key.ToString(), data), policy);
        }
Пример #8
0
        /// <summary>
        /// Get a cached item. If it's not in the cache yet, then load and cache it
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="key">Cache key</param>
        /// <param name="cacheTime">Cache time in minutes (0 - do not cache)</param>
        /// <param name="acquire">Function to load item if it's not in the cache yet</param>
        /// <returns>Cached item</returns>
        public static T Get <T>(this ICacheManager cacheManager, ScdCacheKey key, double cacheTime, Func <T> acquire)
        {
            if (cacheManager.IsSet(key))
            {
                return(cacheManager.Get <T>(key));
            }

            var result = acquire();

            if (cacheTime > 0)
            {
                cacheManager.Set(key, result, cacheTime);
            }
            return(result);
        }
Пример #9
0
        public TResult Get <TResult>(ScdCacheKey key)
        {
            var rValue = _db.StringGet(key.ToString());

            if (!rValue.HasValue)
            {
                // 此处写入文件日志计算Redis的key以及操作对应的次数
                XFileLoger.WriteLogEx("RedisCacheGet_" + key, "NoData");
                return(default(TResult));
            }
            // 此处写入文件日志计算Redis的key以及操作对应的次数
            XFileLoger.WriteLogEx("RedisCacheGet_" + key, CommonHelper.ConvertBytes(((byte[])rValue).Length));
            var result = Deserialize <TResult>(rValue);

            return(result);
        }
Пример #10
0
        public void Set(ScdCacheKey key, object data, double cacheTime)
        {
            var items = GetItems();

            if (items == null)
            {
                return;
            }

            if (data != null)
            {
                if (items.Contains(key.ToString()))
                {
                    items[key.ToString()] = data;
                }
                else
                {
                    items.Add(key.ToString(), data);
                }
            }
        }
Пример #11
0
        public void LockStringSet(ScdCacheKey cacheKey, string vlaue)
        {
            string lockName  = "lockKey_" + cacheKey.ToString();
            string lockToken = Guid.NewGuid().ToString();

            if (AcquireLock(lockName, lockToken))
            {
                try
                {
                    var entryBytes = Serialize(vlaue);
                    var expiresIn  = TimeSpan.FromMinutes(10);

                    _db.StringSet(cacheKey.ToString(), entryBytes, expiresIn);
                }

                finally
                {
                    _db.LockRelease(lockName, lockToken);
                }
            }
        }
Пример #12
0
 public bool IsSet(ScdCacheKey key)
 {
     return(_db.KeyExists(key.ToString()));
 }
Пример #13
0
 public void Remove(ScdCacheKey key)
 {
     _db.KeyDelete(key.ToString());
 }
Пример #14
0
 public bool IsSet(ScdCacheKey key)
 {
     return(Cache.Contains(key.ToString()));
 }
Пример #15
0
 public void Remove(ScdCacheKey key)
 {
     Cache.Remove(key.ToString());
 }
Пример #16
0
        /// <summary>
        /// ScdCachekey
        /// </summary>
        /// <param name="keyFormat">keyFormart 如"site.user_{0}_{1}"</param>
        /// <param name="args">keyFormat的参数,可选</param>
        public static ScdCacheKey GetCacheKey(string keyFormat, params string[] args)
        {
            var cachekey = new ScdCacheKey(keyFormat, args);

            return(cachekey);
        }
Пример #17
0
 public TResult Get <TResult>(ScdCacheKey key)
 {
     return((TResult)Cache[key.ToString()]);
 }
Пример #18
0
 /// <summary>
 /// Get a cached item. If it's not in the cache yet, then load and cache it
 /// </summary>
 /// <typeparam name="T">Type</typeparam>
 /// <param name="cacheManager">Cache manager</param>
 /// <param name="key">Cache key</param>
 /// <param name="acquire">Function to load item if it's not in the cache yet</param>
 /// <returns>Cached item</returns>
 public static T Get <T>(this ICacheManager cacheManager, ScdCacheKey key, Func <T> acquire)
 {
     return(Get(cacheManager, key, 60, acquire));
 }