예제 #1
0
        public ActionResult Index()
        {
            var products = _products.GetProducts(_ => true);

            //_cache.AddOrUpdate("products", products,TimeSpan.FromMinutes(1));
            _cache.AddOrUpdate("products", products, DateTimeOffset.UtcNow.AddSeconds(30));
            _cache.AddOrUpdate("str", "string value", DateTimeOffset.UtcNow.AddSeconds(10));

            return(View());
        }
예제 #2
0
        public async Task SimpleCacheForString_WhenAddAndGetValue_ShouldGet_SameValue(string key, string value)
        {
            //Arrange

            //Act
            await _cacheServiceForStringValue.AddOrUpdate(key, value);

            var cached = await _cacheServiceForStringValue.Get(key);

            //Assert
            Assert.That(cached, Is.Not.Null);
            Assert.That(cached, Is.EqualTo(value));
        }
예제 #3
0
        public async Task <FaqResponse <string> > AskThenIAnswer(FaqRequest request)
        {
            var cachedResponse = await _cacheService.Get(request.Ask);

            if (cachedResponse != null)
            {
                return(cachedResponse);
            }

            var qnaRequest = new QnaMakerRequest {
                Question = request.Ask
            };
            var requestContent = new StringContent(JsonConvert.SerializeObject(qnaRequest), Encoding.UTF8, "application/json");
            var requestMessage = GetRequestMessage(requestContent);

            var response = await _httpClient.SendAsync(requestMessage);

            var responseContent = await response.Content.ReadAsStringAsync();

            var qnaResponse = JsonConvert.DeserializeObject <QnaMakerResponse>(responseContent);
            var faqResponse = new FaqResponse <string> {
                Answer = HttpUtility.HtmlDecode(qnaResponse.Answer), Score = qnaResponse.Score / 100f
            };

            await _cacheService.AddOrUpdate(request.Ask, faqResponse);

            return(faqResponse);
        }
예제 #4
0
        public void SaveConfiguration(List <Configuration> confs)
        {
            dataFlowDbContext.Configurations.AddOrUpdate(confs.ToArray());
            dataFlowDbContext.SaveChanges();

            cacheService.AddOrUpdate(Constants.ConfigurationCacheKey, JsonConvert.SerializeObject(confs));
        }
예제 #5
0
 private void AddOrUpdateRateLimit(ulong messageId, ulong userId)
 {
     _cache.AddOrUpdate(CacheId.StarboardUserMessageReactCountId(messageId, userId), new CacheItem(1, _userRatelimitTtl), (id, item) =>
     {
         int amount = (int)item.Content;
         return(new CacheItem(amount + 1, _userRatelimitTtl)); // Let's refresh the TTL on update
     });
 }
예제 #6
0
        public async Task SimpleCacheForFaqResponse_WhenAddAndGetValue_ShouldGet_SameValue()
        {
            //Arrange
            var key   = "key";
            var value = new FaqResponse <string>()
            {
                Answer = "value", Score = 1
            };

            //Act
            await _cacheServiceForFaqResponseValue.AddOrUpdate(key, value);

            var cached = await _cacheServiceForFaqResponseValue.Get(key);

            //Assert
            Assert.That(cached, Is.Not.Null);
            Assert.That(cached.CompareTo(value) == 0);
        }
예제 #7
0
        public AuthorizationToken GetAuthorizationToken()
        {
            var cacheKey    = GetCacheKey();
            var cachedValue = cacheService.Get(cacheKey);

            if (cachedValue == null)
            {
                apiClient.Configuration = configuration;

                var response = GetAccessTokenResponse();

                SetConfigParameters(response);
                cacheService.AddOrUpdate(cacheKey, response);

                return(new AuthorizationToken(response.AccessToken, response.TokenType));
            }
            else
            {
                SetConfigParameters(cachedValue);
                return(new AuthorizationToken(cachedValue.AccessToken, cachedValue.TokenType));
            }
        }