Esempio n. 1
0
        public async Task <T> GetAsync <T>(string key, Func <Task <T> > fallback, bool isForced, TimeSpan?expire) where T : class
        {
            if (isForced)
            {
                T result = await fallback();

                Set <T>(key, result, expire);
                return(result);
            }

            RedisValueWithExpiry redisValue = _cache.StringGetWithExpiry(key);

            if (string.IsNullOrWhiteSpace(redisValue.Value))
            {
                var result = await fallback();

                Set <T>(key, result, expire);
                return(result);
            }

            if (expire.HasValue && redisValue.Expiry?.TotalSeconds < expire.Value.TotalSeconds / 2)
            {
                _cache.KeyExpire(key, expire);
            }

            return(JsonSerializer.Deserialize <T>(redisValue.Value, SerializeOptions));
        }
Esempio n. 2
0
        public void StringGetWithExpiry()
        {
            string Key = "mykeyt";

            db.StringSet(Key, "myvalue", TimeSpan.FromHours(1), When.Always, CommandFlags.None);
            RedisValueWithExpiry rvalwithxp = db.StringGetWithExpiry(Key);
        }
Esempio n. 3
0
        internal RedisValueWithExpiry CreateRedisValueWithExpiry(RedisValue value, TimeSpan?expiry)
        {
            var result = new RedisValueWithExpiry();

            //Box into object so that we can set properties on the same instance
            object oResult = result;

            result.GetType().GetField("expiry", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(oResult, expiry);
            result.GetType().GetField("value", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(oResult, value);

            //Unbox back to struct
            result = (RedisValueWithExpiry)oResult;

            return(result);
        }
Esempio n. 4
0
        internal async Task <RedisValueWithExpiry> GetFromMemoryWithExpiry(string key, Func <Task <RedisValueWithExpiry> > retrieval)
        {
            ValOrRefNullable <RedisValue> cachedValue = _memCache.Get <RedisValue>(key);

            if (cachedValue.HasValue)
            {
                //If we know the expiry, then a trip to redis isn't necessary.
                var expiry = _memCache.GetExpiry(key);
                if (expiry.HasValue)
                {
                    return(CreateRedisValueWithExpiry(cachedValue.Value, expiry.Value));
                }
            }

            RedisValueWithExpiry result = await retrieval();

            //Cache the value and expiry
            _memCache.Add(key, result.Value, result.Expiry, When.Always);

            return(result);
        }
Esempio n. 5
0
        public async Task <LimitRequestBannedData> IsBannedAsync(string ip)
        {
            if (database.KeyExists(ip + ":banned"))
            {
                RedisValueWithExpiry data = await database.StringGetWithExpiryAsync(ip + ":banned");

                LimitRequestBannedData returnData = new LimitRequestBannedData()
                {
                    IsBanned   = true,
                    ExpiryDate = data.Expiry
                };
                return(returnData);
            }
            return(new LimitRequestBannedData()
            {
                IsBanned = false
            });


            //return await database.KeyExistsAsync(ip + ":banned");
        }