Exemple #1
0
 /// <summary>
 /// Adds an special cache dependency for the file (used by custom tags or FM sources)
 /// </summary>
 /// <param name="cd">The CacheDependency class to add as a dependency</param>
 internal void AddCacheDependency(CacheDependency cd)
 {
     if (this.SpecialDependencies == null)
     {
         this.SpecialDependencies = new List <CacheDependency> {
             cd
         };
     }
     else
     {
         //CHeck if it's already added
         bool alreadyPresent = this.SpecialDependencies.Any(
             item => item.GetUniqueID() == cd.GetUniqueID()
             );
         if (!alreadyPresent)
         {
             this.SpecialDependencies.Add(cd);
         }
     }
 }
Exemple #2
0
        private void UpdateFromDependencies(HttpResponse response)
        {
            CacheDependency dep = null;

            if ((this._etag == null) && this._generateEtagFromFiles)
            {
                dep = response.CreateCacheDependencyForResponse();
                if (dep == null)
                {
                    return;
                }
                string uniqueID = dep.GetUniqueID();
                if (uniqueID == null)
                {
                    throw new HttpException(System.Web.SR.GetString("No_UniqueId_Cache_Dependency"));
                }
                DateTime      time    = this.UpdateLastModifiedTimeFromDependency(dep);
                StringBuilder builder = new StringBuilder(0x100);
                builder.Append(HttpRuntime.AppDomainIdInternal);
                builder.Append(uniqueID);
                builder.Append("+LM");
                builder.Append(time.Ticks.ToString(CultureInfo.InvariantCulture));
                this._etag = MachineKeySection.HashAndBase64EncodeString(builder.ToString());
                this._etag = "\"" + this._etag + "\"";
            }
            if (this._generateLastModifiedFromFiles)
            {
                if (dep == null)
                {
                    dep = response.CreateCacheDependencyForResponse();
                    if (dep == null)
                    {
                        return;
                    }
                }
                DateTime utcDate = this.UpdateLastModifiedTimeFromDependency(dep);
                this.UtcSetLastModified(utcDate);
            }
        }
Exemple #3
0
        private static T LoadInternal(Guid siteId)
        {
            T config = new T();

            config.siteId = siteId;
            using (SPSite elevatedSite = new SPSite(siteId, SPUserToken.SystemAccount)) {
                SiteConfigProviderAttribute attribute = typeof(T).GetCustomAttribute <SiteConfigProviderAttribute>(false);
                ISiteConfigProvider         provider;
                if (attribute == null)
                {
                    provider = new SiteConfigProvider();
                }
                else
                {
                    provider = (ISiteConfigProvider)attribute.ProviderType.CreateInstance();
                }
                provider.Initialize(elevatedSite);

                foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(config))
                {
                    ISiteConfigEntry entry      = provider.GetEntry(pd.Name);
                    bool             needUpdate = false;
                    bool             needCreate = false;

                    if (entry == null || entry.UseDefaultValue)
                    {
                        string defaultValueString = String.Empty;
                        try {
                            pd.ResetValue(config);
                            defaultValueString = Convert.ToString(pd.GetValue(config));
                        } catch (ArgumentException) {
                            DefaultValueAttribute defaultValueAttribute = (DefaultValueAttribute)pd.Attributes[typeof(DefaultValueAttribute)];
                            if (defaultValueAttribute != null && defaultValueAttribute.Value != null)
                            {
                                object defaultValue = ParseValue(pd, defaultValueAttribute.Value);
                                pd.SetValue(config, defaultValue);
                                defaultValueString = Convert.ToString(defaultValueAttribute.Value);
                            }
                        }
                        if (entry == null)
                        {
                            needCreate = true;
                            entry      = (pd.PropertyType == typeof(SecureString)) ? new SecureEntry()
                            {
                                Key = pd.Name
                            } : new Entry()
                            {
                                Key = pd.Name
                            };
                        }
                        if (!CompareString(entry.Value, defaultValueString))
                        {
                            entry.Value = defaultValueString;
                            needUpdate |= CompareString(entry.Value, defaultValueString);
                        }
                    }
                    else
                    {
                        object typedValue = ParseValueFromEntry(pd, entry);
                        pd.SetValue(config, typedValue);
                    }
                    if (!CompareString(entry.Category, pd.Category))
                    {
                        entry.Category = pd.Category;
                        needUpdate    |= CompareString(entry.Category, pd.Category);
                    }
                    if (!CompareString(entry.Description, pd.Description))
                    {
                        entry.Description = pd.Description;
                        needUpdate       |= CompareString(entry.Description, pd.Description);
                    }
                    if (needCreate)
                    {
                        provider.CreateEntry(entry);
                    }
                    else if (needUpdate)
                    {
                        provider.UpdateEntry(entry);
                    }
                    if (pd.GetValue(config) == null && pd.PropertyType.GetConstructor(new Type[0]) != null)
                    {
                        pd.SetValue(config, Activator.CreateInstance(pd.PropertyType));
                    }
                }
                provider.CommitChanges();

                if (HttpContext.Current != null)
                {
                    CacheDependency cacheDependency = provider.GetCacheDependency();
                    if (cacheDependency != null)
                    {
                        HttpContext.Current.Cache.Add(cacheDependency.GetUniqueID(), new object(), cacheDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, (k, v, r) => {
                            Logger.Info("Site config ({0}) flushed due to cache dependency.", typeof(T).FullName);
                            Invalidate(siteId);
                        });
                    }
                }
            }
            return(config);
        }