public Person Get(int id)
        {
            string personId = $"person{id}";

            if (_cacheAdapter.Exists(personId))
            {
                return(_cacheAdapter.Get <Person>(personId));
            }
            Person person = _personRepository.Get(id);

            _cacheAdapter.Add(personId, person);
            return(person);
        }
예제 #2
0
        public async Task <TEntity> FirstItemAsync(Expression <Func <TEntity, bool> > predicate, CancellationToken cancellationToken)
        {
            string key = string.Concat(SingleObjectCacheKey, predicate.Body.ToString());

            if (CacheAdapter.Exist(key))
            {
                return(CacheAdapter.Get <TEntity>(key));
            }
            else
            {
                TEntity entity = await _repository.FirstItemAsync(predicate, cancellationToken);

                CacheAdapter.Add(key, entity);
                return(entity);
            }
        }
예제 #3
0
 /// <summary>
 /// 读取缓存
 /// </summary>
 /// <typeparam name="T">数据类型</typeparam>
 /// <param name="key">键</param>
 /// <returns>值</returns>
 public static T Get <T>(string key)
 {
     using (ICacheAdapter cacheAdapter = (ICacheAdapter)Activator.CreateInstance(_CacheImplType))
     {
         return(cacheAdapter.Get <T>(key));
     }
 }
예제 #4
0
        public async Task <List <TagModel> > GetTags()
        {
            const string cacheKey   = "TagList";
            var          cachedTags = await _cacheAdapter.Get <List <TagModel> >(cacheKey);

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

            var tags = _louisvilleDemographicsContext.Tags
                       .Select(t => new TagModel {
                PostalAbbreviation = t.USPSStandardAbbreviation,
                Name = t.Name
            }).ToList();

            await _cacheAdapter.Add(cacheKey, tags, TimeSpan.FromMinutes(_cacheSettings.CacheTagsMinutes));

            return(tags);
        }
예제 #5
0
        public IList <Customer> GetAllCustomers()
        {
            IList <Customer> customers = (List <Customer>)_cacheAdapter.Get(StorageKey);

            if (customers == null)
            {
                customers = _customerRepository.GetCustomers();
                _cacheAdapter.Insert(StorageKey, customers);
            }

            return(customers);
        }
예제 #6
0
        public async Task Invoke(HttpContext context)
        {
            var newsCount = _cacheAdapter.Get <string>("NewsCount");

            if (newsCount == null)
            {
                newsCount = _newsRepository.GetNewsCount().ToString();

                _cacheAdapter.Set <string>("NewsCount", newsCount);
            }

            await context.Response.WriteAsync($"News count is {newsCount}\n");

            await _next(context);
        }
        public async Task Invoke(HttpContext context)
        {
            var newsCount = _cacheAdapter.Get <string>("NewsCount");

            if (string.IsNullOrEmpty(newsCount))
            {
                newsCount = _newsRepository.GetNewCount().ToString();
                //_cacheAdapter.Set("NewsCount", newsCount);
                _cacheAdapter.Set("NewsCount", newsCount, new CacheOptions {
                    SlidingExpiration = TimeSpan.FromSeconds(3),
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(10)
                });
            }

            await context.Response.WriteAsync($"News Count is {newsCount} \n");

            await _next(context);
        }
예제 #8
0
        public async Task <List <string> > GetZipCodes()
        {
            const string cacheKey       = "ZipCodeList";
            var          cachedZipCodes = await _cacheAdapter.Get <List <string> >(cacheKey);

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

            var zipCodes = _louisvilleDemographicsContext.Addresses
                           .Where(a => a.Zip != null)
                           .Select(a => a.Zip)
                           .Distinct().ToList();

            await _cacheAdapter.Add(cacheKey, zipCodes, TimeSpan.FromMinutes(_cacheSettings.CacheTagsMinutes));

            return(zipCodes);
        }
예제 #9
0
        public async Task <IStateMachine> GetStateMachine(string senderUniqueKey)
        {
            if (senderUniqueKey == null)
            {
                throw new ArgumentNullException(nameof(senderUniqueKey));
            }

            var stateMachine = _cache.Get <IStateMachine>(senderUniqueKey);

            if (stateMachine == null && _tablesUsagePolicy.IsAvailable)
            {
                stateMachine = await _tables.GetAsync <IStateMachine>(StateMachineTable, senderUniqueKey);

                if (stateMachine != null)
                {
                    _cache.Set(senderUniqueKey, stateMachine, _defaultCacheExpiration);
                }
            }

            return(stateMachine);
        }
예제 #10
0
        public async Task <CommandResult <TResult> > DispatchAsync <TResult>(ICommand <TResult> command, CancellationToken cancellationToken)
        {
            CacheOptions options = _cacheOptionsProvider.Get(command);

            if (options == null)
            {
                return(await _commandDispatcher.DispatchAsync(command, cancellationToken));
            }

            var cacheKey = CacheKey(command);

            TResult result = await _cacheAdapter.Get <TResult>(cacheKey);

            if (result != null)
            {
                return(new CommandResult <TResult>(result, false));
            }

            CommandResult <TResult> executedResult;

            if (options.Semaphore != null)
            {
                await options.Semaphore.WaitAsync(cancellationToken);

                try
                {
                    result = await _cacheAdapter.Get <TResult>(cacheKey);

                    if (result != null)
                    {
                        return(new CommandResult <TResult>(result, false));
                    }
                    else
                    {
                        executedResult = await _commandDispatcher.DispatchAsync(command, cancellationToken);
                    }
                }
                finally
                {
                    options.Semaphore.Release();
                }
            }
            else
            {
                executedResult = await _commandDispatcher.DispatchAsync(command, cancellationToken);
            }

            if (options.LifeTime != null)
            {
                await _cacheAdapter.Set(cacheKey, executedResult.Result, options.LifeTime());
            }
            else if (options.ExpiresAtUtc != null)
            {
                await _cacheAdapter.Set(cacheKey, executedResult.Result, options.ExpiresAtUtc());
            }
            else
            {
                // shouldn't happen but lets make sure we spit out a sensible error if it does
                throw new CacheConfigurationException("Either a lifetime or expiry date must be set for a cached command");
            }

            return(executedResult);
        }