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);
        }
Exemplo n.º 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));
        }
Exemplo n.º 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}]");
        }
Exemplo n.º 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));
            }
        }
Exemplo n.º 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());
             */
        }
Exemplo n.º 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));
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 12
0
        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
        }
Exemplo n.º 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();
        }
Exemplo n.º 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"));
        }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 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;
        }
Exemplo n.º 17
0
 public T Get <T>(string key) where T : class => Client.Get <T>(key);
Exemplo n.º 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);
 }
Exemplo n.º 20
0
 public Student Read(string key)
 {
     return(cacheClient.Get <Student>(key));
 }
Exemplo n.º 21
0
 public T Get <T>(string key)
 {
     return(_client.Get <T>(key));
 }
Exemplo n.º 22
0
 public T Get <T>(string key) => _stackExchangeRedisCacheClient.Get <T>(key);
Exemplo n.º 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));
 }