Exemple #1
0
        private async Task <CacheItem <TValue> > GetCacheItemAsync <TValue>(CacheContext context, string key)
        {
            var cacheItem = await context.GetItemAsync <TValue>(Name, key);

            return(cacheItem);
        }
Exemple #2
0
        public RedisCache(String name, RedisCachePolicy policy)
        {
            this.name = name;
            this.log  = NLog.LogManager.GetLogger(typeof(RedisCache).FullName);

            log.Debug("Init Cache {0}", this.name);

            if (policy == null)
            {
                log.Error("Invalid Policy for Cache {0}", this.name);
                throw new ArgumentNullException(nameof(policy));
            }

            if (!string.IsNullOrEmpty(policy.ConnectionName))
            {
                this.connectionString = CacheManager.GetConnectionString(policy.ConnectionName)?.ConnectionString;

                if (string.IsNullOrEmpty(connectionString))
                {
                    throw new ArgumentException(
                              $"{nameof(ICacheConnectionString.ConnectionString)} not found for {nameof(policy.ConnectionName)} {policy.ConnectionName}", $"{nameof(policy)}.{nameof(policy.ConnectionName)}");
                }
            }
            else if (!string.IsNullOrEmpty(policy.ConnectionString))
            {
                this.connectionString = policy.ConnectionString;
            }
            else
            {
                throw new ArgumentException(
                          $"{nameof(policy.ConnectionString)} is undefined", $"{nameof(policy)}.{nameof(policy.ConnectionString)}");
            }

            this.monitorPort = policy.MonitorPort;
            this.monitorIntervalMilliseconds = policy.MonitorIntervalMilliseconds;
            this.converterType = policy.Converter;
            this.clusterType   = policy.ClusterType;

            if (policy.SlidingExpiration.HasValue && policy.SlidingExpiration.Value < TimeSpan.MaxValue)
            {
                this.expireWithin         = policy.SlidingExpiration.Value;
                this.useSlidingExpiration = true;
                this.expireAt             = null;
            }
            else if (policy.ExpirationFromAdd.HasValue && policy.ExpirationFromAdd.Value < TimeSpan.MaxValue)
            {
                this.expireWithin         = policy.ExpirationFromAdd.Value;
                this.useSlidingExpiration = false;
                this.expireAt             = null;
            }
            else if (policy.AbsoluteExpiration.HasValue && policy.AbsoluteExpiration.Value < DateTimeOffset.MaxValue)
            {
                this.expireWithin         = null;
                this.useSlidingExpiration = false;
                this.expireAt             = policy.AbsoluteExpiration.Value.LocalDateTime;
            }
            else
            {
                this.expireWithin         = null;
                this.useSlidingExpiration = false;
                this.expireAt             = null;
            }

            this.useSlidingExpiration = (policy.SlidingExpiration < TimeSpan.MaxValue);

            this.innerCache = new CacheContext(
                this.connectionString, this.converterType, this.clusterType, this.monitorPort, this.monitorIntervalMilliseconds);

            this.notiferName = policy.SyncProvider;

            this.synchronizer = CacheSynchronizer.CreateCacheSynchronizer(this, this.notiferName);
        }
Exemple #3
0
        private CacheItem <TValue> GetCacheItem <TValue>(CacheContext context, string key)
        {
            var cacheItem = context.GetItem <TValue>(this.Name, key);

            return(cacheItem);
        }