コード例 #1
0
        /// <summary>
        /// 获取扩展方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="depFile"></param>
        /// <returns></returns>
        public T Get <T>(string key, string depFile)
        {
            key = CacheUtils.getPrefixKey(key);
            string timeKey = key + "_time";

            if (db.KeyExists(timeKey) && db.KeyExists(key))
            {
                DateTime obj_time  = Get <DateTime>(timeKey);
                T        obj_cache = Get <T>(key);
                if (File.Exists(depFile))
                {
                    FileInfo fi = new FileInfo(depFile);
                    if (obj_time != fi.LastWriteTime)
                    {
                        Delete(key);
                        Delete(timeKey);
                        return(default(T));
                    }
                    else
                    {
                        return(obj_cache);
                    }
                }
                else
                {
                    throw new Exception("文件(" + depFile + ")不存在!");
                }
            }
            else
            {
                return(default(T));
            }
        }
コード例 #2
0
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="T">类型(对象必须可序列化,否则可以作为object类型取出再类型转换,不然会报错)</typeparam>
        /// <param name="key">缓存key</param>
        /// <returns></returns>
        public T Get <T>(string key)
        {
            key = CacheUtils.getPrefixKey(key);
            RedisValue value = db.StringGet(key);

            return(CacheUtils.ConvertObj <T>(value));
        }
コード例 #3
0
        /// <summary>
        /// 获取key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string Get(string key)
        {
            key = CacheUtils.getPrefixKey(key);


            return(db.StringGet(key));
        }
コード例 #4
0
 /// <summary>
 /// 删除多个缓存
 /// </summary>
 /// <param name="keys">缓存key数组</param>
 /// <returns></returns>
 public int Delete(string[] keys)
 {
     if (keys == null || keys.Length == 0)
     {
         return(0);
     }
     return(keys.Count(key => db.KeyDelete(CacheUtils.getPrefixKey(key))));
 }
コード例 #5
0
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="depFile"></param>
        /// <returns></returns>
        public bool Set <T>(string key, T value, string depFile)
        {
            key = CacheUtils.getPrefixKey(key);
            bool ret1 = db.StringSet(key, CacheUtils.ConvertJson(value));

            if (ret1 && File.Exists(depFile))
            {
                FileInfo fi            = new FileInfo(depFile);
                DateTime lastWriteTime = fi.LastWriteTime;
                return(db.StringSet(key + "_time", CacheUtils.ConvertJson(lastWriteTime)));
            }
            return(false);
        }
コード例 #6
0
        /// <summary>
        /// 获取keys
        /// </summary>
        /// <returns></returns>
        public List <string> GetKeys()
        {
            var           endpoints = _connectionMultiplexer.GetEndPoints();
            List <string> keyList   = new List <string>();

            foreach (var ep in endpoints)
            {
                var    server = _connectionMultiplexer.GetServer(ep);
                String reg    = CacheUtils.getPrefixKey("*");
                var    keys   = server.Keys(redisNum, reg);
                foreach (var item in keys)
                {
                    keyList.Add((string)item);
                }
            }
            return(keyList);
            //throw new NotImplementedException();
        }
コード例 #7
0
 /// <summary>
 /// 删除缓存
 /// </summary>
 /// <param name="key">缓存key</param>
 /// <returns></returns>
 public int Delete(string key)
 {
     key = CacheUtils.getPrefixKey(key);
     return(db.KeyDelete(key) ? 1 : 0);
 }
コード例 #8
0
 /// <summary>
 /// 写入缓存,设置过期秒数
 /// </summary>
 /// <typeparam name="T">类型</typeparam>
 /// <param name="key">缓存key</param>
 /// <param name="value">缓存值</param>
 /// <param name="expiresSecond">过期秒数</param>
 /// <returns>返回值,表示:是否写入成功</returns>
 public bool Set <T>(string key, T value, int expiresSecond)
 {
     key = CacheUtils.getPrefixKey(key);
     return(db.StringSet(key, CacheUtils.ConvertJson(value), DateTime.Now.AddSeconds(expiresSecond) - new DateTime()));
 }
コード例 #9
0
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="timeSpan"></param>
        /// <returns></returns>
        public bool Set <T>(string key, T value, TimeSpan timeSpan)
        {
            key = CacheUtils.getPrefixKey(key);

            return(db.StringSet(key, CacheUtils.ConvertJson(value), timeSpan));
        }
コード例 #10
0
 /// <summary>
 /// 写入缓存,设置过期时间点
 /// </summary>
 /// <typeparam name="T">类型</typeparam>
 /// <param name="key">缓存key</param>
 /// <param name="value">缓存值</param>
 /// <param name="expiresAt">过期时间点</param>
 /// <returns>返回值,表示:是否写入成功</returns>
 public bool Set <T>(string key, T value, DateTime expiresAt)
 {
     key = CacheUtils.getPrefixKey(key);
     return(db.StringSet(key, CacheUtils.ConvertJson(value), expiresAt - new DateTime()));
 }