Ejemplo n.º 1
0
        /// <summary>
        /// Resolve cache key
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="invocationContext"></param>
        /// <returns></returns>
        public virtual string GetCacheKey(_IInvocation invocation, IDictionary <string, object> invocationContext)
        {
            var info = invocationContext.TryGetByKey <ICacheSettings>(Global.__flatwhite_outputcache_attribute);

            if (info == null)
            {
                throw new InvalidOperationException($"{nameof(ICacheSettings)} object not found in {nameof(invocationContext)}");
            }

            // The cache key must be different for different instance of same type
            var key = new StringBuilder($"Flatwhite::{(invocation.Method.DeclaringType ?? invocation.TargetType).FullName}.{invocation.Method.Name}(");

            var varyByParams  = (info.VaryByParam ?? "").Split(new [] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            var varyByCustoms = info.GetAllVaryCustomKey().Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);

            var parameters = invocation.Method.GetParameters();

            if (parameters.Length > 0)
            {
                BuildWithParams(invocation, parameters, varyByParams, key);
                key.Remove(key.Length - 2, 2);
            }

            key.Append(") :: ");

            if (varyByCustoms.Length > 0)
            {
                foreach (var custom in varyByCustoms)
                {
                    BuildWithCustom("", invocationContext, custom, key);
                    key.Append(", ");
                }
            }
            return(key.ToString().TrimEnd(' ', ':', ','));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get <see cref="IAsyncCacheStore" /> for current invocation and context
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="invocationContext"></param>
        /// <returns></returns>
        public virtual IAsyncCacheStore GetAsyncCacheStore(_IInvocation invocation, IDictionary<string, object> invocationContext)
        {
            var att = invocationContext.TryGetByKey<ICacheSettings>(Global.__flatwhite_outputcache_attribute, OutputCacheAttribute.Default);
            if (att.CacheStoreId > 0)
            {
                var asyncCacheStore = Global.CacheStoreProvider.GetAsyncCacheStore(att.CacheStoreId);
                if (asyncCacheStore != null) return asyncCacheStore;
            }

            if (att.CacheStoreType != null && typeof(IAsyncCacheStore).IsAssignableFrom(att.CacheStoreType))
            {
                try
                {
                    return _activator.CreateInstance(att.CacheStoreType) as IAsyncCacheStore ?? Global.CacheStoreProvider.GetAsyncCacheStore(att.CacheStoreType);
                }
                catch (KeyNotFoundException)
                {
                }
            }

            if (att.CacheStoreType != null && typeof(ICacheStore).IsAssignableFrom(att.CacheStoreType))
            {
                try
                {
                    var syncCacheStore = _activator.CreateInstance(att.CacheStoreType) as ICacheStore ?? Global.CacheStoreProvider.GetCacheStore(att.CacheStoreType);
                    if (syncCacheStore != null) return new CacheStoreAdaptor(syncCacheStore);
                }
                catch (KeyNotFoundException)
                {
                }
            }
            return Global.CacheStoreProvider.GetAsyncCacheStore();
        }
        /// <summary>
        /// Resolve cache key
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="invocationContext"></param>
        /// <returns></returns>
        public virtual string GetCacheKey(_IInvocation invocation, IDictionary<string, object> invocationContext)
        {
            var info = invocationContext.TryGetByKey<ICacheSettings>(Global.__flatwhite_outputcache_attribute);
            if (info == null)
            {
                throw new InvalidOperationException($"{nameof(ICacheSettings)} object not found in {nameof(invocationContext)}");
            }

            // The cache key must be different for different instance of same type
            var key = new StringBuilder($"Flatwhite::{(invocation.Method.DeclaringType ?? invocation.TargetType).FullName}.{invocation.Method.Name}(");
            
            var varyByParams = (info.VaryByParam ?? "").Split(new [] {',',' '}, StringSplitOptions.RemoveEmptyEntries);
            var varyByCustoms = info.GetAllVaryCustomKey().Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);

            var parameters = invocation.Method.GetParameters();
            if (parameters.Length > 0)
            {
                BuildWithParams(invocation, parameters, varyByParams, key);
                key.Remove(key.Length - 2, 2);
            }
            
            key.Append(") :: ");

            if (varyByCustoms.Length > 0)
            {
                foreach (var custom in varyByCustoms)
                {
                    BuildWithCustom("", invocationContext, custom, key);
                    key.Append(", ");
                }
            }
            return key.ToString().TrimEnd(' ', ':', ',');
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get empty list change monitor
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="invocationContext"></param>
        /// <returns></returns>
        public virtual IEnumerable <IChangeMonitor> GetChangeMonitors(_IInvocation invocation, IDictionary <string, object> invocationContext)
        {
            var att = invocationContext.TryGetByKey <ICacheSettings>(Global.__flatwhite_outputcache_attribute, OutputCacheAttribute.Default);

            if (string.IsNullOrWhiteSpace(att?.RevalidateKeyFormat))
            {
                yield break;
            }

            var revalidationKey = CacheKeyProvider.GetRevalidateKey(invocation, att.RevalidateKeyFormat);

            yield return(new FlatwhiteCacheEntryChangeMonitor(revalidationKey));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Get <see cref="IAsyncCacheStore" /> for current invocation and context
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="invocationContext"></param>
        /// <returns></returns>
        public virtual IAsyncCacheStore GetAsyncCacheStore(_IInvocation invocation, IDictionary <string, object> invocationContext)
        {
            var att = invocationContext.TryGetByKey <ICacheSettings>(Global.__flatwhite_outputcache_attribute, OutputCacheAttribute.Default);

            if (att.CacheStoreId > 0)
            {
                try
                {
                    var asyncCacheStore = Global.CacheStoreProvider.GetAsyncCacheStore(att.CacheStoreId);
                    if (asyncCacheStore != null)
                    {
                        return(asyncCacheStore);
                    }
                }
                catch (Exception ex)
                {
                    Global.Logger.Error($"Cannot resolve cache store with id {att.CacheStoreId}", ex);
                }
            }

            if (att.CacheStoreType != null && typeof(IAsyncCacheStore).IsAssignableFrom(att.CacheStoreType))
            {
                try
                {
                    var asyncCacheStore = Global.CacheStoreProvider.GetAsyncCacheStore(att.CacheStoreType);
                    if (asyncCacheStore != null)
                    {
                        return(asyncCacheStore);
                    }
                }
                catch (KeyNotFoundException)
                {
                }
            }

            if (att.CacheStoreType != null && typeof(ICacheStore).IsAssignableFrom(att.CacheStoreType))
            {
                try
                {
                    var syncCacheStore = Global.CacheStoreProvider.GetCacheStore(att.CacheStoreType);
                    if (syncCacheStore != null)
                    {
                        return(new CacheStoreAdaptor(syncCacheStore));
                    }
                }
                catch (KeyNotFoundException)
                {
                }
            }
            return(Global.CacheStoreProvider.GetAsyncCacheStore());
        }
 /// <summary>
 /// Get <see cref="ICacheStore" /> for current invocation and context
 /// </summary>
 /// <param name="invocation"></param>
 /// <param name="invocationContext"></param>
 /// <returns></returns>
 public virtual ICacheStore GetCacheStore(_IInvocation invocation, IDictionary<string, object> invocationContext)
 {
     var att = invocationContext.TryGetByKey<ICacheSettings>(Global.__flatwhite_outputcache_attribute, OutputCacheAttribute.Default);
     ICacheStore cacheStore = null;
     try
     {
         if (att.CacheStoreId > 0)
         {
             cacheStore = Global.CacheStoreProvider.GetCacheStore(att.CacheStoreId); 
         }
     
         if (cacheStore == null && att.CacheStoreType != null && typeof (ICacheStore).IsAssignableFrom(att.CacheStoreType))
         {
             cacheStore = Global.CacheStoreProvider.GetCacheStore(att.CacheStoreType);
         }
     }
     catch (KeyNotFoundException)
     {
     }
     return cacheStore ?? Global.CacheStoreProvider.GetCacheStore();
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Get <see cref="ICacheStore" /> for current invocation and context
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="invocationContext"></param>
        /// <returns></returns>
        public virtual ICacheStore GetCacheStore(_IInvocation invocation, IDictionary <string, object> invocationContext)
        {
            var         att        = invocationContext.TryGetByKey <ICacheSettings>(Global.__flatwhite_outputcache_attribute, OutputCacheAttribute.Default);
            ICacheStore cacheStore = null;

            try
            {
                if (att.CacheStoreId > 0)
                {
                    cacheStore = Global.CacheStoreProvider.GetCacheStore(att.CacheStoreId);
                }

                if (cacheStore == null && att.CacheStoreType != null && typeof(ICacheStore).IsAssignableFrom(att.CacheStoreType))
                {
                    cacheStore = Global.CacheStoreProvider.GetCacheStore(att.CacheStoreType);
                }
            }
            catch (KeyNotFoundException)
            {
            }
            return(cacheStore ?? Global.CacheStoreProvider.GetCacheStore());
        }
        /// <summary>
        /// Get <see cref="IAsyncCacheStore" /> for current invocation and context
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="invocationContext"></param>
        /// <returns></returns>
        public virtual IAsyncCacheStore GetAsyncCacheStore(_IInvocation invocation, IDictionary<string, object> invocationContext)
        {
            var att = invocationContext.TryGetByKey<ICacheSettings>(Global.__flatwhite_outputcache_attribute, OutputCacheAttribute.Default);
            if (att.CacheStoreId > 0)
            {
                try
                {
                    var asyncCacheStore = Global.CacheStoreProvider.GetAsyncCacheStore(att.CacheStoreId);
                    if (asyncCacheStore != null) return asyncCacheStore;
                }
                catch (Exception ex)
                {
                    Global.Logger.Error($"Cannot resolve cache store with id {att.CacheStoreId}", ex);
                }
            }

            if (att.CacheStoreType != null && typeof(IAsyncCacheStore).IsAssignableFrom(att.CacheStoreType))
            {
                try
                {
                    var asyncCacheStore = Global.CacheStoreProvider.GetAsyncCacheStore(att.CacheStoreType);
                    if (asyncCacheStore != null) return asyncCacheStore;
                }
                catch (KeyNotFoundException)
                {
                }
            }

            if (att.CacheStoreType != null && typeof(ICacheStore).IsAssignableFrom(att.CacheStoreType))
            {
                try
                {
                    var syncCacheStore = Global.CacheStoreProvider.GetCacheStore(att.CacheStoreType);
                    if (syncCacheStore != null) return new CacheStoreAdaptor(syncCacheStore);
                }
                catch (KeyNotFoundException)
                {
                }
            }
            return Global.CacheStoreProvider.GetAsyncCacheStore();
        }
        /// <summary>
        /// Get empty list change monitor
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="invocationContext"></param>
        /// <returns></returns>
        public virtual IEnumerable<IChangeMonitor> GetChangeMonitors(_IInvocation invocation, IDictionary<string, object> invocationContext)
        {
            var att = invocationContext.TryGetByKey<ICacheSettings>(Global.__flatwhite_outputcache_attribute, OutputCacheAttribute.Default);
            if (string.IsNullOrWhiteSpace(att?.RevalidateKeyFormat))
            {
                yield break;
            }

            var revalidationKey = CacheKeyProvider.GetRevalidateKey(invocation, att.RevalidateKeyFormat);
            yield return new FlatwhiteCacheEntryChangeMonitor(revalidationKey);
        }