public IEnumerable <Entry> Add(Entry entity)
        {
            _cacheClient.Add((entity.EntryId).ToString(), entity);
            var result = _cacheClient.Get <Entry>((entity.EntryId).ToString());

            if (result != null)
            {
                _sqlLiteRepository.Add(entity);
            }

            return((IEnumerable <Entry>)result);
        }
        public void Get_With_Complex_Item_Should_Return_Correct_Value()
        {
            var value = Builder <ComplexClassForTest <string, string> >
                        .CreateListOfSize(1)
                        .All()
                        .Build().First();

            Db.StringSet(value.Item1, Serializer.Serialize(value));

            var cachedObject = Sut.Get <ComplexClassForTest <string, string> >(value.Item1);

            Assert.NotNull(cachedObject);
            Assert.Equal(value.Item1, cachedObject.Item1);
            Assert.Equal(value.Item2, cachedObject.Item2);
        }
Пример #3
0
        public async Task <IEnumerable <WordDefinition> > GetWordFromCache(string wordString, string wordType = "")
        {
            if (string.IsNullOrEmpty(wordType))
            {
                var cacheResult = await _cacheClient.GetAllAsync <WordDefinition>(new[] {
                    $"{wordString}-verb",
                    $"{wordString}-noun",
                    $"{wordString}-adjective",
                    $"{wordString}-adverb",
                    $"{wordString}"
                });

                return(cacheResult.Values.Where(v => v != null).ToList());
            }
            else
            {
                var words = new List <WordDefinition>();
                var word  = _cacheClient.Get <WordDefinition>($"{wordString}-{wordType}");
                if (word != null)
                {
                    words.Add(word);
                }

                return(words);
            }
        }
        public bool Validate(Guid id, string text)
        {
            Connect();
            SimpleCaptchaResult captcha = _client.Get <SimpleCaptchaResult>(id.ToString());

            return(string.Equals(captcha.Text, text, StringComparison.CurrentCultureIgnoreCase));
        }
Пример #5
0
        private static void ReadProto(ConnectionMultiplexer redis)
        {
            var serializer  = new ProtobufSerializer();
            var cacheClient = new StackExchangeRedisCacheClient(serializer, "localhost");

            var person = cacheClient.Get <Person>("proto");

            Console.Write($"Person: [{person.Age}]");
        }
Пример #6
0
        public T Read <T>(string key)
        {
            serializer = new NewtonsoftSerializer();
            var redisConfiguration = RedisCachingSectionHandler.GetConfig();

            using (cacheClient = new StackExchangeRedisCacheClient(serializer, redisConfiguration))
            {
                return(cacheClient.Get <T>(key));
            }
        }
Пример #7
0
        public virtual T GetById(string id)
        {
            var resultset = cacheClient.Get <T>(id);

            return(resultset);

            /*
             * var hashes = db.HashGetAll(id);
             * return GetFromEntryList(hashes.ToList());
             */
        }
Пример #8
0
        /// <summary>
        ///
        /// </summary>
        public T Get <T>(string Key) where T : class
        {
            if (_bUseLocalCache)
            {
                return(Utility.Cache.Retrieve <T>(Key));
            }

            var entity = _Cache.Get <string>(Key);

            return(entity != null?Deserialise <T>(entity) : default(T));
        }
Пример #9
0
        public IEnumerable <T> Query <T>(string partitionKey, double min, double max) where T : new()
        {
            IRedis redis = (IRedis) new T();

            if (redis.RedisEntityType == RedisEntityType.String)
            {
                var searchPattern = redis.MakeRedisKey(partitionKey);
                if (searchPattern[searchPattern.Length - 1] != '*')
                {
                    searchPattern += "*";
                }
                var keys = RedisContext.SearchKeys(searchPattern);
                return(keys.Select(key => RedisContext.Get <T>(key)).ToList());
            }
            else if (redis.RedisEntityType == RedisEntityType.SortedList)
            {
                var sortedkey = redis.MakeRedisKey(partitionKey).Replace("*", "");
                return(GetSortedSetByRange <T>(sortedkey, min, max));
            }
            return(null);
        }
Пример #10
0
        public Tuple <string, byte[]> this[string index]
        {
            get
            {
                return(_cache.Get <Tuple <string, byte[]> >(index));
            }

            set
            {
                _cache.Add <Tuple <string, byte[]> >(index, value);
            }
        }
Пример #11
0
        public string GetUserFriendList(int userId)
        {
            //用户好友列表key
            var key = string.Format(LayIMConst.LayIM_All_UserFriends, userId);
            //一天过期
            string list = cacheClient.Get <string>(key);

            if (string.IsNullOrEmpty(list))
            {
                return("");
            }
            return(list);
        }
        static void Main(string[] args)
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
            // for multiple redis servers
            //ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("server1:6379,server2:6379");

            IDatabase db = redis.GetDatabase();

            //Loading and getting a string.
            db.StringSet("some key", "Some Value");
            string gottenFromRedisString = db.StringGet("some key");

            //Loding and getting a byte array from redis.
            byte[] key   = { 0, 1 };
            byte[] value = { 2, 3 };
            db.StringSet(key, value);
            byte[] gottenfromRedisByteArray = db.StringGet(key);

            var someFellow = new Person
            {
                Id      = 1,
                Name    = "Milinda",
                Age     = 26,
                IsMinor = false
            };

            //Loading and getting an Object from Redis using my own serialization methods.
            db.StringSet(JsonConvert.SerializeObject(someFellow.Id), JsonConvert.SerializeObject(someFellow));
            string personJsonString            = db.StringGet("1");
            Person gottenfromRedisPersonCustom = JsonConvert.DeserializeObject <Person>(personJsonString);

            //setting and getting an object from redis using StackExchange.Redis.Extensions
            ISerializer  serializer            = new NewtonsoftSerializer();
            ICacheClient cacheClient           = new StackExchangeRedisCacheClient(redis, serializer);
            bool         success               = cacheClient.Add(someFellow.Id.ToString(), someFellow);
            Person       gottenfromRedisPerson = cacheClient.Get <Person>("1");

            //Writing out the stuff we got back from redis to the console.
            Console.WriteLine($"Gotten from String: {gottenFromRedisString}");
            Console.WriteLine($"Gotten from Array: {gottenfromRedisByteArray}");
            Console.WriteLine($"Gotten from Custom Serializer: {gottenfromRedisPersonCustom.Name}");
            Console.WriteLine($"Gotten from StackExchange.Redis.Extensions: {gottenfromRedisPerson.Name}");

#if DEBUG
            Console.ReadLine();
#endif
        }
Пример #13
0
        static void Main(string[] args)
        {
            ISerializer  serializer = new NewtonsoftSerializer();
            var          c          = ConnectionMultiplexer.Connect("localhost");
            ICacheClient client     = new StackExchangeRedisCacheClient(c, serializer);

            client.Add("Test", new User()
            {
                Name     = "Test",
                Age      = 100,
                Birthday = DateTime.Now
            });

            Console.WriteLine(client.Get <User>("Test").ToString());

            Console.ReadKey();
        }
Пример #14
0
        public IEnumerable <Host> GetAll()
        {
            var config = new RedisConfiguration()
            {
                AbortOnConnectFail = false,
                KeyPrefix          = "MyApp",
                Hosts = new RedisHost[] {
                    new RedisHost()
                    {
                        Host = "redis", Port = 6379
                    }
                },
            };

            var cacheClient = new StackExchangeRedisCacheClient(new NewtonsoftSerializer(), config);

            return(cacheClient.Get <List <Host> >("myhost"));
        }
        public static List <T> GetAll <T>()
        {
            var cache      = Connection.GetDatabase();
            var serializer = new NewtonsoftSerializer();
            var keys       = cacheClient.SearchKeys("*").ToList();
            var list       = new List <T>();

            foreach (var item in keys)
            {
                var value = cacheClient.Get <string>(item.ToString());
                var obj   = JsonConvert.DeserializeObject <T>(value);
                if (obj != null)
                {
                    list.Add(obj);
                }
            }

            return(list);
        }
Пример #16
0
        static void Main(string[] args)
        {
            var redisCacheConnectionString = ConfigurationManager.ConnectionStrings["RedisCacheConnection"].ToString();
            var cacheConnection            = ConnectionMultiplexer.Connect(redisCacheConnectionString);
            var serializer  = new StackExchangeRedisExtensionsMessagePackSerializer();
            var cacheClient = new StackExchangeRedisCacheClient(serializer, cacheConnection.Configuration);

            var testModel = new TestModel
            {
                Now    = DateTime.Now,
                UTCNow = DateTime.UtcNow
            };

            cacheClient.Add("testModel", testModel);

            var fromCache = cacheClient.Get <TestModel>("testModel");

            var isNowTimeSame             = testModel.Now == fromCache.Now;
            var isNowTimeSamwWhenAdjusted = testModel.Now == fromCache.Now.AddHours(7);
            var isUtcNowTimeSame          = testModel.UTCNow == fromCache.UTCNow;
        }
Пример #17
0
 public T Get <T>(string key) where T : class => Client.Get <T>(key);
Пример #18
0
 public T Get <T>(string key)
 {
     return(cacheClient.Get <T>(key));
 }
 public TValue GetValue <TValue>(string key)
 {
     return(CacheClient.Get <TValue>(key));
     //return database.StringGet(key);
 }
Пример #20
0
 public Student Read(string key)
 {
     return(cacheClient.Get <Student>(key));
 }
Пример #21
0
 public T Get <T>(string key)
 {
     return(_client.Get <T>(key));
 }
Пример #22
0
 public T Get <T>(string key) => _stackExchangeRedisCacheClient.Get <T>(key);
Пример #23
0
 public T Get <T>(string key) where T : class
 {
     return(_retryPolicy.ExecuteAction(() => _cacheClient.Get <T>(key)));
 }
 public T Get<T>(string key)
 {
     return ExecuteWithRetry(() => cacheClient.Get<T>(key));
 }