WebDAVStoreBase.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Security.Principal;
  3. using WebDAVSharp.Server.MethodHandlers;
  4. using WebDAVSharp.Server.Stores.Locks.Interfaces;
  5. using WebDAVSharp.Server.Utilities;
  6. namespace WebDAVSharp.Server.Stores.BaseClasses
  7. {
  8. /// <summary>
  9. /// This class is a base class for <see cref="IWebDavStore" /> implementations.
  10. /// </summary>
  11. public abstract class WebDavStoreBase : IWebDavStore
  12. {
  13. #region Variables
  14. #endregion
  15. #region Constructor
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="WebDavStoreBase" /> class.
  18. /// </summary>
  19. /// <param name="root">The root <see cref="IWebDavStoreCollection" />.</param>
  20. /// <param name="lockSystem"></param>
  21. /// <exception cref="System.ArgumentNullException">root</exception>
  22. /// <exception cref="ArgumentNullException"><paramref name="root" /> is <c>null</c>.</exception>
  23. protected WebDavStoreBase(IWebDavStoreCollection root, IWebDavStoreItemLock lockSystem)
  24. {
  25. LockSystem = lockSystem ?? throw new ArgumentNullException(nameof(lockSystem));
  26. Root = root ?? throw new ArgumentNullException(nameof(root));
  27. }
  28. /// <summary>
  29. /// </summary>
  30. protected WebDavStoreBase(IWebDavStoreItemLock lockSystem)
  31. {
  32. LockSystem = lockSystem;
  33. }
  34. #endregion
  35. #region Properties
  36. /// <summary>
  37. /// Gets the root collection of this <see cref="IWebDavStore" />.
  38. /// </summary>
  39. public virtual IWebDavStoreCollection Root { get; }
  40. /// <summary>
  41. /// </summary>
  42. public IWebDavStoreItemLock LockSystem { get; set; }
  43. ///// <summary>
  44. ///// </summary>
  45. //public WebDavServer.ClearCaches FClearCaches { get; set; }
  46. /// <summary>
  47. /// </summary>
  48. /// <param name="ident"></param>
  49. public abstract void UserAuthenticated(IIdentity ident);
  50. private readonly WebDavCacheBase _cache = new WebDavCacheBase();
  51. /// <summary>
  52. /// </summary>
  53. /// <param name="handler"></param>
  54. /// <param name="user"></param>
  55. /// <param name="path"></param>
  56. /// <param name="value"></param>
  57. /// <param name="timeToExpire"></param>
  58. internal void AddCacheObject(WebDavMethodHandlerBase handler, string user, string path, object value, TimeSpan timeToExpire)
  59. {
  60. WebDavCacheBase pathCache = (WebDavCacheBase) _cache.GetCachedObject(path);
  61. WebDavCacheBase handlerCache;
  62. if (pathCache != null)
  63. {
  64. handlerCache = (WebDavCacheBase) pathCache.GetCachedObject(handler.GetType().FullName);
  65. if (handlerCache != null)
  66. {
  67. handlerCache.AddCacheObject(user, value, timeToExpire);
  68. return;
  69. }
  70. handlerCache = new WebDavCacheBase();
  71. handlerCache.AddCacheObject(user, value, timeToExpire);
  72. pathCache.AddCacheObject(handler.GetType().FullName, handlerCache, timeToExpire);
  73. return;
  74. }
  75. pathCache = new WebDavCacheBase();
  76. handlerCache = new WebDavCacheBase();
  77. handlerCache.AddCacheObject(user, value, timeToExpire);
  78. pathCache.AddCacheObject(handler.GetType().FullName, handlerCache, timeToExpire);
  79. _cache.AddCacheObject(path, pathCache, timeToExpire);
  80. }
  81. /// <summary>
  82. /// </summary>
  83. /// <param name="handler"></param>
  84. /// <param name="user"></param>
  85. /// <param name="path"></param>
  86. /// <param name="value"></param>
  87. public void AddCacheObject(WebDavMethodHandlerBase handler, string user, string path, object value)
  88. {
  89. WebDavCacheBase pathCache = (WebDavCacheBase) _cache.GetCachedObject(path);
  90. WebDavCacheBase handlerCache;
  91. if (pathCache != null)
  92. {
  93. handlerCache = (WebDavCacheBase) pathCache.GetCachedObject(handler.GetType().FullName);
  94. if (handlerCache != null)
  95. {
  96. handlerCache.AddCacheObject(user, value);
  97. return;
  98. }
  99. handlerCache = new WebDavCacheBase();
  100. handlerCache.AddCacheObject(user, value);
  101. pathCache.AddCacheObject(handler.GetType().FullName, handlerCache);
  102. return;
  103. }
  104. pathCache = new WebDavCacheBase();
  105. handlerCache = new WebDavCacheBase();
  106. handlerCache.AddCacheObject(user, value);
  107. pathCache.AddCacheObject(handler.GetType().FullName, handlerCache);
  108. _cache.AddCacheObject(path, pathCache);
  109. }
  110. /// <summary>
  111. /// </summary>
  112. /// <param name="handler"></param>
  113. /// <param name="user"></param>
  114. /// <param name="path"></param>
  115. /// <returns></returns>
  116. public object GetCacheObject(WebDavMethodHandlerBase handler, string user, string path)
  117. {
  118. return ((WebDavCacheBase) (((WebDavCacheBase) _cache.GetCachedObject(path))?.GetCachedObject(handler.GetType().FullName)))?.GetCachedObject(user);
  119. }
  120. /// <summary>
  121. /// </summary>
  122. /// <param name="key"></param>
  123. public void RemoveCacheObject(string key)
  124. {
  125. _cache.RemoveCacheObject(key);
  126. }
  127. #endregion
  128. }
  129. }