/// <summary> /// Returns previously cached response or invokes method and caches response /// </summary> /// <param name="input"></param> /// <param name="getNext"></param> /// <returns></returns> public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) { //if caching is disabled, leave: if (!CacheConfiguration.Current.Enabled) { return(Proceed(input, getNext)); } //get the cache settings from the attribute & config: var cacheAttribute = GetCacheSettings(input); if (cacheAttribute.Disabled) { return(Proceed(input, getNext)); } //if there's no cache provider, leave: var cache = Cache.Get(cacheAttribute.CacheType); var serializer = Serializer.GetCurrent(cacheAttribute.SerializationFormat); if (cache == null || cache.CacheType == CacheType.Null || serializer == null) { return(Proceed(input, getNext)); } // Log cache request here var returnType = ((MethodInfo)input.MethodBase).ReturnType; var cacheKey = CacheKeyBuilder.GetCacheKey(input, serializer); var cachedValue = cache.Get(returnType, cacheKey, cacheAttribute.SerializationFormat); if (cachedValue == null) { // missed the cache // Log here for instrumentation //call the intended method to set the return value var methodReturn = Proceed(input, getNext); //only cache if we have a real return value & no exception: if (methodReturn != null && methodReturn.ReturnValue != null && methodReturn.Exception == null) { var lifespan = cacheAttribute.Lifespan; if (lifespan.TotalSeconds > 0) { cache.Set(cacheKey, methodReturn.ReturnValue, lifespan, cacheAttribute.SerializationFormat); } else { cache.Set(cacheKey, methodReturn.ReturnValue, cacheAttribute.SerializationFormat); } } return(methodReturn); } else { // hit the cache // Log here for instrumentation } return(input.CreateMethodReturn(cachedValue)); }
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { string key = CacheKeyBuilder.GetCacheKey(input, new BinarySerializer()); key = key.Replace(".", string.Empty); if (Defaults.TipoInicio == 4 && IsInCache(key + "return")) { var res = FetchFromCache(key + "return"); GetArgumentToCache(input, key); return(input.CreateMethodReturn(res, GetArgument(input))); } IMethodReturn methodReturn = getNext()(input, getNext); if (!input.MethodBase.ToString().Contains("System.Threading.Task") && Utility.ReflectionHelper.IsSerializable(input.MethodBase) && Defaults.HabilitarDistribuidorCache == "1") { if (methodReturn != null && methodReturn.ReturnValue != null && methodReturn.Exception == null) { AddToCache(key + "return", methodReturn); } AddArgumentToCache(input, key); } return(methodReturn); }
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { #region Check cache is enabled if (!CacheConfiguration.Current.Enabled) { return(Proceed(input, getNext)); } #endregion #region NoCache is checking var nocache = input.MethodBase.GetCustomAttributes(typeof(NoCacheAttribute), false); if (nocache.Count() != 0) { return(Proceed(input, getNext)); } #endregion #region Instanciate AppFabric Cache. if it is no working, return. var cache = Caching.Cache.AppFabric; if (cache == null /* or cache is disabled inside the webconfig*/) { return(Proceed(input, getNext)); } #endregion ExpireRegions(input, cache); #region Check Void Methods if (((MethodInfo)input.MethodBase).ReturnType.Name == "Void") // specific behavior for Void Methods { return(Proceed(input, getNext)); } #endregion #region Check the Cache string cacheKey = CacheKeyBuilder.GetCacheKey(input); string region = CacheKeyBuilder.GetRegion(input); var cachedValue = cache.Get(cacheKey, region); if (cachedValue == null) { var methodReturn = Proceed(input, getNext); if (methodReturn != null && methodReturn.ReturnValue != null && methodReturn.Exception == null) { cache.Set(cacheKey, region, methodReturn.ReturnValue); } return(methodReturn); } else { return(input.CreateMethodReturn(cachedValue)); } #endregion }
public override void Set(string key, object value, DateTime expiresAt) { try { var itemBytes = Serializer.Binary.Serialize(value) as byte[]; var item = new CacheItem() { ItemBytes = itemBytes }; var serializedItem = Serializer.Json.Serialize(item); var cacheKey = CacheKeyBuilder.GetCacheKey(key); Current.SetInternal(cacheKey, serializedItem, expiresAt); } catch (Exception ex) { //Log.Warn("CacheBase.Set - failed, item not cached. Message: {0}", ex.Message); } }
public void GetCacheKey_MethodInvocation_CustomPrefix() { var invocation = CustomKeyPrefixMethodInvocationStub.GetMock(); var key1 = CacheKeyBuilder.GetCacheKey(invocation); Assert.IsTrue(key1.StartsWith("*CustomKeyPrefix*")); AssertGuid(key1, true); //same input should generate same key: var key1v2 = CacheKeyBuilder.GetCacheKey(invocation); Assert.AreEqual(key1, key1v2); //different input should generate different key: invocation = CustomKeyPrefixMethodInvocationStub.GetMock(); var key2 = CacheKeyBuilder.GetCacheKey(invocation); Assert.IsTrue(key2.StartsWith("*CustomKeyPrefix*")); AssertGuid(key2, true); Assert.AreNotEqual(key1, key2); }
public void GetCacheKey() { var format = "{0}_{1}"; var p1 = Guid.NewGuid().ToString(); var p2 = _random.Next(); var key1 = CacheKeyBuilder.GetCacheKey(format, p1, p2); AssertGuid(key1); //same input should generate same key: var key1v2 = CacheKeyBuilder.GetCacheKey(format, p1, p2); Assert.AreEqual(key1, key1v2); //different input should generate different key: var p3 = _random.Next(); var key2 = CacheKeyBuilder.GetCacheKey(format, p1, p3); AssertGuid(key2); Assert.AreNotEqual(key1, key2); }
public override object Get(string key) { var cacheKey = CacheKeyBuilder.GetCacheKey(key); object item = null; try { var serializedItem = Current.GetInternal(cacheKey); if (serializedItem != null) { var cacheItem = Serializer.Json.Deserialize(typeof(CacheItem), serializedItem) as CacheItem; item = Serializer.Binary.Deserialize(null, cacheItem.ItemBytes); } } catch (Exception ex) { //Log.Warn("CacheBase.Get - failed, item not cached. Message: {0}", ex.Message); } return(item); }
/// <inheritdoc /> /// <summary> /// Returns previously cached response or invokes method and caches response /// </summary> /// <param name="method"></param> /// <param name="getNext"></param> /// <returns></returns> public IMethodReturn Invoke(IMethodInvocation method, GetNextHandlerDelegate getNext) { //if caching is disabled, reset all cache and leave: if (!CacheConfiguration.Current.Enabled) { if (!CachesWereReset) { ClearAllCaches(); } return(Proceed(method, getNext)); } //get the cache settings from the attribute & config: var cacheAttribute = GetCacheSettings(method); var serializer = Serializer.GetCurrent(cacheAttribute.SerializationFormat); var cacheKey = CacheKeyBuilder.GetCacheKey(method, serializer); var cache = Caching.Cache.Get(cacheAttribute.CacheType); //if method cache disabled, remove data and leave if (cacheAttribute.Disabled) { cache?.Remove(cacheKey); return(Proceed(method, getNext)); } //if there's no cache provider, leave: if (cache == null || cache.CacheType == CacheType.Null || serializer == null) { return(Proceed(method, getNext)); } var targetCategory = InstrumentCacheRequest(method); var returnType = ((MethodInfo)method.MethodBase).ReturnType; var cachedValue = cache.Get(cacheKey); if (cachedValue == null) { InstrumentCacheMiss(targetCategory, method); //call the intended method to set the return value var methodReturn = Proceed(method, getNext); //only cache if we have a real return value & no exception: if (methodReturn == null || methodReturn.ReturnValue == null || methodReturn.Exception != null) { return(methodReturn); } cachedValue = serializer.Serialize(methodReturn.ReturnValue); StoreCacheValue(cache, cacheKey, cachedValue, cacheAttribute.Lifespan); SetupChangeTracking(method, cacheAttribute.ChangeTrackingDictionary , cacheKey, cache.CacheType); return(methodReturn); } InstrumentCacheHit(targetCategory, method); var returnValue = serializer.Deserialize(returnType, cachedValue); return(method.CreateMethodReturn(returnValue)); }