Exemple #1
0
        public IMethodInvocationResult Intercept(ISyncMethodInvocation methodInvocation)
        {
            IMethodInvocationResult result = null;

            CacheAttribute attribute = methodInvocation.InstanceMethodInfo.GetCustomAttribute <CacheAttribute>();

            if (attribute != null)
            {
                string key  = KeyBuilder.BuildCacheKey(attribute.CacheSetting, attribute.CacheName, methodInvocation.TargetInstance.ToString(), methodInvocation.InstanceMethodInfo.Name, attribute.PropertyName, methodInvocation.Arguments.ToArray());
                string area = KeyBuilder.BuildCacheKey(attribute.CacheSetting, attribute.CacheArea, methodInvocation.TargetInstance.ToString(), methodInvocation.InstanceMethodInfo.Name, attribute.PropertyName, methodInvocation.Arguments.ToArray());

                CachedItemDTO cachedItem = CacheManager.GetValue(area, key);

                object cachedValue = null;

                if (cachedItem != null)
                {
                    cachedValue = cachedItem.CacheValue;
                }

                if (cachedValue != null)
                {
                    MethodInfo methodInfo = methodInvocation.InstanceMethodInfo;
                    Type       typeFoo    = methodInfo.ReturnType;

                    var returnData = Convert.ChangeType(cachedValue, typeFoo);

                    return(methodInvocation.CreateResult(returnData));
                }
                else
                {
                    result = methodInvocation.InvokeNext();

                    CacheManager.AddValue(area, key, result.ReturnValue, attribute.TimeoutSeconds);

                    return(result);
                }
            }
            else
            {
                result = methodInvocation.InvokeNext();
                return(result);
            }
        }
Exemple #2
0
        public static void AddValue(string area, string key, object obj, int timeoutSeconds = 2630000)
        {
            try
            {
                if (obj == null)
                {
                    return;
                }

                var cachedItem = new CachedItemDTO()
                {
                    SetTime        = DateTime.UtcNow,
                    ExpirationTime = DateTime.UtcNow + TimeSpan.FromSeconds(timeoutSeconds),
                    TimeoutSeconds = timeoutSeconds,
                    CacheValue     = obj,
                };

                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                        .SetSlidingExpiration(TimeSpan.FromSeconds(timeoutSeconds))
                                        .SetPriority(CacheItemPriority.High)
                                        .RegisterPostEvictionCallback(
                    (echoKey, value, reason, substate) =>
                {
                    // Triggered when this cache item is evicted.
                    // Add Log
                });

                CacheInstance.Set(key, cachedItem, cacheEntryOptions);

                AddCacheArea(new CacheAreaDTO(area, key));
            }
            catch (Exception ex)
            {
                throw;
            }
        }