Пример #1
0
        public bool Remove(string key)
        {
            IRedisClient client = RedisFactory.CreateClient(key);

            if (client == null)
            {
                return(false);
            }

            using (client)
            {
                return(client.Remove(key));
            }
        }
Пример #2
0
        public bool Set(string key, object value)
        {
            IRedisClient client = RedisFactory.CreateClient(key);

            if (client == null)
            {
                return(false);
            }

            using (client)
            {
                if (IsSerializableType(value.GetType()))
                {
                    return(client.Set(key, BinarySerialize(value)));
                }

                return(client.Set(key, new ValueWrapper <object>(value)));
            }
        }
Пример #3
0
        public T Get <T>(string key)
        {
            IRedisClient client = RedisFactory.CreateClient(key);

            if (client == null)
            {
                return(default(T));
            }

            T result = default(T);

            using (client)
            {
                bool isDeserializable = IsSerializableType(typeof(T));
                if (isDeserializable)
                {
                    try
                    {
                        byte[] buffer = client.Get <byte[]>(key);
                        //先尝试进行二进制序列化
                        if (buffer != null)
                        {
                            using (MemoryStream ms = new MemoryStream(buffer))
                            {
                                object obj = new BinaryFormatter().Deserialize(ms);
                                result = (T)obj;
                            }
                        }
                    }
                    catch
                    {
                        isDeserializable = false;
                    }
                }

                if (!isDeserializable)
                {
                    string           cacheValue = client.GetValue(key);
                    ValueWrapper <T> wrapper    = JsonSerializer.DeserializeFromString <ValueWrapper <T> >(cacheValue);
                    if (wrapper != null)
                    {
                        if (wrapper.Value.GetType() == wrapper.ValueType) //判断序列化的类型与实际类型是否一致
                        {
                            result = wrapper.Value;
                        }
                        else
                        {
                            Type wrapperType = typeof(ValueWrapper <>);
                            wrapperType = wrapperType.MakeGenericType(wrapper.ValueType);
                            MethodInfo method       = typeof(JsonSerializer).GetMethod("DeserializeFromString", new [] { typeof(string) });
                            MethodInfo generic      = method.MakeGenericMethod(wrapperType);
                            object     corretResult = generic.Invoke(null, new[] { cacheValue });
                            if (corretResult != null)
                            {
                                result = (T)wrapperType.GetProperty("Value").GetValue(corretResult, null);
                            }
                        }
                    }
                }
            }

            return(result);
        }