Ejemplo n.º 1
0
        private void LoadDefaultSettings()
        {
            QueryLevelCachePolicyElement queryLevelCachePolicy = Application.Instance.CachePolicy.APILevelCaching.GetEffectivePolicy();
            int seconds = queryLevelCachePolicy.ExpirationTime;

            if (queryLevelCachePolicy.ExpirationType == CachePolicy.Expirations.Sliding)
            {
                _resultItem.SlidingExpiration = new TimeSpan(0, 0, seconds);
            }
            else if (queryLevelCachePolicy.ExpirationType == CachePolicy.Expirations.Absolute)
            {
                _resultItem.AbsoluteExpiration = DateTime.Now.AddSeconds(seconds);
            }
            _resultItem.DbSyncDependency = queryLevelCachePolicy.DbSyncDependency;
            _resultItem.TargetDatabase   = Application.Instance.CachePolicy.DatabaseType;
        }
Ejemplo 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);
        }