/// -----------------------------------------------------------------------------
        /// <summary>
        /// Saves the Page State to the Cache
        /// </summary>
        /// <history>
        ///     [cnurse]	    11/30/2006	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public override void Save()
        {
            //No processing needed if no states available
            if (ViewState == null && ControlState == null)
            {
                return;
            }

            //Generate a unique cache key
            var key = new StringBuilder();
            {
                key.Append("VS_");
                key.Append(Page.Session == null ? Guid.NewGuid().ToString() : Page.Session.SessionID);
                key.Append("_");
                key.Append(DateTime.Now.Ticks.ToString());
            }

            //Save view state and control state separately
            var state = new Pair(ViewState, ControlState);

            //Add view state and control state to cache
            DNNCacheDependency objDependency = null;

            DataCache.SetCache(key.ToString(), state, objDependency, DateTime.Now.AddMinutes(Page.Session.Timeout), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);

            //Register hidden field to store cache key in
            Page.ClientScript.RegisterHiddenField(VIEW_STATE_CACHEKEY, key.ToString());
        }
        public void DataCache_SetCache_With_CacheDependency_AbsoluteExpiration_SlidingExpiration_Should_Throw_On_Null_CacheKey(string key)
        {
            DNNCacheDependency dep           = CreateTestDependency();    // Dependency type or value doesn't matter
            DateTime           absExpiry     = DateTime.Today.AddDays(1); // DateTime doesn't matter
            TimeSpan           slidingExpiry = TimeSpan.FromMinutes(5);   // TimeSpan doesn't matter

            Assert.Throws <ArgumentException>(() => DataCache.SetCache(key, Constants.CACHEING_ValidValue, dep, absExpiry, slidingExpiry));
        }
Example #3
0
        public override void Insert(string cacheKey, object itemToCache, DNNCacheDependency dependency, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority,
                                    CacheItemRemovedCallback onRemoveCallback)
        {
            //onRemoveCallback += ItemRemovedCallback;

            //Call base class method to add obect to cache
            base.Insert(cacheKey, itemToCache, dependency, absoluteExpiration, slidingExpiration, priority, onRemoveCallback);
        }
        public void DataCache_SetCache_With_Priority_Should_Throw_On_Null_CacheKey(string key)
        {
            DNNCacheDependency dep           = this.CreateTestDependency(); // Dependency type or value doesn't matter
            DateTime           absExpiry     = DateTime.Today.AddDays(1);   // DateTime doesn't matter
            TimeSpan           slidingExpiry = TimeSpan.FromMinutes(5);     // TimeSpan doesn't matter
            CacheItemPriority  priority      = CacheItemPriority.High;      // Priority doesn't matter

            Assert.Throws <ArgumentException>(() => DataCache.SetCache(key, Constants.CACHEING_ValidValue, dep, absExpiry, slidingExpiry, priority, null));
        }
        private static void SetPageCache(string key, object value, DNNCacheDependency dependency, FriendlyUrlSettings settings, CacheItemRemovedCallback callback)
        {
            DateTime absoluteExpiration = DateTime.Now.Add(settings.CacheTime);

            DataCache.SetCache(key,
                               value,
                               dependency,
                               absoluteExpiration,
                               Cache.NoSlidingExpiration,
                               CacheItemPriority.AboveNormal,
                               callback);
        }
Example #6
0
 public static void SetCache(string CacheKey, object objObject, DNNCacheDependency objDependency, DateTime AbsoluteExpiration, TimeSpan SlidingExpiration, CacheItemPriority Priority,
                             CacheItemRemovedCallback OnRemoveCallback)
 {
     if (objObject != null)
     {
         //if no OnRemoveCallback value is specified, use the default method
         if (OnRemoveCallback == null)
         {
             OnRemoveCallback = ItemRemovedCallback;
         }
         CachingProvider.Instance().Insert(GetDnnCacheKey(CacheKey), objObject, objDependency, AbsoluteExpiration, SlidingExpiration, Priority, OnRemoveCallback);
     }
 }
        public void DataCache_SetCache_With_Priority_Should_Succeed_On_Valid_CacheKey_And_Any_Value()
        {
            // Arrange
            DNNCacheDependency dep           = CreateTestDependency();    // Dependency type or value doesn't matter
            DateTime           absExpiry     = DateTime.Today.AddDays(1); // DateTime doesn't matter
            TimeSpan           slidingExpiry = TimeSpan.FromMinutes(5);   // TimeSpan doesn't matter
            CacheItemPriority  priority      = CacheItemPriority.High;    // Priority doesn't matter

            // Act
            DataCache.SetCache(Constants.CACHEING_ValidKey, Constants.CACHEING_ValidValue, dep, absExpiry, slidingExpiry, priority, null);

            // Assert
            mockCache.Verify(cache => cache.Insert(GetDnnCacheKey(Constants.CACHEING_ValidKey), Constants.CACHEING_ValidValue, dep, absExpiry, slidingExpiry, priority, DataCache.ItemRemovedCallback));
        }
Example #8
0
        public static TemplateInfo GetTemplate(string templatePath, string templateName, bool parse, bool isLogged)
        {
            string file     = Path.Combine(templatePath, templateName);
            string cacheKey = file + parse + isLogged;

            TemplateInfo templateInfo = (TemplateInfo)DataCache.GetCache(cacheKey);

            if (templateInfo == null)
            {
                templateInfo = new TemplateInfo(file, parse, isLogged);
                DNNCacheDependency cacheDep = new DNNCacheDependency(file);
                DataCache.SetCache(cacheKey, templateInfo, cacheDep);
            }
            return(templateInfo);
        }
 public override void Insert(string key, object value, DNNCacheDependency dependency, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority,
                             CacheItemRemovedCallback onRemoveCallback)
 {
     try
     {
         // Simply serialize the object before inserting into memory. Optional: use compression
         if (UseCompression)
         {
             base.Insert(key, CompressData(value), dependency, absoluteExpiration, slidingExpiration,
                         priority, onRemoveCallback);
         }
         else
         {
             base.Insert(key, Serialize(value), dependency, absoluteExpiration, slidingExpiration,
                         priority, onRemoveCallback);
         }
     }
     catch (Exception serialEx)
     {
         // If the object is not serializable, do something
         if (bool.Parse(GetProviderConfigAttribute("silentMode", "false")))
         {
             // Write on the cache for debugging purposes
             base.Insert("SERIALIZATION_ERROR_" + key,
                         "An exception was thrown during the serialization of this object" +
                         string.Format("{0}", value), dependency, Cache.NoAbsoluteExpiration,
                         new TimeSpan(0, 0, int.Parse(GetProviderConfigAttribute("defaultCacheTimeout", "300"))));
             try
             {
                 throw new SerializationException(
                           string.Format("Error while trying to cache key {0} (Object type: {1}): {2}", key,
                                         value.GetType(), serialEx));
             }
             catch (Exception ex)
             {
                 DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
             }
         }
         else
         {
             throw;
         }
     }
 }
        public void DataCache_SetCache_With_Dependency_Should_Succeed_On_Valid_CacheKey_And_Any_Value()
        {
            // Arrange
            DNNCacheDependency dep = CreateTestDependency(); // Dependency type or value doesn't matter

            // Act
            DataCache.SetCache(Constants.CACHEING_ValidKey, Constants.CACHEING_ValidValue, dep);

            // Assert
            mockCache.Verify(
                cache =>
                cache.Insert(GetDnnCacheKey(Constants.CACHEING_ValidKey),
                             Constants.CACHEING_ValidValue,
                             dep,
                             Cache.NoAbsoluteExpiration,
                             Cache.NoSlidingExpiration,
                             CacheItemPriority.Normal,
                             DataCache.ItemRemovedCallback));
        }
        public void DataCache_SetCache_Should_Succeed_On_Valid_CacheKey_And_Any_Value()
        {
            // Arrange

            // Act
            DataCache.SetCache(Constants.CACHEING_ValidKey, Constants.CACHEING_ValidValue);

            // Assert
            DNNCacheDependency dep = null;

            mockCache.Verify(
                cache =>
                cache.Insert(GetDnnCacheKey(Constants.CACHEING_ValidKey),
                             Constants.CACHEING_ValidValue,
                             dep,
                             Cache.NoAbsoluteExpiration,
                             Cache.NoSlidingExpiration,
                             CacheItemPriority.Normal,
                             DataCache.ItemRemovedCallback));
        }
Example #12
0
        public override void Insert(string key, object value, DNNCacheDependency dependency, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority,
                                    CacheItemRemovedCallback onRemoveCallback)
        {
            try
            {
                // Calculate expiry
                TimeSpan?expiry = null;
                if (absoluteExpiration != DateTime.MinValue)
                {
                    expiry = absoluteExpiration.Subtract(DateTime.UtcNow);
                }
                else
                {
                    if (slidingExpiration != TimeSpan.Zero)
                    {
                        expiry = slidingExpiration;
                    }
                }

                if (UseCompression)
                {
                    var cvalue = Shared.CompressData(value);
                    base.Insert(key, cvalue, dependency, absoluteExpiration, slidingExpiration,
                                priority, onRemoveCallback);
                    RedisCache.StringSet(KeyPrefix + key, Shared.Serialize(cvalue), expiry);
                }
                else
                {
                    base.Insert(key, value, dependency, absoluteExpiration, slidingExpiration,
                                priority, onRemoveCallback);
                    RedisCache.StringSet(KeyPrefix + key, Shared.Serialize(value), expiry);
                }
            }
            catch (Exception e)
            {
                if (!Shared.ProcessException(ProviderName, e, key, value))
                {
                    throw;
                }
            }
        }
Example #13
0
        public override void Insert(string cacheKey, object itemToCache, DNNCacheDependency dependency, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority,
                                    CacheItemRemovedCallback onRemoveCallback)
        {
			//initialize cache dependency
            DNNCacheDependency d = dependency;

            //if web farm is enabled
            if (IsWebFarm())
            {
                //get hashed file name
                var f = new string[1];
                f[0] = GetFileName(cacheKey);
                //create a cache file for item
                CreateCacheFile(f[0], cacheKey);
                //create a cache dependency on the cache file
                d = new DNNCacheDependency(f, null, dependency);
            }
			
            //Call base class method to add obect to cache
            base.Insert(cacheKey, itemToCache, d, absoluteExpiration, slidingExpiration, priority, onRemoveCallback);
        }
        public override void Insert(string cacheKey, object itemToCache, DNNCacheDependency dependency, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority,
                                    CacheItemRemovedCallback onRemoveCallback)
        {
            //initialize cache dependency
            DNNCacheDependency d = dependency;

            //if web farm is enabled
            if (IsWebFarm())
            {
                //get hashed file name
                var f = new string[1];
                f[0] = GetFileName(cacheKey);
                //create a cache file for item
                CreateCacheFile(f[0], cacheKey);
                //create a cache dependency on the cache file
                d = new DNNCacheDependency(f, null, dependency);
            }

            //Call base class method to add obect to cache
            base.Insert(cacheKey, itemToCache, d, absoluteExpiration, slidingExpiration, priority, onRemoveCallback);
        }
        public void DataCache_SetCache_With_AbsoluteExpiration_Should_Succeed_On_Valid_CacheKey_And_Any_Value()
        {
            // Arrange
            DateTime absExpiry = DateTime.Today.AddDays(1); // DateTime doesn't matter

            // Act
            DataCache.SetCache(Constants.CACHEING_ValidKey, Constants.CACHEING_ValidValue, absExpiry);

            // Assert
            DNNCacheDependency dep = null;

            mockCache.Verify(
                cache =>
                cache.Insert(GetDnnCacheKey(Constants.CACHEING_ValidKey),
                             Constants.CACHEING_ValidValue,
                             dep,
                             absExpiry,
                             Cache.NoSlidingExpiration,
                             CacheItemPriority.Normal,
                             DataCache.ItemRemovedCallback));
        }
        public void DataCache_SetCache_With_SlidingExpiration_Should_Succeed_On_Valid_CacheKey_And_Any_Value()
        {
            // Arrange
            TimeSpan slidingExpiry = TimeSpan.FromMinutes(5); // TimeSpan doesn't matter

            // Act
            DataCache.SetCache(Constants.CACHEING_ValidKey, Constants.CACHEING_ValidValue, slidingExpiry);

            // Assert
            // Assert
            DNNCacheDependency dep = null;

            mockCache.Verify(
                cache =>
                cache.Insert(GetDnnCacheKey(Constants.CACHEING_ValidKey),
                             Constants.CACHEING_ValidValue,
                             dep,
                             Cache.NoAbsoluteExpiration,
                             slidingExpiry,
                             CacheItemPriority.Normal,
                             DataCache.ItemRemovedCallback));
        }
Example #17
0
 public static void SetCache(string CacheKey, object objObject, DNNCacheDependency objDependency, DateTime AbsoluteExpiration, TimeSpan SlidingExpiration)
 {
     SetCache(CacheKey, objObject, objDependency, AbsoluteExpiration, SlidingExpiration, CacheItemPriority.Normal, null);
 }
Example #18
0
        public static void SetCache(string CacheKey, object objObject, TimeSpan SlidingExpiration, bool PersistAppRestart)
        {
            DNNCacheDependency objDependency = null;

            SetCache(CacheKey, objObject, objDependency, Cache.NoAbsoluteExpiration, SlidingExpiration, CacheItemPriority.Normal, null);
        }
Example #19
0
        // ReSharper disable SuggestBaseTypeForParameter
        private static IDictionary <string, string> LoadResource(
            IDictionary <string, string> resources, string cacheKey, string resourceFile, CustomizedLocale checkCustomCulture, int portalId)
        {
            //// ReSharper restore SuggestBaseTypeForParameter
            string f = null;

            // Are we looking for customised resources
            switch (checkCustomCulture)
            {
            case CustomizedLocale.None:
                f = resourceFile;
                break;

            case CustomizedLocale.Portal:
                f = resourceFile.Replace(".RESX", ".Portal-" + portalId.ToString(CultureInfo.InvariantCulture) + ".resx");
                break;

            case CustomizedLocale.Host:
                f = resourceFile.Replace(".RESX", ".Host.resx");
                break;
            }

            // If the filename is empty or the file does not exist return the dictionary
            string filePath = HostingEnvironment.MapPath(f);

            if (f == null || !File.Exists(filePath))
            {
                return(resources);
            }

            XPathDocument doc = null;

            using (var dp = new DNNCacheDependency(filePath))
            {
                bool xmlLoaded;
                try
                {
                    // ReSharper disable AssignNullToNotNullAttribute
                    doc = new XPathDocument(filePath);

                    // ReSharper restore AssignNullToNotNullAttribute
                    xmlLoaded = true;
                }
                catch
                {
                    xmlLoaded = false;
                }

                if (xmlLoaded)
                {
                    foreach (XPathNavigator nav in doc.CreateNavigator().Select("root/data"))
                    {
                        if (nav.NodeType != XPathNodeType.Comment)
                        {
                            resources[nav.GetAttribute("name", string.Empty)] = nav.SelectSingleNode("value").Value;
                        }
                    }

                    try
                    {
                        int cacheMinutes = 3 * (int)Host.PerformanceSetting;
                        if (cacheMinutes > 0)
                        {
                            DataCache.SetCache(cacheKey, resources, dp, DateTime.MaxValue, new TimeSpan(0, cacheMinutes, 0));
                        }
                    }

#pragma warning disable 1692
#pragma warning disable EmptyGeneralCatchClause
                    catch
                    {
                    }

#pragma warning restore EmptyGeneralCatchClause
#pragma warning restore 1692
                }
            }

            return(resources);
        }
 public override void Insert(string cacheKey, object itemToCache, DNNCacheDependency dependency, DateTime absoluteExpiration,
                             TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
 {
     this._dictionary[cacheKey] = itemToCache;
 }
        public void DataCache_SetCache_With_Dependency_Should_Throw_On_Null_CacheKey(string key)
        {
            DNNCacheDependency dep = CreateTestDependency(); // Dependency type or value doesn't matter

            Assert.Throws <ArgumentException>(() => DataCache.SetCache(key, Constants.CACHEING_ValidValue, dep));
        }
Example #22
0
 public static void SetCache(string CacheKey, object objObject, DNNCacheDependency objDependency)
 {
     SetCache(CacheKey, objObject, objDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
 }
Example #23
0
        public override void SetModule(int tabModuleId, string cacheKey, TimeSpan duration, byte[] moduleOutput)
        {
            DNNCacheDependency dep = null;

            DataCache.SetCache(cacheKey, moduleOutput, dep, DateTime.UtcNow.Add(duration), System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
        }