private void CacheIntercept(CachingMethod method, IInvocation invocation, string key, SectionType type, int time)
        {
            switch (method)
            {
            case CachingMethod.Get:
            {
                var cacheObj = CacheContainer.GetInstances <ICacheProvider>(CacheTargetType.WebCache.ToString());
                var list     = cacheObj.Get(key);
                if (list == null)
                {
                    invocation.Proceed();
                    list = invocation.ReturnValue;
                    if (list != null)
                    {
                        cacheObj.Add(key, list, time);
                    }
                }
                invocation.ReturnValue = list;
                break;
            }

            case CachingMethod.Put:
            {
                break;
            }

            case CachingMethod.Remove:
            {
                break;
            }
            }
        }
Esempio n. 2
0
 public ModifiableStream(Stream source, CachingMethod cachingMethod, long maximumCacheSize)
 {
     ValidateStream(source);
     Source            = source;
     _CachingMethod    = cachingMethod;
     _Length           = source.Length;
     _MaximumCacheSize = maximumCacheSize;
     _Position         = source.Position;
     Segments          = new LinkedList <Segment>();
     CachedData        = new List <StreamCache>();
     InitFirstSegment();
 }
        public static ServiceDescriptor  Method(this ServiceDescriptor descriptor, CachingMethod cachingMethod, string metadataId)
        {
            var metadata       = GetInterceptMetadata(descriptor, metadataId);
            var iCachingMethod = Convert.ToInt32(cachingMethod).ToString();

            if (string.IsNullOrEmpty(metadata.Item1))
            {
                metadata.Item1 = iCachingMethod;
            }
            else
            {
                metadata.Item1 += $"|{iCachingMethod}";
            }
            metadata.Item2[metadataId]        = metadata.Item1;
            descriptor.Metadatas["Intercept"] = metadata.Item2;
            return(descriptor);
        }
        private void RedisIntercept(CachingMethod method, IInvocation invocation, string key, SectionType type, int time)
        {
            switch (method)
            {
            case CachingMethod.Get:
            {
                var list     = default(object);
                var cacheObj = CacheContainer.GetInstances <ICacheProvider>(string.Format("{0}.{1}", type.ToString(), CacheTargetType.Redis.ToString()));
                var json     = cacheObj.Get <string>(key);
                if (string.IsNullOrEmpty(json))
                {
                    invocation.Proceed();
                    list = invocation.ReturnValue;
                    if (list != null)
                    {
                        json = JsonConvert.SerializeObject(list);
                        cacheObj.Add(key, json, time);
                    }
                }
                else
                {
                    list = JsonConvert.DeserializeObject(json, invocation.Method.ReturnType);
                }
                invocation.ReturnValue = list;
                break;
            }

            case CachingMethod.Put:
            {
                break;
            }

            case CachingMethod.Remove:
            {
                break;
            }
            }
        }
Esempio n. 5
0
 /// <summary>
 /// 初始化一个新的<c>InterceptMethodAttribute</c>类型。
 /// </summary>
 /// <param name="method">缓存方式。</param>
 /// <param name="correspondingMethodNames">与当前缓存方式相关的方法名称。注:此参数仅在缓存方式为Remove时起作用。</param>
 public ServiceCacheIntercept(CachingMethod method, params string[] correspondingMethodNames)
     : this(method)
 {
     CorrespondingKeys = correspondingMethodNames;
 }
Esempio n. 6
0
 public CacheAttribute(CachingMethod method, string cacheName)
 {
     CacheName = cacheName;
     Method = method;
 }
Esempio n. 7
0
 /// <summary>
 /// 初始化一个新的<c>InterceptMethodAttribute</c>类型。
 /// </summary>
 /// <param name="method">缓存方式。</param>
 public ServiceCacheIntercept(CachingMethod method)
 {
     Method = method;
 }
 /// <summary>
 /// 初始化一个新的<c>CachingAttribute</c>类型。
 /// </summary>
 /// <param name="method">缓存方式。</param>
 /// <param name="correspondingMethodNames">与当前缓存方式相关的方法名称。注:此参数仅在缓存方式为Remove时起作用。</param>
 public CachingAttribute(CachingMethod method, params string[] correspondingMethodNames)
     : this(method)
 {
     this.CorrespondingMethodNames = correspondingMethodNames;
 }
Esempio n. 9
0
 public EntLibCacheAttribute(CachingMethod method)
 {
     this.CacheMethod = method;
 }
Esempio n. 10
0
 /// <summary>
 /// Parameterized Constructor.
 /// </summary>
 public CachingAttribute(CachingMethod method, params string[] relatedAreas)
 {
     this.Method       = method;
     this.RelatedAreas = relatedAreas;
 }
Esempio n. 11
0
 /// <summary>
 /// 初始化一个新的<c>CachingAttribute</c>类型。
 /// </summary>
 /// <param name="method">缓存方式。</param>
 /// <param name="correspondingMethodNames">与当前缓存方式相关的方法名称。注:此参数仅在缓存方式为Remove时起作用。</param>
 public CachingAttribute(CachingMethod method, params string[] correspondingMethodNames)
     : this(method)
 {
     this.CorrespondingMethodNames = correspondingMethodNames;
 }
 public CachingCallHandler(int hours = 0, int minutes = 0, int seconds = 0, bool cachingenable = true,
     CachingMethod cachingmethod = CachingMethod.Get, bool force = false, string[] correspondingmethodnames = null)
 {
     Hours = hours;
     Minutes = minutes;
     Seconds = seconds;
     CachingEnable = cachingenable;
     CachingMethod = cachingmethod;
     Force = force;
     CorrespondingMethodNames = correspondingmethodnames;
 }
Esempio n. 13
0
 /// <summary>
 /// 初始化一个新的<c>CachingAttribute</c>类型。
 /// </summary>
 /// <param name="method">缓存方式。</param>
 /// <param name="value">缓存的key</param>
 public CachingAttribute(CachingMethod method, string value, bool isAll = false)
     : this(method)
 {
     this.value = value;
     this.isAll = isAll;
 }
Esempio n. 14
0
 /// <summary>
 /// 初始化一个新的<c>CachingAttribute</c>类型。
 /// </summary>
 /// <param name="method">缓存方式。</param>
 /// <param name="cacheType">缓存类型</param>
 public CachingAttribute(CachingMethod method,Type cacheType)
 {
     Method = method;
     CacheType = cacheType;
 }
Esempio n. 15
0
        // Main implementation
        private static T FromCacheImplementation <T>(CachingMethod cachingMethod, QueryDeferred <T> query, out string cacheKey, CachingOptions options)
        {
            Logger.Log(
                "Performing " + cachingMethod + " for " + query.ToString() + " with options " + options.ToLog() + ".",
                Microsoft.Extensions.Logging.LogLevel.Trace
                );
            options = (CachingOptions)options.Clone();
            // Always store as collection
            options.StoreAs = StoreAs.Collection;

            bool        cacheHit    = false;
            IDictionary cacheResult = default(Hashtable);

            cacheKey = QueryCacheManager.GetQueryCacheKey(query.Query, options.QueryIdentifier);

            // If user has specified tag, leave it as it is
            // Otherwise overwrite it with 'cacheKey'
            options.QueryIdentifier = options.QueryIdentifier ?? cacheKey;

            /* NOTE: If user stored result with a tag and is trying to query
             *       it without the tag, it's a different query so don't
             *       worry about that.
             */

            // Get result into 'cacheResult' hashtable if it exists
            if (cachingMethod == CachingMethod.FromCache)
            {
                // Get by the tag (more reliable)
                cacheHit = QueryCacheManager.Cache.GetByKey(options.QueryIdentifier, out cacheResult);
            }
            // If result wasn't found OR result was meant to be stored fresh
            if (cachingMethod == CachingMethod.LoadIntoCache || !cacheHit)
            {
                CacheDependency dbDependency = null;

                if (options.CreateDbDependency)
                {
                    IRelationalCommand command = query.Query.CreateCommand(out RelationalQueryContext queryContext);

                    string connectionString = queryContext.Connection.ConnectionString;

                    dbDependency = GetDependency(NCacheConfiguration.DatabaseType, command.CommandText, connectionString);
                }

                object item = query.Execute();

                QueryCacheManager.Cache.SetAsCacheEntry(cacheKey, item ?? Null.Value, options, dbDependency);

                return(item == null ? default(T) : (T)item);
            }
            // If result was meant to be fetched instead of stored fresh AND it was found (somewhat)
            else
            {
                object returnVal = default(T);

                if (cacheResult != default(Hashtable))
                {
                    returnVal = cacheResult.Values.Cast <CacheEntry>().FirstOrDefault().Value;
                }
                return(returnVal != null ? (returnVal is Null ? default(T) : (T)returnVal) : default(T));
            }
        }
Esempio n. 16
0
 /// <summary>
 /// Parameterized Constructor.
 /// </summary>
 public CachingAttribute(CachingMethod method, params string[] relatedAreas)
 {
     this.Method = method;
     this.RelatedAreas = relatedAreas;
 }
Esempio n. 17
0
 /// <summary>
 /// Parameterized Constructor.
 /// </summary>
 public CachingAttribute(CachingMethod method)
     : this(method, new string[0])
 {
 }
Esempio n. 18
0
 public EntLibCacheAttribute(CachingMethod method, params string[] methodNames) : this(method)
 {
     this.CorrespondingMethodNames = methodNames;
 }
Esempio n. 19
0
        // Main implementation
        private static IEnumerable<T> FromCacheImplementation<T>(CachingMethod cachingMethod, IQueryable<T> query, out string cacheKey, CachingOptions options) where T : class
        {
            Logger.Log(
                "Performing " + cachingMethod + " for " + query.ToString() + " with options " + options.ToLog() + ".", LogLevel.Trace
            );

            // Create NCache entry options
            CachingOptions optionsCloned = (CachingOptions)options.Clone();

            cacheKey = null;
            string queryStoreKey = null;

            if (cachingMethod != CachingMethod.LoadIntoCache)
            {
                // Verify if query can be fetched seperately
                string pkCacheKey;
                if (QueryHelper.CanDirectPkFetch(query, optionsCloned, out pkCacheKey))
                {
                    object pkItem;
                    if (QueryCacheManager.Cache.TryGetValue(pkCacheKey, out pkItem))
                    {
                        List<T> resultSetPk = new List<T>();
                        List<T> resultSetPkTracked = new List<T>();
                        var stateManagerPk = query.GetStateManager();

                        resultSetPk.Add((T)pkItem);

                        foreach (var entity in resultSetPk)
                        {
                            resultSetPkTracked.Add(((StateManager)stateManagerPk).GetRefValue(entity));
                        }
                        return resultSetPkTracked;
                    }
                }
            }

            bool cacheHit = false;
            IDictionary cacheResult = null;

            queryStoreKey = QueryCacheManager.GetQueryCacheKey(query, optionsCloned.QueryIdentifier);
            if (optionsCloned.StoreAs == StoreAs.Collection || optionsCloned.QueryIdentifier == null)
            {
                if (optionsCloned.StoreAs == StoreAs.Collection)
                    cacheKey = queryStoreKey;
                if (optionsCloned.QueryIdentifier == null)
                    optionsCloned.QueryIdentifier = queryStoreKey;
            }

            // Check in cache
            if (cachingMethod != CachingMethod.LoadIntoCache)
            {

                cacheHit = QueryCacheManager.Cache.GetByKey(queryStoreKey, out cacheResult);

            }

            // If not found in cache go for db
            if (!cacheHit)
            {
                var enumerableSet = query.AsEnumerable<T>();

                CacheDependency dbDependency = null;
                if (optionsCloned.CreateDbDependency)
                {
                    RelationalQueryContext queryContext = null;
                    IRelationalCommand command = query.CreateCommand(out queryContext);
                    string connectionString = queryContext.Connection.ConnectionString;
                    dbDependency = GetDependency(NCacheConfiguration.DatabaseType, command.CommandText, connectionString);
                }
                return new NCacheEnumerable<T>(queryStoreKey, query, enumerableSet, optionsCloned, dbDependency);
            }
            // data is found in cache return result set
            else
            {
                // Assume its a collection
                if (cacheResult.Count == 1)
                {
                    foreach (var item in cacheResult.Values)
                    {
                        CacheEntry entry = item as CacheEntry;
                        if (entry != null)
                        {
                            // Confirmed stored as collection just return the value after casting
                            IEnumerable<T> resultSetC = (IEnumerable<T>)entry.Value;
                            // [Umer] i know this tracking is costly but there is no other solution
                            var resultSetCTracked = new List<T>();
                            var stateManagerC = query.GetStateManager();
                            foreach (var entity in resultSetC)
                            {
                                resultSetCTracked.Add(((StateManager)stateManagerC).GetRefValue(entity));
                            }
                            return resultSetCTracked;
                        }
                        break;
                    }
                }

                var resultSetSE = cacheResult.Values.Cast<T>();
                // [Umer] i know this tracking is costly but there is no other solution
                var resultSetSETracked = new List<T>();
                var stateManagerSE = query.GetStateManager();
                foreach (var entity in resultSetSE)
                {
                    resultSetSETracked.Add(((StateManager)stateManagerSE).GetRefValue(entity));
                }
                return resultSetSETracked;
            }
        }
Esempio n. 20
0
 public ModifiableStream(Stream source, CachingMethod cachingMethod)
     : this(source, cachingMethod, cachingMethod == CachingMethod.InMemory ? MemoryCache.DefaultMaxBuffer : TempFileCache.DefaultMaxBuffer)
 {
 }
Esempio n. 21
0
 /// <summary>
 /// Parameterized Constructor.
 /// </summary>
 public CachingAttribute(CachingMethod method)
     : this(method, new string[0])
 {
 }
Esempio n. 22
0
 /// <summary>
 /// 初始化一个新的<c>InterceptMethodAttribute</c>类型。
 /// </summary>
 /// <param name="method">缓存方式。</param>
 public InterceptMethodAttribute(CachingMethod method)
 {
     this._method = method;
 }
Esempio n. 23
0
 /// <summary>
 /// 初始化一个新的<c>CachingAttribute</c>类型。
 /// </summary>
 /// <param name="method">缓存方式。</param>
 public CachingAttribute(CachingMethod method)
 {
     this.Method = method;
 }
Esempio n. 24
0
 public ModifiableStream(CachingMethod cachingMethod, long maximumCacheSize)
     : this(new MemoryStream(), cachingMethod, maximumCacheSize)
 {
     SourceOwner = true;
 }
Esempio n. 25
0
 /// <summary>
 /// 初始化一个新的<c>CachingAttribute</c>类型。
 /// </summary>
 /// <param name="method">缓存方式。</param>
 public CachingAttribute(CachingMethod method)
 {
     this.Method = method;
 }
Esempio n. 26
0
 public CacheAttribute(CachingMethod method)
 {
     CacheName = "redis";
     Method = method;
 }
Esempio n. 27
0
 /// <summary>
 /// 初始化缓存拦截器
 /// </summary>
 /// <param name="cachingMethod"></param>
 public CachingAspectAttribute(CachingMethod cachingMethod, params string[] invalidKeyList)
 {
     this.cachingMethod  = cachingMethod;
     this.invalidKeyList = invalidKeyList;
 }
Esempio n. 28
0
 /// <summary>
 /// 初始化缓存拦截器
 /// </summary>
 /// <param name="cachingMethod"></param>
 public CachingAspectAttribute(CachingMethod cachingMethod)
 {
     this.cachingMethod = cachingMethod;
 }
Esempio n. 29
0
 /// <summary>
 /// 初始化一个新的<c>InterceptMethodAttribute</c>类型。
 /// </summary>
 /// <param name="method">缓存方式。</param>
 /// <param name="correspondingMethodNames">与当前缓存方式相关的方法名称。注:此参数仅在缓存方式为Remove时起作用。</param>
 public InterceptMethodAttribute(CachingMethod method, params string[] correspondingMethodNames)
     : this(method)
 {
     this._correspondingKeys = correspondingMethodNames;
 }
Esempio n. 30
0
 public ModifiableStream(CachingMethod cachingMethod) : this(new MemoryStream(), cachingMethod)
 {
     SourceOwner = true;
 }