Example #1
0
        static StackSigCacheConfig GetStackSigConfig()
        {
            string stackSig = GetStackSig();

            // if the sig is null, return a null sig config (stops caching)
            if (stackSig == null)
            {
                return(new StackSigCacheConfig(null));
            }
            // Check for a cache config override in the matrix
            if (!CacheMatrix.ContainsKey(stackSig))
            {
                Log.Info(string.Format("CACHEMATRIX: Cache Matrix does not have an entry for:\r\n\t{0}", stackSig));
                // This line will add a row to the cache matrix with caching enabled or disabled DEPENDING ON THE web.configs CacheEnabled setting
                // This means the web.configs CacheEnabled setting is the /default/ caching behaviour, however each cahe matrix entry can override that
                StackSigCacheConfig c = new StackSigCacheConfig(stackSig)
                {
                    Cache = CacheManager.CacheEnabled
                };
                try
                {
                    CacheMatrix.Add(stackSig, c);
                    SaveCacheMatrix();
                }
                catch {}
                return(c);
            }
            return(CacheMatrix[stackSig]);
        }
Example #2
0
 /// <summary>
 /// Writes the cacheMatrix to the Configuration file - using file locking to prevent reading whilst writing
 /// </summary>
 /// <remarks>
 /// Before writing the file out, the existing file is read and merged into cacheMatrix (Ignoring any existing file-lock file).
 /// If the read fails, it does not bother to save, we'll never write the file out unless we're dead sure that we're not destroying what's in the file.
 /// </remarks>
 public static void SaveCacheMatrix()
 {
     Log.Debug(string.Format("CACHEMATRIX: Attempting to save cache matrix of {0} items to {1}", cacheMatrix.Count, ConfPath));
     if (!File.Exists(LockPath))
     {
         File.Create(LockPath).Close();
         LoadAndMergeCacheMatrixIgnoringLockFile();
         using (StreamWriter w = new StreamWriter(ConfPath, false))
         {
             w.WriteLine(DEFAULT_CACHE_MATRIX_HEADER);
             foreach (string k in cacheMatrix.Keys)
             {
                 StackSigCacheConfig c = cacheMatrix [k];
                 w.WriteLine(string.Format("{0},{1},{2}", c.Signature.PadRight(FILE_SIG_PADDING), c.Cache, c.MaxAgeOverride.ToString()));
             }
             w.Flush();
             w.Close();
         }
         KillLockFile();
         Log.Debug(string.Format("CACHEMATRIX: Saved cache matrix to {0}", ConfPath));
     }
     else
     {
         CheckLockFileAge();
     }
 }
Example #3
0
 /// <summary>
 /// Merges the CacheMatrix file into the cacheMatrix SortedDictionary in memory, regardless whether the lockfile exists
 /// </summary>
 /// <remarks>
 /// This function is called during the SaveCacheMatrix to merge the existing configuration file before overwriting.
 /// The code expects cacheMatrix to be valid after a call to this - DO NOT remove the SortedDictionary create.
 /// ConfigOverwrite is the only way to reset the config file without merging, but, even so, the config file will soon contain the merges again.
 /// </remarks>
 public static void LoadAndMergeCacheMatrixIgnoringLockFile()
 {
     if (cacheMatrix == null)
     {
         cacheMatrix = new SortedDictionary <string, StackSigCacheConfig>();
     }
     if (File.Exists(ConfPath))
     {
         Log.Debug(string.Format("CACHEMATRIX: Loading cache matrix from {0}", ConfPath));
         File.Create(LockPath).Close();
         using (StreamReader r = new StreamReader(ConfPath))
         {
             cacheMatrixFileContents = r.ReadToEnd();
             r.Close();
             string[] lines = cacheMatrixFileContents.Split('\n');
             foreach (string line in lines)
             {
                 if (!line.Trim().StartsWith("#") && line.Trim() != string.Empty)
                 {
                     string              l     = line.Trim();
                     string[]            parts = l.Split(',');
                     StackSigCacheConfig c     = new StackSigCacheConfig(parts[0].Trim());
                     if (parts.Length > 1)
                     {
                         c.Cache = Convert.ToBoolean(parts[1].Trim());
                     }
                     if (parts.Length > 2)
                     {
                         c.MaxAgeOverride = Convert.ToInt32(parts[2].Trim());
                     }
                     parts[0] = parts[0].Trim();
                     if (cacheMatrix.ContainsKey(parts[0]))
                     {
                         cacheMatrix[parts[0]] = c;
                     }
                     else
                     {
                         cacheMatrix.Add(parts[0], c);
                     }
                 }
             }
         }
         ConfigLoaded = true;
         KillLockFile();
         Log.Debug(string.Format("CACHEMATRIX: Loaded {0} lines from {1}", cacheMatrix.Count, ConfPath));
     }
 }
Example #4
0
        private static DataTable GetCacheMatrixDataTable()
        {
            DataTable ret = new DataTable();

            ret.Columns.Add("No", typeof(int));
            ret.Columns.Add("App", typeof(string));
            ret.Columns.Add("Caching Enabled", typeof(bool));
            ret.Columns.Add("TTL Override", typeof(int));
            ret.Columns.Add("Stack Signature", typeof(string));
            int x = 1;

            foreach (string k in CacheMatrix.Keys)
            {
                StackSigCacheConfig c        = CacheMatrix[k];
                string[]            sigParts = c.Signature.Split(':');
                string app = sigParts[sigParts.Length - 2].Split('.')[0];
                ret.Rows.Add(new object[] { x, app, c.Cache, c.MaxAgeOverride, c.Signature });
                x++;
            }
            return(ret);
        }
Example #5
0
 public static T GetFromCache <T>(string key) where T : class
 {
     try
     {
         StackSigCacheConfig c = GetStackSigConfig();
         Log.Debug(string.Format("CACHEMATRIX: CallStackCaching.GetFromCache() with sig {0} is {1}", c.Signature, c.Cache));
         if (c.Cache)
         {
             object ret = SERV.Utils.Caching.CacheManager.GetFromCache <T>(0, SERV.Utils.Caching.CacheManager.GetCacheIdentifier(key), true, c.MaxAgeOverride);
             return((T)ret);
         }
         else
         {
             return(null);
         }
     }
     catch (Exception e)
     {
         Log.Warn("A (now non-fatal) caching error occured at CallStackCaching::GetFromCache() - " + e.Message);
         return(null);
     }
 }