public bool Keydeletes(string textdel)
 {
     if (_redis.ContainsKey(textdel))
     {
         _redis.Remove(textdel);
         return(true);
     }
     else
     {
         return(false);
     }
 }
예제 #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);
            }

        }
예제 #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));
     }
 }
예제 #4
0
 public static bool Exists(string key)
 {
     using (var client = new RedisClient(Host))
     {
         return(client.ContainsKey(key));
     }
 }
예제 #5
0
 public bool HasMessage()
 {
     using (var redisClient = new RedisClient())
     {
         return redisClient.ContainsKey(string.Format("{0}:{1}", _address.Machine, _address.Queue));
     }
 }
예제 #6
0
 public static bool ContainsKey(string key)
 {
     using (RedisClient client = GetClient())
     {
         return(client.ContainsKey(key));
     }
 }
예제 #7
0
 public bool Exists()
 {
     using (var redisClient = new RedisClient())
     {
         return(redisClient.ContainsKey("asa." + _cacheKey + ".all"));
     }
 }
예제 #8
0
파일: Program.cs 프로젝트: muairui/c-
        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);
        }
예제 #9
0
 public bool ContainsKey(string cachekey)
 {
     using (IRedisClient client = new RedisClient())
     {
         return(client.ContainsKey(cachekey));
     }
 }
예제 #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);
         }
     }
 }
예제 #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);
                }
            }
        }
예제 #12
0
 public bool IsSet(string key)
 {
     using (IRedisClient client = new RedisClient(_redisConfiguration))
     {
         return(client.ContainsKey(key));
     }
 }
예제 #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);
            }
        }
예제 #14
0
 public bool IsSet(string key)
 {
     using (IRedisClient client = new RedisClient(conf))
     {
         return(client.ContainsKey(key));
     }
 }
예제 #15
0
 /// <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));
     }
 }
예제 #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);
        }
예제 #17
0
        public bool IsContainsKey(string key)
        {
            bool result;

            using (RedisClient redisClient = this.GetRedisClient())
            {
                result = redisClient.ContainsKey(key);
            }
            return(result);
        }
예제 #18
0
 public void Clear <T>(string cachekey)
 {
     if (ContainsKey(cachekey))
     {
         using (IRedisClient client = new RedisClient())
         {
             client.ContainsKey(cachekey);
         }
     }
 }
예제 #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();
        }
예제 #20
0
        private static bool IsCacheKeyExistsRedis(string key)
        {
            bool result;

            using (var client = new RedisClient())
            {
                result = client.ContainsKey(key);
            }
            return(result);
        }
예제 #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);
         }
     }));
 }
예제 #22
0
        public bool ItemExists(string hash)
        {
            bool exists = false;

            using (var client = new RedisClient(_redisURL, _redisPort))
            {
                exists = client.ContainsKey(hash);
            }
            return(exists);
        }
예제 #23
0
        /// <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);
        }
예제 #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));
            }
        }
예제 #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;
            }
        }
예제 #27
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);
     }
 }
예제 #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));
     }
 }
예제 #29
0
 public bool IsKeyExists(string key)
 {
     using (var redisClient = new RedisClient(_redisEndpoint))
     {
         if (redisClient.ContainsKey(key))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
예제 #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);
         }
     }
 }
예제 #31
0
 public bool IsSet(string key)
 {
     try
     {
         using (IRedisClient client = new RedisClient(conf))
         {
             return(client.ContainsKey(key));
         }
     }
     catch
     {
         throw new RedisNotAvailableException();
         //return false;
     }
 }