Exemplo n.º 1
0
        public async Task <IActionResult> Get(int id)
        {
            Log.Information($"Get entry: {id}");

            var entry = await _cache.GetCacheValueAsync <Entry>($"entry_{id}");

            if (entry != null)
            {
                return(Ok(JsonConvert.SerializeObject(entry)));
            }

            entry = _mapper.Map <Entry>(_unitOfWork.Entries.Get(id));
            await _cache.SetCacheValueAsync($"entry_{id}", entry);

            return(Ok(JsonConvert.SerializeObject(entry)));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Get(int id)
        {
            Log.Information($"Get phoneBook: {id}");

            var phoneBook = await _cache.GetCacheValueAsync <PhoneBook>($"phoneBookResult_{id}");

            if (phoneBook != null)
            {
                return(Ok(JsonConvert.SerializeObject(phoneBook)));
            }

            phoneBook = _mapper.Map <PhoneBook>(_unitOfWork.PhoneBooks.Get(id));
            await _cache.SetCacheValueAsync($"phoneBookResult_{id}", phoneBook);

            return(Ok(JsonConvert.SerializeObject(phoneBook)));
        }
        public async Task <IEnumerable <WeatherForecast> > Get()
        {
            var weatherResult = await _cache.GetCacheValueAsync <List <WeatherForecast> >("weathers1");

            if (weatherResult == null)
            {
                var rng = new Random();
                weatherResult = Enumerable.Range(1, 5).Select(index => new WeatherForecast
                {
                    Date         = DateTime.Now.AddDays(index),
                    TemperatureC = rng.Next(-20, 55),
                    Summary      = Summaries[rng.Next(Summaries.Length)]
                }).ToList();
                await _cache.SetCacheValueAsync("weathers1", weatherResult);
            }

            return(weatherResult);
        }
Exemplo n.º 4
0
        public async Task <List <T> > GetAllAsync <T>(string contain) where T : class
        {
            var result = new List <T>();

            using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect($"{Environment.GetEnvironmentVariable("redisserver")}:{Environment.GetEnvironmentVariable("redisserverport")},allowAdmin=true"))
            {
                var keys = redis.GetServer(Environment.GetEnvironmentVariable("redisserver"), int.Parse(Environment.GetEnvironmentVariable("redisserverport"))).Keys();

                var keysArr = keys.Where(key => ((string)key).StartsWith(contain)).ToList();

                foreach (string key in keysArr)
                {
                    var roulettte = await DistributedCache.GetCacheValueAsync <T>(key);

                    result.Add(roulettte);
                }
            }
            return(result);
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Index(CancellationToken cancellationToken)
        {
FetchData:
            var result = await _distributedCache.GetCacheValueAsync <string>("code");

            if (string.IsNullOrEmpty(result))
            {
                var generatedCode = RandomHelper.GenerateRandom(_configuration.GetValue <int>("Length"));

                await _distributedCache.SetCacheValueAsync("code", generatedCode, cancellationToken);

                goto FetchData;
            }

            ViewBag.Result = $"Your Code is: {result}";

            _logger.LogInformation($"your code is: {result}");

            return(View());
        }
Exemplo n.º 6
0
        public async Task <ApiResult> Handle(GetListProductRequest request, CancellationToken cancellationToken)
        {
            try
            {
                var cacheKey = CacheKey.LIST_PRODUCT + JsonSerializer.Serialize(request);
                var products = await _cache.GetCacheValueAsync <Paginate <ProductViewModel> >(cacheKey);

                if (products == null)
                {
                    products = await GetListProductInDatabase(request, cancellationToken);

                    _ = _cache.SetCacheValueAsync(cacheKey, products, 60 * 5);
                }

                return(ApiResult.Succeeded(products));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                return(ApiResult.Failed(HttpCode.InternalServerError));
            }
        }
Exemplo n.º 7
0
 public async Task <model.Roulette> GetRoulette(int Id)
 {
     return(await DistributedCache.GetCacheValueAsync <model.Roulette>(BaseKey + Id.ToString()));
 }