CachingCompiler.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // System.Web.Compilation.CachingCompiler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.CodeDom;
  11. using System.CodeDom.Compiler;
  12. using System.Collections;
  13. using System.IO;
  14. using System.Web.UI;
  15. namespace System.Web.Compilation
  16. {
  17. //TODO: caching should be done using System.Web.Caching, but that namespace still need some work.
  18. internal class CompilationCacheItem
  19. {
  20. CompilerResults result;
  21. ArrayList dependencies;
  22. DateTime reference;
  23. public CompilationCacheItem (CompilerResults result, ArrayList dependencies)
  24. {
  25. this.result = result;
  26. this.dependencies = dependencies;
  27. this.reference = DateTime.Now;
  28. }
  29. public bool CheckDependencies (string key)
  30. {
  31. if (dependencies == null)
  32. return true; // Forever young
  33. foreach (string s in dependencies) {
  34. if (!File.Exists (s) || File.GetLastWriteTime (s) > reference)
  35. return false;
  36. }
  37. return true;
  38. }
  39. public CompilerResults Result {
  40. get { return result; }
  41. }
  42. }
  43. internal class CompilationCache
  44. {
  45. static Hashtable cache;
  46. static CompilationCache instance = new CompilationCache ();
  47. private CompilationCache ()
  48. {
  49. }
  50. static CompilationCache ()
  51. {
  52. cache = new Hashtable ();
  53. }
  54. public static CompilationCache GetInstance ()
  55. {
  56. return instance;
  57. }
  58. public bool CheckDependencies (CompilationCacheItem item, string key)
  59. {
  60. bool result = item.CheckDependencies (key);
  61. if (result == false)
  62. cache.Remove (key);
  63. return result;
  64. }
  65. public CompilationCacheItem this [string key] {
  66. get { return cache [key] as CompilationCacheItem; }
  67. set { cache [key] = value; }
  68. }
  69. }
  70. internal class CachingCompiler
  71. {
  72. static CompilationCache cache = CompilationCache.GetInstance ();
  73. private CachingCompiler () {}
  74. public static CompilationCacheItem GetCached (string key)
  75. {
  76. if (key == null)
  77. throw new ArgumentNullException ("key");
  78. CompilationCacheItem item = cache [key];
  79. if (item != null && cache.CheckDependencies (item, key))
  80. return item;
  81. return null;
  82. }
  83. static object compilationLock = new object ();
  84. public static CompilerResults Compile (BaseCompiler compiler)
  85. {
  86. string key = compiler.Parser.InputFile;
  87. CompilationCacheItem item = GetCached (key);
  88. if (item != null)
  89. return item.Result;
  90. CompilerResults results = null;
  91. lock (compilationLock) {
  92. item = GetCached (key);
  93. if (item != null)
  94. return item.Result;
  95. results = compiler.Compiler.CompileAssemblyFromDom (compiler.CompilerParameters, compiler.Unit);
  96. cache [key] = new CompilationCacheItem (results, compiler.Parser.Dependencies);
  97. }
  98. return results;
  99. }
  100. }
  101. }