Esempio n. 1
0
        /// <summary>
        /// Get expirations
        /// </summary>
        /// <param name="absoluteExpiration"></param>
        /// <param name="slidingExpiration"></param>
        private void GetExpirations(Alachisoft.NCache.Integrations.EntityFramework.Config.CachePolicy policy, out DateTime absoluteExpiration, out TimeSpan slidingExpiration)
        {
            absoluteExpiration = Cache.NoAbsoluteExpiration;
            slidingExpiration  = Cache.NoSlidingExpiration;

            switch (policy.ExpirationType)
            {
            case CachePolicy.Expirations.Absolute:
                absoluteExpiration = DateTime.Now.AddSeconds(policy.ExpirationTime);
                break;

            case CachePolicy.Expirations.Sliding:
                slidingExpiration = new TimeSpan(0, 0, policy.ExpirationTime);
                break;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Get policy settings for specified query
        /// </summary>
        /// <param name="query">Query command</param>
        /// <param name="absoluteExpiration">Absolute expiration value as specified in config</param>
        /// <param name="slidingExpiration">Sliding expiration value as specified in config</param>
        /// <param name="dbType">Target database type</param>
        /// <param name="dbSyncDependency">Indicate whether database sycn dependency is enabled or not</param>
        /// <returns>True if caching is enabled for the selected policy, false otherwise</returns>
        public bool GetEffectivePolicy(string query, out DateTime absoluteExpiration, out TimeSpan slidingExpiration, out CachePolicyElement.DatabaseType dbType, out bool dbSyncDependency, out List <string> parameterList)
        {
            absoluteExpiration = Cache.NoAbsoluteExpiration;
            slidingExpiration  = Cache.NoSlidingExpiration;
            dbType             = CachePolicyElement.DatabaseType.None;
            dbSyncDependency   = false;
            parameterList      = null;
            bool cacheable = false;

            if (query == null)
            {
                return(cacheable);
            }

            try
            {
                this.rwLock.AcquireReaderLock(Timeout.Infinite);

                Alachisoft.NCache.Integrations.EntityFramework.Config.CachePolicy selected = null;

                QueryPolicyElement queryPolicyElement = this.GetQueryElement(this.cachePolicy.Query, query);
                if (queryPolicyElement != null)
                {
                    selected = queryPolicyElement.CachePolicy;
                    if (selected != null)
                    {
                        parameterList = ((QueryCachePolicyElement)selected).CacheableParameterList;
                    }
                    else
                    {
                        Logger.Instance.TraceError("No cache policy found for the query " + query + ". Not Cacheable.");
                    }
                }

                if (selected == null)
                {
                    if (this.cachePolicy.CacheAllQueries)
                    {
                        //   absoluteExpiration=this.GetQueryElement.
                        QueryLevelCachePolicyElement queryLevelCachePolicy = this.cachePolicy.APILevelCaching;
                        dbType           = this.cachePolicy.Database;
                        cacheable        = true;
                        dbSyncDependency = queryLevelCachePolicy.DbSyncDependency;
                        if (queryLevelCachePolicy.ExpirationType == EntityFramework.Config.CachePolicy.Expirations.Absolute)
                        {
                            absoluteExpiration = DateTime.Now.AddSeconds(queryLevelCachePolicy.ExpirationTime);
                        }
                        else
                        {
                            slidingExpiration = new TimeSpan(0, 0, queryLevelCachePolicy.ExpirationTime);
                        }
                    }
                    return(cacheable);
                }

                dbType           = this.cachePolicy.Database;
                cacheable        = selected.Enabled;
                dbSyncDependency = selected.DbSyncDependency;
                this.GetExpirations(selected, out absoluteExpiration, out slidingExpiration);
            }
            catch (Exception ex)
            {
                cacheable = false;
            }
            finally
            {
                this.rwLock.ReleaseReaderLock();
            }

            return(cacheable);
        }