Пример #1
0
        public T Get <T>(string key, DataType type)
        {
            T value = (T)GetByCache(key);

            if (value != null)
            {
                return(value);
            }
            using (RedisHost.ClientItem c = GetReader())
            {
                using (Command cmd = new Command())
                {
                    cmd.Add(CONST_VALURES.REDIS_COMMAND_GET);
                    cmd.Add(key);
                    using (Result result = TcpClient.Send(cmd, c.Client))
                    {
                        if (result.ResultDataBlock.Count > 0)
                        {
                            value = (T)FromRedis(result.ResultDataBlock[0], type, typeof(T));
                            SetCache(key, value);
                            return(value);
                        }
                    }
                }

                return(default(T));
            }
        }
Пример #2
0
        public IList <T> ListRange <T>(string key, int start, int end, DataType dtype)
        {
            string    cachekey = string.Format("{0}_list_{1}_{2}", key, start, end);
            IList <T> lst      = null;

            lst = (IList <T>)GetByCache(cachekey);
            using (RedisHost.ClientItem c = GetReader())
            {
                lst = new List <T>();
                using (Command cmd = new Command())
                {
                    cmd.Add(CONST_VALURES.REDIS_COMMAND_LRANGE);
                    cmd.Add(key);
                    cmd.Add(start.ToString());
                    cmd.Add(end.ToString());
                    using (Result result = TcpClient.Send(cmd, c.Client))
                    {
                        foreach (ArraySegment <byte> item in result.ResultDataBlock)
                        {
                            lst.Add((T)FromRedis(item, dtype, typeof(T)));
                        }
                    }
                }
                SetCache(cachekey, lst);
                return(lst);
            }
        }
Пример #3
0
        public IList <object> Sort(string key, int?offset, int?count, string BYpattern, string GETpattern, bool ALPHA, string STOREdestination,
                                   SortOrderType orderby, Type type, DataType dtype)
        {
            List <object> result = new List <object>();

            using (RedisHost.ClientItem c = GetReader())
            {
                using (Command cmd = new Command())
                {
                    cmd.Add(CONST_VALURES.REDIS_COMMAND_SORT);
                    cmd.Add(key);
                    if (!string.IsNullOrEmpty(BYpattern))
                    {
                        cmd.Add("BY");
                        cmd.Add(BYpattern);
                    }
                    if (!string.IsNullOrEmpty(GETpattern))
                    {
                        cmd.Add("GET");
                        cmd.Add(GETpattern);
                    }
                    if (offset != null)
                    {
                        cmd.Add("LIMIT");
                        cmd.Add(offset.Value.ToString());
                        cmd.Add(count == null?"1000":count.Value.ToString());
                    }
                    if (ALPHA)
                    {
                        cmd.Add("ALPHA");
                    }
                    cmd.Add(Enum.GetName(typeof(SortOrderType), orderby));
                    if (!string.IsNullOrEmpty(STOREdestination))
                    {
                        cmd.Add("STORE");
                        cmd.Add(STOREdestination);
                    }

                    using (Result rd = TcpClient.Send(cmd, c.Client))
                    {
                        foreach (ArraySegment <byte> item in rd.ResultDataBlock)
                        {
                            result.Add(FromRedis(item, dtype, type));
                        }
                    }
                }
            }
            return(result);
        }
Пример #4
0
 public T ListRemove <T>(string key, DataType dtype)
 {
     using (RedisHost.ClientItem c = GetWriter())
     {
         using (Command cmd = new Command())
         {
             cmd.Add(CONST_VALURES.REDIS_COMMAND_RPOP);
             cmd.Add(key);
             using (Result result = TcpClient.Send(cmd, c.Client))
             {
                 return((T)FromRedis(result.ResultDataBlock[0], dtype, typeof(T)));
             }
         }
     }
 }
Пример #5
0
 public void ListAdd(string key, object value, DataType dtype)
 {
     using (RedisHost.ClientItem c = GetWriter())
     {
         using (Command cmd = new Command())
         {
             cmd.Add(CONST_VALURES.REDIS_COMMAND_RPUSH);
             cmd.Add(key);
             ToRedis(value, dtype, cmd);
             using (Result result = TcpClient.Send(cmd, c.Client))
             {
             }
         }
     }
 }
Пример #6
0
 public T GetListItem <T>(string key, int index, DataType dtype)
 {
     using (RedisHost.ClientItem c = GetReader())
     {
         using (Command cmd = new Command())
         {
             cmd.Add(CONST_VALURES.REDIS_COMMAND_LINDEX);
             cmd.Add(key);
             cmd.Add(index.ToString());
             using (Result result = TcpClient.Send(cmd, c.Client))
             {
                 return((T)FromRedis(result.ResultDataBlock[0], dtype, typeof(T)));
             }
         }
     }
 }
Пример #7
0
        public int ListLength(string key)
        {
            using (RedisHost.ClientItem c = GetReader())
            {
                using (Command cmd = new Command())
                {
                    cmd.Add(CONST_VALURES.REDIS_COMMAND_LLEN);
                    cmd.Add(key);

                    using (Result result = TcpClient.Send(cmd, c.Client))
                    {
                        return(int.Parse(result.ResultData.ToString()));
                    }
                }
            }
        }
Пример #8
0
 public int PEXPIRE(string key, long milliseconds)
 {
     using (RedisHost.ClientItem c = GetWriter())
     {
         using (Command cmd = new Command())
         {
             cmd.Add(CONST_VALURES.REDIS_COMMAND_PEXPIRE);
             cmd.Add(key);
             cmd.Add(milliseconds.ToString());
             using (Result result = TcpClient.Send(cmd, c.Client))
             {
                 return(int.Parse(result.ResultData.ToString()));
             }
         }
     }
 }
Пример #9
0
        public IList <Field> GetFields(string key, NameType[] fields, DataType type)
        {
            List <Field> result = GetResultSpace <Field>(fields.Length);
            object       value;
            NameType     item;

            using (RedisHost.ClientItem c = GetReader())
            {
                List <NameType> inputs = new List <NameType>();
                using (Command cmd = new Command())
                {
                    cmd.Add(CONST_VALURES.REDIS_COMMAND_HMGET);
                    cmd.Add(key);
                    for (int i = 0; i < fields.Length; i++)
                    {
                        item  = fields[i];
                        value = GetByCache(key + "_field_" + item.Name);
                        if (value != null)
                        {
                            result[i] = new Field {
                                Name = item.Name, Value = value
                            };
                        }
                        else
                        {
                            inputs.Add(item);
                            item.Index = i;
                            cmd.Add(item.Name);
                        }
                    }
                    using (Result R = TcpClient.Send(cmd, c.Client))
                    {
                        for (int i = 0; i < inputs.Count; i++)
                        {
                            item = inputs[i];

                            value = FromRedis(R.ResultDataBlock[i], type, item.Type);
                            SetCache(key + "_field_" + item.Name, value);
                            result[item.Index] = new Field {
                                Name = item.Name, Value = value
                            };
                        }
                    }
                }
            }
            return(result);
        }
Пример #10
0
 public void Set(Field[] KValues, DataType dtype)
 {
     using (RedisHost.ClientItem c = GetWriter())
     {
         using (Command cmd = new Command())
         {
             cmd.Add(CONST_VALURES.REDIS_COMMAND_MSET);
             foreach (Field item in KValues)
             {
                 cmd.Add(item.Name);
                 ToRedis(item.Value, dtype, cmd);
             }
             using (Result result = TcpClient.Send(cmd, c.Client))
             {
             }
         }
     }
 }
Пример #11
0
 public void SetFields(string key, IEnumerable <Field> items, DataType type)
 {
     using (RedisHost.ClientItem c = GetWriter())
     {
         using (Command cmd = new Command())
         {
             cmd.Add(CONST_VALURES.REDIS_COMMAND_HMSET);
             cmd.Add(key);
             foreach (Field item in items)
             {
                 cmd.Add(item.Name);
                 ToRedis(item.Value, type, cmd);
             }
             using (Result result = TcpClient.Send(cmd, c.Client))
             {
             }
         }
     }
 }
Пример #12
0
 public void HDEL(string key, params string[] fields)
 {
     using (RedisHost.ClientItem c = GetWriter())
     {
         using (Command cmd = new Command())
         {
             cmd.Add(CONST_VALURES.REDIS_COMMAND_HDEL);
             cmd.Add(key);
             foreach (string field in fields)
             {
                 DelCache(key + "_field_" + field);
                 cmd.Add(field);
             }
             using (Result result = TcpClient.Send(cmd, c.Client))
             {
             }
         }
     }
 }
Пример #13
0
 public int Delete(params string[] keys)
 {
     if (mCached != null)
     {
         mCached.Delete(keys);
     }
     using (RedisHost.ClientItem c = GetWriter())
     {
         using (Command cmd = new Command())
         {
             cmd.Add(CONST_VALURES.REDIS_COMMAND_DEL);
             foreach (string key in keys)
             {
                 cmd.Add(key);
             }
             using (Result result = TcpClient.Send(cmd, c.Client))
             {
                 return(int.Parse(result.ResultData.ToString()));
             }
         }
     }
 }
Пример #14
0
 public IList <object> Get(Type[] types, string[] keys, DataType dtype)
 {
     using (RedisHost.ClientItem c = GetReader())
     {
         List <object>   result = GetResultSpace <object>(keys.Length);
         List <NameType> _types = new List <NameType>();
         using (Command cmd = new Command())
         {
             cmd.Add(CONST_VALURES.REDIS_COMMAND_MGET);
             for (int i = 0; i < keys.Length; i++)
             {
                 string key  = keys[i];
                 object data = GetByCache(key);
                 if (data != null)
                 {
                     result[i] = data;
                 }
                 else
                 {
                     cmd.Add(key);
                     _types.Add(new NameType(types[i], keys[i], i));
                 }
             }
             using (Result r = TcpClient.Send(cmd, c.Client))
             {
                 for (int i = 0; i < _types.Count; i++)
                 {
                     object item = FromRedis(r.ResultDataBlock[i], dtype, _types[i].Type);
                     SetCache(_types[i].Name, item);
                     result[_types[i].Index] = item;
                 }
             }
         }
         return(result);
     }
 }
Пример #15
0
        public IList <string> Keys(string match)
        {
            IList <string> r = null;

            using (RedisHost.ClientItem c = GetReader())
            {
                using (Command cmd = new Command())
                {
                    cmd.Add(CONST_VALURES.REDIS_COMMAND_KEYS);

                    cmd.Add(match);

                    using (Result result = TcpClient.Send(cmd, c.Client))
                    {
                        r = new List <string>(result.ResultDataBlock.Count);
                        foreach (ArraySegment <byte> i in result.ResultDataBlock)
                        {
                            r.Add(i.GetString());
                        }
                    }
                }
            }
            return(r);
        }