public bool Keydeletes(string textdel)
 {
     if (_redis.ContainsKey(textdel))
     {
         _redis.Remove(textdel);
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #2
0
        private void button7_Click(object sender, EventArgs e)
        {
            if (client.ContainsKey("Increment") == true)
            {

                textBox1.Text += " "+client.IncrementValue("Increment");
            }
            else
            {
                client.Set("Increment",1);
            }

        }
Example #3
0
 /// <summary>
 /// 获取缓存值
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="nameSpace"></param>
 /// <returns></returns>
 public T GetValue <T>(string key, string nameSpace = "")
 {
     if (!string.IsNullOrEmpty(nameSpace))
     {
         key = $"n:{nameSpace},c:{key}";
     }
     if (redisClient.ContainsKey(key))
     {
         return(redisClient.Get <T>(key));
     }
     else
     {
         return(default(T));
     }
 }
Example #4
0
 public static bool Exists(string key)
 {
     using (var client = new RedisClient(Host))
     {
         return(client.ContainsKey(key));
     }
 }
Example #5
0
 public bool HasMessage()
 {
     using (var redisClient = new RedisClient())
     {
         return redisClient.ContainsKey(string.Format("{0}:{1}", _address.Machine, _address.Queue));
     }
 }
Example #6
0
 public static bool ContainsKey(string key)
 {
     using (RedisClient client = GetClient())
     {
         return(client.ContainsKey(key));
     }
 }
Example #7
0
 public bool Exists()
 {
     using (var redisClient = new RedisClient())
     {
         return(redisClient.ContainsKey("asa." + _cacheKey + ".all"));
     }
 }
Example #8
0
        private static void AddString(RedisClient client)
        {
            var timeOut = new TimeSpan(0, 0, 0, 30);

            client.Add("Test", "Learninghard", timeOut);
            while (true)
            {
                if (client.ContainsKey("Test"))
                {
                    Console.WriteLine("String Key: Test -Value: {0}, 当前时间: {1}", client.Get <string>("Test"), DateTime.Now);
                    Thread.Sleep(10000);
                }
                else
                {
                    Console.WriteLine("Value 已经过期了,当前时间:{0}", DateTime.Now);
                    break;
                }
            }
            var person = new Person()
            {
                Name = "Learninghard", Age = 26
            };

            client.Add("lh", person);
            var cachePerson = client.Get <Person>("lh");

            Console.WriteLine("Person's Name is : {0}, Age: {1}", cachePerson.Name, cachePerson.Age);
        }
Example #9
0
 public bool ContainsKey(string cachekey)
 {
     using (IRedisClient client = new RedisClient())
     {
         return(client.ContainsKey(cachekey));
     }
 }
Example #10
0
 /// <summary>
 /// 清除缓存
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public static object RemoveCache(string key)
 {
     key = CachePrefix + key;
     if (CacheType == "Redis")
     {
         //IRedisClient redis = Redis().GetClient();
         RedisClient redis = new RedisClient(RedisHost, RedisPort, RedisPassword);
         if (redis.ContainsKey(key))
         {
             bool IsRemove = redis.Remove(key);
             redis.Dispose();
             return(IsRemove);
         }
         else
         {
             redis.Dispose();
             return(null);
         }
     }
     else
     {
         if (HttpRuntime.Cache[key] != null)
         {
             return(HttpRuntime.Cache.Remove(key));
         }
         else
         {
             return(null);
         }
     }
 }
Example #11
0
        /// <summary>
        /// 读取缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetCache(string key)
        {
            key = CachePrefix + key;
            if (CacheType == "Redis")
            {
                //IRedisClient redis = Redis().GetClient();
                RedisClient redis = new RedisClient(RedisHost, RedisPort, RedisPassword);

                if (redis.ContainsKey(key))
                {
                    var    ser  = new ObjectSerializer();
                    object data = ser.Deserialize(redis.Get <byte[]>(key)) as object;
                    redis.Dispose();
                    return(data);
                }
                else
                {
                    redis.Dispose();
                    return(null);
                }
            }
            else
            {
                if (HttpRuntime.Cache[key] != null)
                {
                    return(HttpRuntime.Cache[key]);
                }
                else
                {
                    return(null);
                }
            }
        }
Example #12
0
 public bool IsSet(string key)
 {
     using (IRedisClient client = new RedisClient(_redisConfiguration))
     {
         return(client.ContainsKey(key));
     }
 }
Example #13
0
        private static void AddList(RedisClient client)
        {
            var timeOut = new TimeSpan(0, 0, 0, 30);

            if (!client.ContainsKey("FirstList"))
            {
                var list = new List <Person>();
                var per1 = new Person()
                {
                    Age  = 1,
                    Name = "a"
                };
                var per2 = new Person()
                {
                    Age  = 2,
                    Name = "b"
                };
                list.Add(per1);
                list.Add(per2);
                client.Set <List <Person> >("FirstList", list, timeOut);
            }
            var newlist = client.Get <List <Person> >("FirstList");

            foreach (var item in newlist)
            {
                var index = newlist.IndexOf(item);
                Console.WriteLine("集合FirstList{0}的值:Age:{1},Name:{2}", index, item.Age, item.Name);
            }
        }
Example #14
0
 public bool IsSet(string key)
 {
     using (IRedisClient client = new RedisClient(conf))
     {
         return(client.ContainsKey(key));
     }
 }
 /// <summary>
 /// 判断key值是否存在
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 #region 判断key是否存在
 public bool Judge(string key)
 {
     using (IRedisClient client = new RedisClient("127.0.0.1", 6379))
     {
         //判断是否存在
         return(client.ContainsKey(key));
     }
 }
Example #16
0
        // PushItemToList radi kao rpush
        // Ako ne postoji takav 'nameID' liste onda pravi novu listu
        // Ukoliko postoji takav 'nameID' liste onda ubacuje element u postojecu listu
        public bool AddUser(string userName, string pageID)
        {
            bool successfully = false;

            // Ako se salje kao jedan string
            if (!String.IsNullOrEmpty(userName) && !String.IsNullOrEmpty(pageID))
            {
                // Ako zadati kljuc ne postoji u bazi
                if (!redis.ContainsKey("userLista3"))
                {
                    // Mozda treba da se limitira lista
                    // Do work...
                }

                // konvertovanje pageID-a u byte array
                byte[] idToByte = Encoding.ASCII.GetBytes(pageID);

                // Ubacivanje liste u bazu, LPush ubacuje sa leve strane (kao stek, red)
                // Vraca broj elemenata u listi, zajedno sa dodatim elementom
                long elementsNumber = redis.LPush(userName, idToByte);

                successfully = true;
            }
            return(successfully);
        }
Example #17
0
        public bool IsContainsKey(string key)
        {
            bool result;

            using (RedisClient redisClient = this.GetRedisClient())
            {
                result = redisClient.ContainsKey(key);
            }
            return(result);
        }
Example #18
0
 public void Clear <T>(string cachekey)
 {
     if (ContainsKey(cachekey))
     {
         using (IRedisClient client = new RedisClient())
         {
             client.ContainsKey(cachekey);
         }
     }
 }
Example #19
0
        //添加注释代码
        //再来一行注释代码
        //再来最后一行注释
        static void Main(string[] args)
        {
            var client = new RedisClient("192.168.107.128", 6379);

            client.Set("location", "www.crsky.com");
            string location = Encoding.Default.GetString(client.Get("location"));

            Console.WriteLine(location);

            List <TestClass> testClassList = new List <TestClass>();

            testClassList.Add(new TestClass()
            {
                TestId = 1, TestName = "A"
            });
            testClassList.Add(new TestClass()
            {
                TestId = 2, TestName = "B"
            });
            testClassList.Add(new TestClass()
            {
                TestId = 3, TestName = "C"
            });
            client.Set <List <TestClass> >("testclassList", testClassList);
            var result = client.Get <List <TestClass> >("testclassList");

            Console.WriteLine(string.Format("{0}", "{1}", "{2}"), result.Count,
                              string.Join(",", testClassList.Select(x => x.TestId).ToList()),
                              string.Join(",", testClassList.Select(x => x.TestName).ToList()));
            if (client.ContainsKey("testclassList"))
            {
                Console.WriteLine("exist key testclassList");
            }
            client.Remove("testclassList");
            Console.WriteLine("remove key testclassList");
            if (!client.ContainsKey("testclassList"))
            {
                Console.WriteLine("not exist key testclassList");
            }


            Console.ReadLine();
        }
Example #20
0
        private static bool IsCacheKeyExistsRedis(string key)
        {
            bool result;

            using (var client = new RedisClient())
            {
                result = client.ContainsKey(key);
            }
            return(result);
        }
Example #21
0
 public async static Task <bool> ExistsAsync(string key)
 {
     return(await Common.ThreadHelper.StartAsync(() =>
     {
         using (var client = new RedisClient(Host))
         {
             return client.ContainsKey(key);
         }
     }));
 }
Example #22
0
        public bool ItemExists(string hash)
        {
            bool exists = false;

            using (var client = new RedisClient(_redisURL, _redisPort))
            {
                exists = client.ContainsKey(hash);
            }
            return(exists);
        }
        /// <summary>
        /// IsInCache
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool IsInCache(string key)
        {
            bool isInCache = false;

            using (RedisClient client = new RedisClient(_endPoint))
            {
                isInCache = client.ContainsKey(key);
            }

            return(isInCache);
        }
Example #24
0
 /// <summary>
 /// 存储值为string类型
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <returns></returns>
 public static void SetKey(string key, string value)
 {
     using (var client = new RedisClient(Host))
     {
         if (client.ContainsKey(key))
         {
             client.Del(key);
         }
         client.Add <string>(key, value);
     }
 }
        public T Get <T>(string key)
        {
            using (var client = new RedisClient(new RedisEndpoint("127.0.0.1", 6379)))
            {
                if (!client.ContainsKey(key))
                {
                    return(default(T));
                }

                return(client.Get <T>(key));
            }
        }
Example #26
0
        public override async Task OnConnected()
        {
            var                 isFromConsole = Context.QueryString["console"];
            var                 sessionID     = Context.QueryString["sessionID"];
            Configuration       config        = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
            SessionStateSection section       = (SessionStateSection)config.GetSection("system.web/sessionState");

            try
            {
                using (IRedisClient client = new RedisClient(conf))
                {
                    if (isFromConsole == "0" && !client.ContainsKey(Context.ConnectionId)) // client(Redis)'de ilgili connection yok..
                    {
                        //Eğer Önceden eklenmiş Listede Yok ise
                        //Liste yapmadan amaç önceden bu kayıt var ve Sayfayı Refresh yapmış ise önceki connectionID'sini Redisden yenileyebilmek.
                        if (!PersonList.HasSession(sessionID))
                        {
                            DateTime sessionExpireDate = DateTime.Now.AddMinutes(section.Timeout.TotalMinutes);
                            client.Set(Context.ConnectionId, sessionExpireDate);
                            var _clientData = new ClientData()
                            {
                                ClientConnectionID = Context.ConnectionId, ClientSessionTime = sessionExpireDate
                            };
                            PersonList.Add(sessionID, _clientData);
                        }
                        else
                        {
                            //Eğer Önceden eklenmiş Listede Var İse ama redis'de yok ise kısa bir süre önce sayfadan ayrılınmış demektir.
                            //PersonList'e eklenen süre, session'ın süresini geçmiş ise redis'e  atılmadan yani client'a gönderilmeden ilgili client function tetiklenir.
                            DateTime sessionTime = PersonList.GetSession(sessionID).ClientSessionTime;
                            var      seconds     = sessionTime.Subtract(DateTime.Now).TotalSeconds;
                            if (seconds > 30)
                            {
                                client.Set(Context.ConnectionId, PersonList.GetSession(sessionID).ClientSessionTime);
                            }
                            else
                            {
                                //string message = seconds < 30 ? "Session Süreniz Dolmuştur" : "Session Sürenizin Dolmasına çok az kalmıştır";
                                string message = seconds <= 0 ? "Session Süreniz Dolmuştur" : "Session Sürenizin Dolmasına çok az kalmıştır";
                                await Clients.Caller.notifyUser(message);
                            }
                        }
                        PersonList.ClearExpiredPersonList(); //Süresi dolanlar listeden kaldırılır.
                    }
                    await Clients.Caller.notifySession(Context.ConnectionId + ":" + DateTime.Now.AddMinutes(section.Timeout.TotalMinutes));
                }
            }
            catch (Exception ex)
            {
                int i = 0;
            }
        }
 public static T Get <T>(string key, Func <T> source)
 {
     using (IRedisClient client = new RedisClient(redisHost))
     {
         if (client.ContainsKey(key))
         {
             return(client.Get <T>(key));
         }
         T obj = source();
         client.Set <T>(key, obj);
         return(obj);
     }
 }
Example #28
0
 /// <summary>
 /// Get
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="func"></param>
 /// <returns></returns>
 public T Get <T>(string key, Func <T> func)
 {
     using (var client = new RedisClient(RedisHost))
     {
         if (!client.ContainsKey(key))
         {
             lock (LockObj)
             {
                 if (!client.ContainsKey(key))
                 {
                     T obj = func();
                     if (null == obj)
                     {
                         obj = default(T);
                     }
                     client.Set <T>(key, obj);
                     return(obj);
                 }
             }
         }
         return(client.Get <T>(key));
     }
 }
Example #29
0
 public bool IsKeyExists(string key)
 {
     using (var redisClient = new RedisClient(_redisEndpoint))
     {
         if (redisClient.ContainsKey(key))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Example #30
0
 /// <summary>
 /// 通过key获得string类型
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public static string GetKey(string key)
 {
     using (var client = new RedisClient(Host))
     {
         if (client.ContainsKey(key))
         {
             return(client.Get <string>(key));
         }
         else
         {
             return(null);
         }
     }
 }
 public bool IsSet(string key)
 {
     try
     {
         using (IRedisClient client = new RedisClient(conf))
         {
             return(client.ContainsKey(key));
         }
     }
     catch
     {
         throw new RedisNotAvailableException();
         //return false;
     }
 }