Ejemplo n.º 1
0
        public async Task <IActionResult> Add(string key, [FromBody] AddCacheItem item)
        {
            if (item == null)
            {
                return(this.NullValueErrorResult(key, _logger));
            }

            if (!item.Seconds.HasValue)
            {
                item.Seconds = 0;
            }

            var value = _memcachedClient.Get(key);

            if (value == null)
            {
                await _memcachedClient.AddAsync(key, item.Value, item.Seconds.Value);

                value = _memcachedClient.Get(key);
                if (value != null)
                {
                    return(StatusCode(StatusCodes.Status201Created));
                }
                else
                {
                    return(this.ApiErrorResult(_logger));
                }
            }
            else
            {
                var error = new { error = "Key Exists", description = "The key:[" + key + "] already exist." };
                return(StatusCode(StatusCodes.Status400BadRequest, error));
            }
        }
Ejemplo n.º 2
0
        public async Task <IForecastResponse> Query(GeoCoordinate coord)
        {
            var cacheKey = $"{coord.Lat},{coord.Long}";

            var result = await _memcachedClient.GetAsync <ForecastResponse>(cacheKey);

            if (!result.Success)
            {
                var sb = new StringBuilder("https://api.darksky.net/forecast/");

                sb.Append(Environment.GetEnvironmentVariable("DARKSKY_API_KEY"));
                sb.Append($"/{coord.Lat},{coord.Long}");

                var query = sb.ToString();

                var forecast = await _weatherQueryDispatchService.Query(query).ConfigureAwait(false);

                await _memcachedClient.AddAsync(cacheKey, forecast, 600);

                return(forecast);
            }
            else
            {
                return(result.Value);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sets the specified cacheKey, cacheValue and expiration async.
        /// </summary>
        /// <returns>The async.</returns>
        /// <param name="cacheKey">Cache key.</param>
        /// <param name="cacheValue">Cache value.</param>
        /// <param name="expiration">Expiration.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public async Task SetAsync <T>(string cacheKey, T cacheValue, TimeSpan expiration) where T : class
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
            ArgumentCheck.NotNull(cacheValue, nameof(cacheValue));
            ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));

            await _memcachedClient.AddAsync(cacheKey, cacheValue, expiration.Seconds);
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Postbody()
        {
            var postbody = (await _blogPostService.GetRecent(10)).First()?.Body;
            await _postbodyMemcachedClient.AddAsync(PostbodyCacheKey, postbody, 10);

            var result = await _postbodyMemcachedClient.GetAsync <string>(PostbodyCacheKey);

            return(result.Success ? Ok() : StatusCode(500));
        }
        public async Task <List <Dictionary <string, object> > > GetOrCreateOrdersListWaitAndPolicyMemcached(Func <Task <List <Dictionary <string, object> > > > query, string key)
        {
            ConcurrentDictionary <object, SemaphoreSlim> _locks = new ConcurrentDictionary <object, SemaphoreSlim>();
            List <Dictionary <string, object> >          cacheEntry;

            var result = await _memcachedClient.GetAsync <List <Dictionary <string, object> > >(key);

            if (!result.Success)
            {
                SemaphoreSlim mylock = _locks.GetOrAdd(key, k => new SemaphoreSlim(1, 1));
                await mylock.WaitAsync();

                try
                {
                    result = await _memcachedClient.GetAsync <List <Dictionary <string, object> > >(key);

                    if (!result.Success)// Key not in cache, so get data.
                    {
                        cacheEntry = await query();

                        await _memcachedClient.AddAsync(key, cacheEntry, 100);
                    }
                    else
                    {
                        cacheEntry = result.Value;
                    }
                }
                finally
                {
                    mylock.Release();
                }
            }
            else
            {
                cacheEntry = result.Value;
            }
            return(cacheEntry);
        }
Ejemplo n.º 6
0
 public Task <bool> AddAsync(string key, object value, int cacheSeconds)
 {
     return(_memcachedClient.AddAsync(key, value, cacheSeconds));
 }
 public async Task SetAsync(string cacheKey, object cacheValue, int cacheSeconds)
 {
     await _cache.AddAsync(cacheKey, cacheValue, cacheSeconds);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// 缓存,用户信息
 /// </summary>
 /// <param name="userid"></param>
 /// <param name="entity"></param>
 /// <returns></returns>
 public async Task SaveUserInfo(long userid, UserShowModel entity)
 {
     await _cache.AddAsync(GetUserInfoKey(userid), entity, 300);
 }