public virtual void Clear() { var generationKey = CacheNamespace.GetGenerationKey(); var globalKeysKey = CacheNamespace.GetGlobalKeysKey(); //log.DebugFormat("clear cache : {0}", generationKey); try { using (var client = clientManager.GetClient()) using (var transaction = client.CreateTransaction()) { // Update to a new generation. transaction.QueueCommand(r => r.Increment(generationKey, 1), x => CacheNamespace.SetGeneration(x)); // Empty the set of current keys for this region. // NOTE: The actual cached objects will eventually expire. transaction.QueueCommand( r => r.Remove(globalKeysKey)); transaction.Commit(); } } catch (Exception e) { log.ErrorFormat("could not clear cache : {0}", generationKey); var evtArg = new RedisCacheExceptionEventArgs(e); OnException(evtArg); if (evtArg.Throw) { throw; } } }
protected virtual void OnException(RedisCacheExceptionEventArgs e) { var isSocketException = e.Exception is SocketException || e.Exception.InnerException is SocketException; if (!isSocketException) { e.Throw = true; } }
protected void SyncGeneration() { try { if (CacheNamespace.GetGeneration() == -1) { CacheNamespace.SetGeneration(FetchGeneration()); } } catch (Exception e) { log.ErrorFormat("could not sync generation"); var evtArg = new RedisCacheExceptionEventArgs(e); OnException(evtArg); if (evtArg.Throw) { throw; } } }
public virtual void Unlock(object key) { string globalKey; if (!acquiredLocks.TryGetValue(key, out globalKey)) { return; } log.DebugFormat("releasing cache lock : {0}", key); try { using (var client = clientManager.GetClient()) { client.Remove(globalKey); } } catch (Exception e) { log.ErrorFormat("could not release cache lock : {0}", key); var evtArg = new RedisCacheExceptionEventArgs(e); OnException(evtArg); if (evtArg.Throw) { throw; } } }
public virtual void Remove(object key) { key.ThrowIfNull(); //log.DebugFormat("remove from cache : {0}", key); try { var htKey = CacheNamespace.GetGlobalKeysKey(); ExecuteEnsureGeneration(transaction => transaction.QueueCommand(r => { var dataKey = serializer.Serialize(CacheNamespace.GlobalCacheKey(key)); ((RedisNativeClient) r).HDel(htKey, dataKey); })); } catch (Exception e) { log.ErrorFormat("could not remove from cache : {0}", key); var evtArg = new RedisCacheExceptionEventArgs(e); OnException(evtArg); if (evtArg.Throw) { throw; } } }
public virtual void Put(object key, object value) { key.ThrowIfNull("key"); value.ThrowIfNull("value"); //log.DebugFormat("put in cache : {0}", key); try { var globalKeysKey = CacheNamespace.GetGlobalKeysKey(); var data = serializer.Serialize(value); ExecuteEnsureGeneration(transaction => transaction.QueueCommand(r => { var dataKey = serializer.Serialize(CacheNamespace.GlobalCacheKey(key)); ((IRedisNativeClient)r).HSetNX(globalKeysKey, dataKey, data); })); } catch (Exception e) { log.ErrorFormat("could not put in cache : {0}", key); var evtArg = new RedisCacheExceptionEventArgs(e); OnException(evtArg); if (evtArg.Throw) { throw; } } }
public virtual void Lock(object key) { log.DebugFormat("acquiring cache lock : {0}", key); try { var globalKey = CacheNamespace.GlobalKey(key, RedisNamespace.NumTagsForLockKey); using (var client = clientManager.GetClient()) { // Derived from ServiceStack's RedisLock. ExecExtensions.RetryUntilTrue(() => { var wasSet = client.SetEntryIfNotExists(globalKey, "lock " + DateTime.UtcNow.ToUnixTime()); if (wasSet) { acquiredLocks[key] = globalKey; } return wasSet; }, lockTimeout); } } catch (Exception e) { log.ErrorFormat("could not acquire cache lock : ", key); var evtArg = new RedisCacheExceptionEventArgs(e); OnException(evtArg); if (evtArg.Throw) { throw; } } }
public virtual object Get(object key) { key.ThrowIfNull(); //log.DebugFormat("get from cache : {0}", key); try { var globalKeysKey = CacheNamespace.GetGlobalKeysKey(); byte[] data = null; ExecuteEnsureGeneration(transaction => transaction.QueueCommand(r => { var cacheKey = serializer.Serialize(CacheNamespace.GlobalCacheKey(key)); return ((IRedisNativeClient)r).HGet(globalKeysKey, cacheKey); }, x => data = x)); return serializer.Deserialize(data); } catch (Exception e) { log.ErrorFormat("coult not get from cache : {0}", key); var evtArg = new RedisCacheExceptionEventArgs(e); OnException(evtArg); if (evtArg.Throw) { throw; } return null; } }