/// <summary>
        /// Configure the cache
        /// </summary>
        /// <param name="regionName">the name of the cache region</param>
        /// <param name="properties">configuration settings</param>
        /// <returns></returns>
        public ICache BuildCache(string regionName, IDictionary <string, string> properties)
        {
            //return a configured cache region if we have one for the region already
            //the only way this will really happen is if there is a query cache specified for a region that is configured
            //since query caches are not configured at session factory startup
            if (String.IsNullOrEmpty(regionName) == false && cacheRegions.ContainsKey(regionName))
            {
                return(cacheRegions[regionName]);
            }

            //build the cache from preconfigured values if the region has configuration values
            if (cacheRegionSettingsList != null)
            {
                CacheRegionElement regionSettings = cacheRegionSettingsList[regionName];

                if (regionSettings != null)
                {
                    SysCacheRegion cacheRegion;

                    lock (regionsSyncRoot)
                    {
                        //note that the only reason we have to do this double check is because the query cache
                        //can try to create caches at unpredictable times
                        if (cacheRegions.TryGetValue(regionName, out cacheRegion) == false)
                        {
                            if (log.IsDebugEnabled)
                            {
                                log.DebugFormat("building cache region, '{0}', from configuration", regionName);
                            }

                            //build the cache region with settings and put it into the list so that this proces will not occur again
                            cacheRegion = new SysCacheRegion(regionName, regionSettings, properties);
                            cacheRegions[regionName] = cacheRegion;
                        }
                    }

                    return(cacheRegion);
                }
            }

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("building non-configured cache region : {0}", regionName);
            }

            //we will end up creating cache regions here for cache regions that nhibernate
            //uses internally and cache regions that weren't specified in the application config file
            return(new SysCacheRegion(regionName, properties));
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SysCacheRegion"/> class.
        /// </summary>
        /// <param name="name">The name of the region</param>
        /// <param name="settings">The configuration settings for the cache region</param>
        /// <param name="additionalProperties">additional NHibernate configuration properties</param>
        public SysCacheRegion(string name, CacheRegionElement settings, IDictionary<string, string> additionalProperties)
        {
            //validate the params
            if (String.IsNullOrEmpty(name))
            {
                log.Info("No region name specified for cache region. Using default name of 'nhibernate'");
                name = "nhibernate";
            }

            _webCache = HttpRuntime.Cache;
            _name = name;

            //configure the cache region based on the configured settings and any relevant nhibernate settings
            Configure(settings, additionalProperties);

            //creaet the cache key that will be used for the root cache item which all other
            //cache items are dependent on
            _rootCacheKey = GenerateRootCacheKey();
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SysCacheRegion"/> class.
        /// </summary>
        /// <param name="name">The name of the region</param>
        /// <param name="settings">The configuration settings for the cache region</param>
        /// <param name="additionalProperties">additional NHibernate configuration properties</param>
        public SysCacheRegion(string name, CacheRegionElement settings, IDictionary <string, string> additionalProperties)
        {
            //validate the params
            if (String.IsNullOrEmpty(name))
            {
                log.Info("No region name specified for cache region. Using default name of 'nhibernate'");
                name = "nhibernate";
            }

            _webCache = HttpRuntime.Cache;
            _name     = name;

            //configure the cache region based on the configured settings and any relevant nhibernate settings
            Configure(settings, additionalProperties);

            //creaet the cache key that will be used for the root cache item which all other
            //cache items are dependent on
            _rootCacheKey = GenerateRootCacheKey();
        }
Example #4
0
        /// <summary>
        /// Configures the cache region from configuration values
        /// </summary>
        /// <param name="settings">Configuration settings for the region</param>
        /// <param name="additionalProperties">The additional properties supplied by NHibernate engine</param>
        private void Configure(CacheRegionElement settings, IDictionary<string, string> additionalProperties)
        {
            log.Debug("Configuring cache region");

            //these are some default conenction values that can be later used by the data dependencies
            //if no custome settings are specified
            string connectionName = null;
            string connectionString = null;

            if (additionalProperties != null)
            {
                //pick up connection settings that might be used later for data dependencis if any are specified
                if (additionalProperties.ContainsKey(Environment.ConnectionStringName))
                {
                    connectionName = additionalProperties[Environment.ConnectionStringName];
                }

                if (additionalProperties.ContainsKey(Environment.ConnectionString))
                {
                    connectionString = additionalProperties[Environment.ConnectionString];
                }
            }

            if (settings != null)
            {
                _priority = settings.Priority;
                _timeOfDayExpiration = settings.TimeOfDayExpiration;
                _relativeExpiration = settings.RelativeExpiration;

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("using priority: {0}", settings.Priority.ToString("g"));

                    if (_relativeExpiration.HasValue)
                    {
                        log.DebugFormat("using relative expiration :{0}", _relativeExpiration);
                    }

                    if (_timeOfDayExpiration.HasValue)
                    {
                        log.DebugFormat("using time of day expiration : {0}", _timeOfDayExpiration);
                    }
                }

                CreateDependencyEnlisters(settings.Dependencies, connectionName, connectionString);
            }
            else
            {
                _priority = CacheItemPriority.Default;

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("no priority specified using default : {0}", _priority.ToString("g"));
                }
            }

            //use the default expiration of no expiration was set
            if (_relativeExpiration.HasValue == false && _timeOfDayExpiration.HasValue == false)
            {
                _relativeExpiration = defaultRelativeExpiration;

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("no expiration specified using default : {0}", _relativeExpiration);
                }
            }
        }
Example #5
0
        /// <summary>
        /// Configures the cache region from configuration values
        /// </summary>
        /// <param name="settings">Configuration settings for the region</param>
        /// <param name="additionalProperties">The additional properties supplied by NHibernate engine</param>
        private void Configure(CacheRegionElement settings, IDictionary <string, string> additionalProperties)
        {
            log.Debug("Configuring cache region");

            //these are some default conenction values that can be later used by the data dependencies
            //if no custome settings are specified
            string connectionName   = null;
            string connectionString = null;

            if (additionalProperties != null)
            {
                //pick up connection settings that might be used later for data dependencis if any are specified
                if (additionalProperties.ContainsKey(Environment.ConnectionStringName))
                {
                    connectionName = additionalProperties[Environment.ConnectionStringName];
                }

                if (additionalProperties.ContainsKey(Environment.ConnectionString))
                {
                    connectionString = additionalProperties[Environment.ConnectionString];
                }
            }

            if (settings != null)
            {
                _priority            = settings.Priority;
                _timeOfDayExpiration = settings.TimeOfDayExpiration;
                _relativeExpiration  = settings.RelativeExpiration;

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("using priority: {0}", settings.Priority.ToString("g"));

                    if (_relativeExpiration.HasValue)
                    {
                        log.DebugFormat("using relative expiration :{0}", _relativeExpiration);
                    }

                    if (_timeOfDayExpiration.HasValue)
                    {
                        log.DebugFormat("using time of day expiration : {0}", _timeOfDayExpiration);
                    }
                }

                CreateDependencyEnlisters(settings.Dependencies, connectionName, connectionString);
            }
            else
            {
                _priority = CacheItemPriority.Default;

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("no priority specified using default : {0}", _priority.ToString("g"));
                }
            }

            //use the default expiration of no expiration was set
            if (_relativeExpiration.HasValue == false && _timeOfDayExpiration.HasValue == false)
            {
                _relativeExpiration = defaultRelativeExpiration;

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("no expiration specified using default : {0}", _relativeExpiration);
                }
            }
        }
Example #6
0
        /// <summary>
        /// Configures the cache region from configuration values
        /// </summary>
        /// <param name="settings">Configuration settings for the region</param>
        /// <param name="additionalProperties">The additional properties supplied by NHibernate engine</param>
        private void Configure(CacheRegionElement settings, IDictionary <string, string> additionalProperties)
        {
            log.Debug("Configuring cache region");

            //these are some default conenction values that can be later used by the data dependencies
            //if no custome settings are specified
            string connectionName    = null;
            string connectionString  = null;
            string expirationString  = null;
            var    defaultExpiration = defaultRelativeExpiration;

            if (additionalProperties != null)
            {
                //pick up connection settings that might be used later for data dependencis if any are specified
                if (additionalProperties.ContainsKey(Environment.ConnectionStringName))
                {
                    connectionName = additionalProperties[Environment.ConnectionStringName];
                }

                if (additionalProperties.ContainsKey(Environment.ConnectionString))
                {
                    connectionString = additionalProperties[Environment.ConnectionString];
                }

                if (additionalProperties.ContainsKey(Environment.CacheDefaultExpiration))
                {
                    expirationString = additionalProperties[Environment.CacheDefaultExpiration];
                }

                if (!string.IsNullOrEmpty(expirationString))
                {
                    try
                    {
                        var seconds = Convert.ToInt32(expirationString);
                        defaultExpiration = TimeSpan.FromSeconds(seconds);
                        log.DebugFormat("default expiration value: {0}", seconds);
                    }
                    catch (Exception ex)
                    {
                        log.ErrorFormat("error parsing expiration value '{0}'", expirationString);
                        throw new ArgumentException($"could not parse expiration '{expirationString}' as a number of seconds", ex);
                    }
                }
            }

            if (settings != null)
            {
                _priority            = settings.Priority;
                _timeOfDayExpiration = settings.TimeOfDayExpiration;
                _relativeExpiration  = settings.RelativeExpiration;

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("using priority: {0}", settings.Priority.ToString("g"));

                    if (_relativeExpiration.HasValue)
                    {
                        log.DebugFormat("using relative expiration :{0}", _relativeExpiration);
                    }

                    if (_timeOfDayExpiration.HasValue)
                    {
                        log.DebugFormat("using time of day expiration : {0}", _timeOfDayExpiration);
                    }
                }

                CreateDependencyEnlisters(settings.Dependencies, connectionName, connectionString);
            }
            else
            {
                _priority = CacheItemPriority.Default;

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("no priority specified using default : {0}", _priority.ToString("g"));
                }
            }

            //use the default expiration of no expiration was set
            if (_relativeExpiration.HasValue == false && _timeOfDayExpiration.HasValue == false)
            {
                _relativeExpiration = defaultExpiration;

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("no expiration specified using default : {0}", _relativeExpiration);
                }
            }
        }
 private ICache BuildCache(string regionName, IDictionary <string, string> properties, CacheRegionElement settings)
 {
     Log.Debug(
         settings != null
                                 ? "building cache region, '{0}', from configuration"
                                 : "building non-configured cache region : {0}", regionName);
     return(new SysCacheRegion(regionName, settings, properties));
 }
Example #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SysCacheRegion"/> class.
 /// </summary>
 /// <param name="name">The name of the region.</param>
 /// <param name="settings">The configuration settings for the cache region.</param>
 /// <param name="additionalProperties">Additional NHibernate configuration properties.</param>
 public SysCacheRegion(string name, CacheRegionElement settings,
                       IDictionary <string, string> additionalProperties)
     : base(name, settings, additionalProperties)
 {
 }
Example #9
0
        /// <summary>
        /// Configures the cache region from configuration values.
        /// </summary>
        /// <param name="settings">Configuration settings for the region.</param>
        /// <param name="additionalProperties">The additional properties supplied by NHibernate engine.</param>
        private void Configure(CacheRegionElement settings, IDictionary <string, string> additionalProperties)
        {
            Log.Debug("Configuring cache region");

            // these are some default connection values that can be later used by the data dependencies
            // if no custom settings are specified
            string connectionName    = null;
            string connectionString  = null;
            var    defaultExpiration = DefaultRelativeExpiration;

            _useSlidingExpiration = _defaultUseSlidingExpiration;

            if (additionalProperties != null)
            {
                // pick up connection settings that might be used later for data dependencis if any are specified
                if (additionalProperties.ContainsKey(Environment.ConnectionStringName))
                {
                    connectionName = additionalProperties[Environment.ConnectionStringName];
                }

                if (additionalProperties.ContainsKey(Environment.ConnectionString))
                {
                    connectionString = additionalProperties[Environment.ConnectionString];
                }

                if (!additionalProperties.TryGetValue("expiration", out var expirationString))
                {
                    additionalProperties.TryGetValue(Environment.CacheDefaultExpiration, out expirationString);
                }

                if (expirationString != null)
                {
                    try
                    {
                        var seconds = Convert.ToInt32(expirationString);
                        defaultExpiration = TimeSpan.FromSeconds(seconds);
                        Log.Debug("default expiration value: {0}", seconds);
                    }
                    catch (Exception ex)
                    {
                        Log.Error("error parsing expiration value '{0}'", expirationString);
                        throw new ArgumentException($"could not parse expiration '{expirationString}' as a number of seconds", ex);
                    }
                }

                _useSlidingExpiration = PropertiesHelper.GetBoolean("cache.use_sliding_expiration", additionalProperties, _defaultUseSlidingExpiration);
                Log.Debug("default sliding expiration value: {0}", _useSlidingExpiration);
            }

            if (settings != null)
            {
                _priority             = settings.Priority;
                _timeOfDayExpiration  = settings.TimeOfDayExpiration;
                _relativeExpiration   = settings.RelativeExpiration;
                _useSlidingExpiration = settings.UseSlidingExpiration ?? _useSlidingExpiration;

                if (Log.IsDebugEnabled())
                {
                    Log.Debug("using priority: {0:g}", settings.Priority);

                    if (_relativeExpiration.HasValue)
                    {
                        Log.Debug("using relative expiration: {0}{1}", _relativeExpiration, _useSlidingExpiration ? " (sliding)" : string.Empty);
                    }

                    if (_timeOfDayExpiration.HasValue)
                    {
                        Log.Debug("using time of day expiration: {0}", _timeOfDayExpiration);
                    }
                }

                CreateDependencyEnlisters(settings.Dependencies, connectionName, connectionString);
            }
            else
            {
                _priority = CacheItemPriority.Default;

                Log.Debug("no priority specified, using default: {0:g}", _priority);
            }

            //use the default expiration as no expiration was set
            if (_relativeExpiration.HasValue == false && _timeOfDayExpiration.HasValue == false)
            {
                _relativeExpiration = defaultExpiration;

                Log.Debug("no expiration specified, using default: {0}", _relativeExpiration);
            }
        }
 /// <summary>
 /// Set a region configuration.
 /// </summary>
 /// <param name="configuration">The region configuration.</param>
 public static void SetRegionConfiguration(CacheRegionElement configuration)
 {
     CacheRegionSettings[configuration.Name] = configuration;
 }