public byte[] PutIfAbsent(string name, byte[] key, byte[] value, TimeSpan ttl)
        {
            SbAssert.NotNull(name, "Name must not be null!");
            SbAssert.NotNull(key, "Key must not be null!");
            SbAssert.NotNull(value, "Value must not be null!");

            return(Execute <byte[]>(name, database =>
            {
                if (IsLockingCacheWriter())
                {
                    DoLock(name, database);
                }

                try
                {
                    if (database.StringSet(key, value, when: When.NotExists))
                    {
                        if (ShouldExpireWithin(ttl))
                        {
                            database.KeyExpire(key, ttl);
                        }
                        return null;
                    }

                    return database.StringGet(key);
                }
                finally
                {
                    if (IsLockingCacheWriter())
                    {
                        DoUnlock(name, database);
                    }
                }
            }));
        }
        public byte[] Get(string name, byte[] key)
        {
            SbAssert.NotNull(name, "Name must not be null!");
            SbAssert.NotNull(key, "Key must not be null!");

            return(Execute(name, database => database.StringGet(key)));
        }
        public void Remove(string name, byte[] key)
        {
            SbAssert.NotNull(name, "Name must not be null!");
            SbAssert.NotNull(key, "Key must not be null!");

            Execute(name, database => database.KeyDelete(key));
        }
        public RedisCacheWriter(IConnectionMultiplexer connectionMultiplexer, TimeSpan sleepTime)
        {
            SbAssert.NotNull(connectionMultiplexer, "ConnectionMultiplexer不能为null");
            SbAssert.NotNull(sleepTime, "sleepTime不能为null");

            this.ConnectionMultiplexer = connectionMultiplexer;
            this.SleepTime             = sleepTime;
        }
Example #5
0
        public RedisCache(string name, RedisCacheConfiguration configuration, IRedisCacheWriter redisCacheWriter)
        {
            SbAssert.NotNull(configuration, "redis 配置RedisCacheConfiguration不能为空");
            SbAssert.NotNull(redisCacheWriter, "RedisCacheWriter不能为空");

            this.Name                    = name;
            this.redisCacheWriter        = redisCacheWriter;
            this.redisCacheConfiguration = configuration;
        }
Example #6
0
        public CacheOperationMetadata GetCacheOperationMetadata(CacheOperation operation, MethodInfo method, Type targetType)
        {
            var cacheKey = new CacheOperationCacheKey(operation, method, targetType);

            this.metadataCaches.TryGetValue(cacheKey, out var metadata);
            if (metadata == null)
            {
                IKeyGenerator keyGenerator;
                var           keyGeneratorName = operation.KeyGenerator;
                if (keyGeneratorName.HasText())
                {
                    keyGenerator = this._serviceProvider.GetServiceByName <IKeyGenerator>(keyGeneratorName);
                }
                else
                {
                    keyGenerator = new SimpleKeyGenerator();
                }

                ICacheResolver cacheResolver;
                var            cacheResolverName = operation.CacheResolver;
                if (cacheResolverName.HasText())
                {
                    cacheResolver = this._serviceProvider.GetServiceByName <ICacheResolver>(cacheResolverName);
                }
                else if (operation.CacheManager.HasText())
                {
                    ICacheManager cacheManager =
                        this._serviceProvider.GetServiceByName <ICacheManager>(operation.CacheManager);

                    SbAssert.NotNull(cacheManager, "cacheManager:" + operation.CacheManager + " Not Exist");

                    cacheResolver = new SimpleCacheResolver(cacheManager);
                }
                else
                {
                    cacheResolver = new SimpleCacheResolver();
                }

                metadata = new CacheOperationMetadata(operation, method, targetType, keyGenerator, cacheResolver);
            }

            return(metadata);
        }
        public void Put(string name, byte[] key, byte[] value, TimeSpan ttl)
        {
            SbAssert.NotNull(name, "Name must not be null!");
            SbAssert.NotNull(key, "Key must not be null!");
            SbAssert.NotNull(value, "Value must not be null!");

            Execute(name, database =>
            {
                if (ShouldExpireWithin(ttl))
                {
                    database.StringSet(key, value, ttl);
                }
                else
                {
                    database.StringSet(key, value);
                }
                return("ok");
            });
        }
        public void Clean(string name, byte[] pattern)
        {
            SbAssert.NotNull(name, "Name must not be null!");

            Execute(name, database =>
            {
                bool wasLocked = false;

                try
                {
                    if (IsLockingCacheWriter())
                    {
                        DoLock(name, database);
                        wasLocked = true;
                    }

                    var endpoints = ConnectionMultiplexer.GetEndPoints(true);


                    foreach (var endpoint in endpoints)
                    {
                        var server = ConnectionMultiplexer.GetServer(endpoint);
                        var keys   = server.Keys(pattern: pattern).ToArray();
                        database.KeyDelete(keys);
                    }
                }
                finally
                {
                    if (wasLocked && IsLockingCacheWriter())
                    {
                        DoUnlock(name, database);
                    }
                }

                return("OK");
            });
        }
Example #9
0
 public SimpleCacheResolver(ICacheManager cacheManager) : base(cacheManager)
 {
     SbAssert.NotNull(cacheManager, "cacheManager Not be Null");
 }
Example #10
0
 protected AbstractCacheResolver(ICacheManager cacheManager)
 {
     this.CacheManager = cacheManager;
     SbAssert.NotNull(CacheManager, "cacheManager Not be Null");
 }