Exemple #1
0
        /// <summary>
        /// 设置单体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="t"></param>
        /// <param name="expire">  过期时间,秒。小于0时采用默认缓存时间NewLife.Caching.Cache.Expire,这里用的是 过期时间-当前时间,剩余的秒数</param>
        /// <returns></returns>
        public static bool Set <T>(string key, T t, DateTime expireTime)
        {
            try
            {
                using (NewLife.Caching.Redis redis = GetClient())
                {
                    TimeSpan ts = expireTime - DateTime.Now;
                    return(redis.Set <T>(key, t, ts.TotalSeconds.ToInt()));
                }
            }
            catch (Exception e)
            {
                retryflag++;

                if (5 < retryflag)
                {
                    throw e;
                }

                using (NewLife.Caching.Redis redis = GetClient(1))
                {
                    TimeSpan ts = expireTime - DateTime.Now;
                    return(redis.Set <T>(key, t, ts.TotalSeconds.ToInt()));
                }
            }
        }
Exemple #2
0
 /// <summary>
 /// 设置单体
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="t"></param>
 /// <param name="timeSpan"></param>
 /// <returns></returns>
 public static bool Set <T>(string key, T t)
 {
     try
     {
         using (NewLife.Caching.Redis redis = GetClient())
         {
             return(redis.Set <T>(key, t));
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Exemple #3
0
 /// <summary>
 /// 是否连接成功
 /// </summary>
 /// <returns></returns>
 public static bool IsConnectSuscess()
 {
     try
     {
         using (NewLife.Caching.Redis redis = GetClient())
         {
             redis.Set <string>("TestRedis", "OK");
             var redisvalue = redis.Get <string>("TestRedis");
         }
         return(true);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
 private void UpdateStatus()
 {
     // 更新状态,永不过期
     _Status.LastActive = DateTime.Now;
     Redis.Set(_StatusKey, _Status);
 }