Esempio n. 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()));
                }
            }
        }
Esempio n. 2
0
 /// <summary>
 /// 移除单体
 /// </summary>
 /// <param name="key"></param>
 public static bool Remove(string key)
 {
     using (NewLife.Caching.Redis redis = GetClient())
     {
         return(redis.Remove(key.Split(',')) > 0);
     }
 }
Esempio n. 3
0
 /// <summary>
 /// 获取SortedSet的长度
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public static int GetCacheCount()
 {
     using (NewLife.Caching.Redis redis = GetClient())
     {
         return(redis.Count);
     }
 }
Esempio n. 4
0
 /// <summary>
 /// 累减,原子操作
 /// </summary>
 /// <param name="key"></param>
 /// <param name="datetime"></param>
 public static long Decrement(string key, long value)
 {
     using (NewLife.Caching.Redis redis = GetClient())
     {
         return(redis.Decrement(key, value));
     }
 }
Esempio n. 5
0
 /// <summary>
 /// 清空所有缓存
 /// </summary>
 public static void RemoveAll()
 {
     using (NewLife.Caching.Redis redis = GetClient())
     {
         redis.Clear();
     }
 }
Esempio n. 6
0
 /// <summary>
 /// 设置缓存过期
 /// </summary>
 /// <param name="key"></param>
 /// <param name="datetime"></param>
 public static void SetExpire(string key, DateTime datetime)
 {
     using (NewLife.Caching.Redis redis = GetClient())
     {
         TimeSpan ts = datetime - DateTime.Now;
         redis.SetExpire(key, ts);
     }
 }
Esempio n. 7
0
 /// <summary>
 /// 累加,原子操作
 /// </summary>
 /// <param name="key"></param>
 /// <param name="datetime"></param>
 public static long Increment(string key, long value)
 {
     try
     {
         using (NewLife.Caching.Redis redis = GetClient())
         {
             return(redis.Increment(key, value));
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Esempio n. 8
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;
     }
 }
Esempio n. 9
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);
     }
 }
Esempio n. 10
0
 /// <summary>
 /// 创建链接池管理对象
 /// </summary>
 private static void CreateManager()
 {
     try
     {
         FullRedis.Register();
         ThreadPool.SetMinThreads(100, 100);
         prcm        = new NewLife.Caching.FullRedis(redisConfigInfo.ReadServerList, redisConfigInfo.RedisDbPassword, redisConfigInfo.RedisDbInt);
         prcm.Expire = 0;//默认缓存时间 0 表示不过期
         //prcm.Log = XTrace.Log;// 调试日志。正式使用时注释掉
         prcm.StartPipeline();
         prcm.AutoPipeline = 10;
         prcm.Timeout      = 3000;
         prcm.Retry        = 5;
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Esempio n. 11
0
        /// <summary>
        /// 获取单体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T Get <T>(string key) where T : class
        {
            try
            {
                using (NewLife.Caching.Redis redis = GetClient())
                {
                    return(redis.Get <T>(key));
                }
            }
            catch (Exception e)
            {
                retryflag++;

                if (5 < retryflag)
                {
                    throw e;
                }

                using (NewLife.Caching.Redis redis = GetClient(1))
                {
                    return(redis.Get <T>(key));
                }
            }
        }
Esempio n. 12
0
 private void UpdateStatus()
 {
     // 更新状态,永不过期
     _Status.LastActive = DateTime.Now;
     Redis.Set(_StatusKey, _Status);
 }
Esempio n. 13
0
 /// <summary>实例化队列</summary>
 /// <param name="redis"></param>
 /// <param name="key"></param>
 public RedisQueue(Redis redis, String key) : base(redis, key)
 {
     TraceName = key;
 }
Esempio n. 14
0
        /// <summary>测试</summary>
        public static void Test()
        {
            //var rds = new RedisClient
            //{
            //    Log = XTrace.Log,
            //    Server = new NetUri("tcp://127.0.0.1:6379"),
            //};
            var rds = Redis.Create("127.0.0.1:6379", 4);

            rds.Password = "";
            rds.Log      = XTrace.Log;
            (rds.Pool as ObjectPool <RedisClient>).Log = XTrace.Log;

            //rds.Bench();
            //return;

            var rc = rds.Pool.Get();

            var f = rc.Select(4);
            //Console.WriteLine(f);

            var p = rc.Ping();
            //Console.WriteLine(p);

            var vs = rds.GetAll <String>(new[] { "num", "dd", "dt" });

            Console.WriteLine(vs);

            var num = Rand.Next(10243);

            rds.Set("num", num);
            var num2 = rds.Get <Int16>("num");
            //Console.WriteLine("{0} => {1}", num, num2);

            var d1 = (Double)Rand.Next(10243) / 100;

            rds.Set("dd", d1);
            var d2 = rds.Get <Double>("dd");
            //Console.WriteLine("{0} => {1}", d1, d2);

            var dt = DateTime.Now;

            rds.Set("dt", dt);
            var dt2 = rds.Get <DateTime>("dt");
            //Console.WriteLine("{0} => {1}", dt, dt2);

            var v = Rand.NextString(7);

            rds.Set("name", v);
            v = rds.Get <String>("name");
            //Console.WriteLine(v);

            var buf1 = Rand.NextBytes(35);

            rds.Set("bs", buf1);
            var buf2 = rds.Get <Byte[]>("bs");

            Console.WriteLine(buf1.ToHex());
            Console.WriteLine(buf2.ToHex());

            //var inf = rc.GetInfo();
            //foreach (var item in inf)
            //{
            //    Console.WriteLine("{0}\t{1}", item.Key, item.Value);
            //}

            // 加锁测试
            var sw = Stopwatch.StartNew();

            Parallel.For(0, 5, k =>
            {
                var key = "num";
                using (var rlock = rds.AcquireLock(key, 3000))
                {
                    var vnum = rds.Get <Int32>(key);
                    vnum++;
                    Thread.Sleep(Rand.Next(100, 1000));
                    rds.Set(key, vnum);
                }
            });
            sw.Stop();

            // 加锁结果检查
            var rnum = rds.Get <Int32>("num");

            Console.WriteLine("加锁累加结果:{0} 匹配:{1} 耗时:{2:n0}ms", rnum, rnum == num2 + 5, sw.ElapsedMilliseconds);

            //rds.Quit();
            rds.Dispose();
        }
Esempio n. 15
0
 /// <summary>实例化超级基数</summary>
 /// <param name="redis"></param>
 /// <param name="key"></param>
 public HyperLogLog(Redis redis, String key) : base(redis, key)
 {
 }
Esempio n. 16
0
 public RedisQueue(Redis rds, String key)
 {
     _Redis = rds;
     _Key   = key;
 }
Esempio n. 17
0
 /// <summary>实例化</summary>
 /// <param name="redis"></param>
 /// <param name="key"></param>
 public RedisSet(Redis redis, String key) : base(redis, key)
 {
 }
Esempio n. 18
0
 /// <summary>实例化队列</summary>
 /// <param name="redis"></param>
 /// <param name="key"></param>
 public RedisQueue(Redis redis, String key) : base(redis, key)
 {
 }
Esempio n. 19
0
 /// <summary>实例化</summary>
 /// <param name="redis"></param>
 /// <param name="server"></param>
 public RedisClient(Redis redis, NetUri server)
 {
     Host   = redis;
     Server = server;
 }
Esempio n. 20
0
 /// <summary>实例化延迟队列</summary>
 /// <param name="redis"></param>
 /// <param name="key"></param>
 public QueueBase(Redis redis, String key) : base(redis, key) => TraceName = key;