Exemplo n.º 1
0
        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);
                    }
                }
            }));
        }
Exemplo n.º 2
0
        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)));
        }
Exemplo n.º 3
0
        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));
        }
Exemplo n.º 4
0
        public RedisCacheWriter(IConnectionMultiplexer connectionMultiplexer, TimeSpan sleepTime)
        {
            SbAssert.NotNull(connectionMultiplexer, "ConnectionMultiplexer不能为null");
            SbAssert.NotNull(sleepTime, "sleepTime不能为null");

            this.ConnectionMultiplexer = connectionMultiplexer;
            this.SleepTime             = sleepTime;
        }
Exemplo n.º 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;
        }
Exemplo n.º 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);
        }
Exemplo n.º 7
0
        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");
            });
        }
Exemplo n.º 8
0
        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");
            });
        }
Exemplo n.º 9
0
 public SimpleCacheResolver(ICacheManager cacheManager) : base(cacheManager)
 {
     SbAssert.NotNull(cacheManager, "cacheManager Not be Null");
 }
Exemplo n.º 10
0
 protected AbstractCacheResolver(ICacheManager cacheManager)
 {
     this.CacheManager = cacheManager;
     SbAssert.NotNull(CacheManager, "cacheManager Not be Null");
 }