HTTPCacheDatabase.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Best.HTTP.Shared;
  5. using Best.HTTP.Shared.Databases;
  6. using Best.HTTP.Shared.Databases.Indexing;
  7. using Best.HTTP.Shared.Databases.Indexing.Comparers;
  8. using Best.HTTP.Shared.Databases.MetadataIndexFinders;
  9. using Best.HTTP.Shared.Databases.Utils;
  10. using Best.HTTP.Shared.Extensions;
  11. using Best.HTTP.Shared.Logger;
  12. using Best.HTTP.Shared.PlatformSupport.Threading;
  13. using UnityEngine;
  14. namespace Best.HTTP.Caching
  15. {
  16. struct v128View
  17. {
  18. public ulong low;
  19. public ulong high;
  20. }
  21. /// <summary>
  22. /// Possible lock-states a cache-content can be in.
  23. /// </summary>
  24. public enum LockTypes : byte
  25. {
  26. /// <summary>
  27. /// No reads or writes are happening on the cached content.
  28. /// </summary>
  29. Unlocked,
  30. /// <summary>
  31. /// There's one writer operating on the cached content. No other writes or reads allowed while this lock is held on the content.
  32. /// </summary>
  33. Write,
  34. /// <summary>
  35. /// There's at least one read operation happening on the cached content. No writes allowed while this lock is held on the content.
  36. /// </summary>
  37. Read
  38. }
  39. /// <summary>
  40. /// Metadata stored for every cached content. It contains only limited data about the content to help early cache decision making and cache management.
  41. /// </summary>
  42. internal class CacheMetadata : Metadata
  43. {
  44. /// <summary>
  45. /// Unique hash of the cached content, generated by <see cref="HTTPCache.CalculateHash(HTTPMethods, Uri)"/>.
  46. /// </summary>
  47. public UnityEngine.Hash128 Hash { get; set; }
  48. /// <summary>
  49. /// Size of the stored content in bytes.
  50. /// </summary>
  51. public ulong ContentLength { get; set; }
  52. /// <summary>
  53. /// When the last time the content is accessed. Also initialized when the initial download completes.
  54. /// </summary>
  55. public DateTime LastAccessTime { get; set; }
  56. /// <summary>
  57. /// What kind of lock the content is currently in.
  58. /// </summary>
  59. public LockTypes Lock { get; set; }
  60. /// <summary>
  61. /// Number of readers.
  62. /// </summary>
  63. public int ReadLockCount { get; set; }
  64. public unsafe override void SaveTo(Stream stream)
  65. {
  66. base.SaveTo(stream);
  67. var hash = this.Hash;
  68. v128View view = *(v128View*)&hash;
  69. stream.EncodeUnsignedVariableByteInteger(view.low);
  70. stream.EncodeUnsignedVariableByteInteger(view.high);
  71. stream.EncodeUnsignedVariableByteInteger(ContentLength);
  72. stream.EncodeSignedVariableByteInteger(LastAccessTime.ToBinary() >> CacheMetadataContentParser.PrecisionShift);
  73. // Only Write locks should persist as Reads doesn't alter the cached content
  74. if (this.Lock == LockTypes.Write)
  75. stream.EncodeUnsignedVariableByteInteger((byte)this.Lock);
  76. else
  77. stream.EncodeUnsignedVariableByteInteger((byte)LockTypes.Unlocked);
  78. }
  79. public unsafe override void LoadFrom(Stream stream)
  80. {
  81. base.LoadFrom(stream);
  82. var hash = default(v128View);
  83. hash.low = stream.DecodeUnsignedVariableByteInteger();
  84. hash.high = stream.DecodeUnsignedVariableByteInteger();
  85. this.Hash = *(UnityEngine.Hash128*)&hash;
  86. this.ContentLength = stream.DecodeUnsignedVariableByteInteger();
  87. this.LastAccessTime = DateTime.FromBinary(stream.DecodeSignedVariableByteInteger() << CacheMetadataContentParser.PrecisionShift);
  88. this.Lock = (LockTypes)stream.DecodeUnsignedVariableByteInteger();
  89. }
  90. public override string ToString() => $"[Metadata {Hash}, {ContentLength:N0}, {Lock}, {ReadLockCount}]";
  91. }
  92. /// <summary>
  93. /// Possible caching flags that a `Cache-Control` header can send.
  94. /// </summary>
  95. [Flags]
  96. public enum CacheFlags : byte
  97. {
  98. /// <summary>
  99. /// No special treatment required.
  100. /// </summary>
  101. None = 0x00,
  102. /// <summary>
  103. /// Indicates whether the entity must be revalidated with the server or can be serverd directly from the cache without touching the server when the content is considered stale.
  104. /// </summary>
  105. /// <remarks>
  106. /// More details can be found here:
  107. /// <list type="bullet">
  108. /// <item><description><see href="https://www.rfc-editor.org/rfc/rfc9111.html#name-must-revalidate"/></description></item>
  109. /// </list>
  110. /// </remarks>
  111. MustRevalidate = 0x01,
  112. /// <summary>
  113. /// If it's true, the client always have to revalidate the cached content when it's stale.
  114. /// </summary>
  115. /// <remarks>
  116. /// More details can be found here:
  117. /// <list type="bullet">
  118. /// <item><description><see href="https://www.rfc-editor.org/rfc/rfc9111.html#name-no-cache-2"/></description></item>
  119. /// </list>
  120. /// </remarks>
  121. NoCache = 0x02
  122. }
  123. /// <summary>
  124. /// Cached content associated with a <see cref="CacheMetadata"/>.
  125. /// </summary>
  126. /// <remarks>This is NOT the cached content received from the server! It's for storing caching values to decide on how the content can be used.</remarks>
  127. internal sealed class CacheMetadataContent
  128. {
  129. /// <summary>
  130. /// ETag of the entity.
  131. /// </summary>
  132. /// <remarks>
  133. /// More details can be found here:
  134. /// <list type="bullet">
  135. /// <item><description><see href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag"/></description></item>
  136. /// </list>
  137. /// </remarks>
  138. public string ETag;
  139. /// <summary>
  140. /// LastModified date of the entity. Use ToString("r") to convert it to the format defined in RFC 1123.
  141. /// </summary>
  142. public DateTime LastModified = DateTime.MinValue;
  143. /// <summary>
  144. /// When the cache will expire.
  145. /// </summary>
  146. /// <remarks>
  147. /// More details can be found here:
  148. /// <list type="bullet">
  149. /// <item><description><see href="https://www.rfc-editor.org/rfc/rfc9111.html#name-expires"/></description></item>
  150. /// </list>
  151. /// </remarks>
  152. public DateTime Expires = DateTime.MinValue;
  153. /// <summary>
  154. /// The age that came with the response
  155. /// </summary>
  156. /// <remarks>
  157. /// More details can be found here:
  158. /// <list type="bullet">
  159. /// <item><description><see href="https://www.rfc-editor.org/rfc/rfc9111.html#name-age"/></description></item>
  160. /// </list>
  161. /// </remarks>
  162. public uint Age;
  163. /// <summary>
  164. /// Maximum how long the entry should served from the cache without revalidation.
  165. /// </summary>
  166. /// <remarks>
  167. /// More details can be found here:
  168. /// <list type="bullet">
  169. /// <item><description><see href="https://www.rfc-editor.org/rfc/rfc9111.html#name-max-age-2"/></description></item>
  170. /// </list>
  171. /// </remarks>
  172. public uint MaxAge;
  173. /// <summary>
  174. /// The Date that came with the response.
  175. /// </summary>
  176. /// <remarks>
  177. /// More details can be found here:
  178. /// <list type="bullet">
  179. /// <item><description><see href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Date"/></description></item>
  180. /// </list>
  181. /// </remarks>
  182. public DateTime Date = DateTime.MinValue;
  183. /// <summary>
  184. /// It's a grace period to serve staled content without revalidation.
  185. /// </summary>
  186. /// <remarks>
  187. /// More details can be found here:
  188. /// <list type="bullet">
  189. /// <item><description><see href="https://www.rfc-editor.org/rfc/rfc5861.html#section-3"/></description></item>
  190. /// </list>
  191. /// </remarks>
  192. public uint StaleWhileRevalidate;
  193. /// <summary>
  194. /// Allows the client to serve stale content if the server responds with an 5xx error.
  195. /// </summary>
  196. /// <remarks>
  197. /// More details can be found here:
  198. /// <list type="bullet">
  199. /// <item><description><see href="https://www.rfc-editor.org/rfc/rfc5861.html#section-4"/></description></item>
  200. /// </list>
  201. /// </remarks>
  202. public uint StaleIfError;
  203. /// <summary>
  204. /// bool values packed into one single flag.
  205. /// </summary>
  206. public CacheFlags Flags = CacheFlags.None;
  207. /// <summary>
  208. /// The value of the clock at the time of the request that resulted in the stored response.
  209. /// </summary>
  210. /// <remarks>
  211. /// More details can be found here:
  212. /// <list type="bullet">
  213. /// <item><description><see href="https://www.rfc-editor.org/rfc/rfc9111.html#section-4.2.3-3.8"/></description></item>
  214. /// </list>
  215. /// </remarks>
  216. public DateTime RequestTime = DateTime.MinValue;
  217. /// <summary>
  218. /// The value of the clock at the time the response was received.
  219. /// </summary>
  220. public DateTime ResponseTime = DateTime.MinValue;
  221. public CacheMetadataContent()
  222. {
  223. }
  224. internal void From(Dictionary<string, List<string>> headers)
  225. {
  226. this.ETag = headers.GetFirstHeaderValue("ETag").ToStr(this.ETag ?? string.Empty);
  227. this.Expires = headers.GetFirstHeaderValue("Expires").ToDateTime(this.Expires);
  228. if (this.Expires < DateTime.Now)
  229. this.Expires = DateTime.MinValue;
  230. this.LastModified = headers.GetFirstHeaderValue("Last-Modified").ToDateTime(DateTime.MinValue);
  231. this.Age = headers.GetFirstHeaderValue("Age").ToUInt32(this.Age);
  232. this.Date = headers.GetFirstHeaderValue("Date").ToDateTime(this.Date);
  233. // https://www.rfc-editor.org/rfc/rfc9111.html#section-4.2.1-4
  234. // When there is more than one value present for a given directive
  235. // (e.g., two Expires header field lines or multiple Cache-Control: max-age directives),
  236. // either the first occurrence should be used or the response should be considered stale.
  237. var cacheControl = headers.GetFirstHeaderValue("cache-control");
  238. if (!string.IsNullOrEmpty(cacheControl))
  239. {
  240. HeaderParser parser = new HeaderParser(cacheControl);
  241. if (parser.Values != null)
  242. {
  243. this.Flags = CacheFlags.None;
  244. for (int i = 0; i < parser.Values.Count; ++i)
  245. {
  246. var kvp = parser.Values[i];
  247. switch (kvp.Key.ToLowerInvariant())
  248. {
  249. // https://www.rfc-editor.org/rfc/rfc9111.html#name-max-age-2
  250. case "max-age":
  251. if (kvp.HasValue)
  252. {
  253. // Some cache proxies will return float values
  254. double maxAge;
  255. if (double.TryParse(kvp.Value, out maxAge) && maxAge >= 0)
  256. this.MaxAge = (uint)maxAge;
  257. else
  258. this.MaxAge = 0;
  259. }
  260. else
  261. this.MaxAge = 0;
  262. // https://www.rfc-editor.org/rfc/rfc9111.html#section-5.3-8
  263. // https://www.rfc-editor.org/rfc/rfc9111.html#cache-response-directive.s-maxage
  264. // If a response includes a Cache-Control header field with the max-age directive (Section 5.2.2.1), a recipient MUST ignore the Expires header field.
  265. this.Expires = DateTime.MinValue;
  266. break;
  267. // https://www.rfc-editor.org/rfc/rfc9111.html#name-must-revalidate
  268. case "must-revalidate": this.Flags |= CacheFlags.MustRevalidate; break;
  269. // https://www.rfc-editor.org/rfc/rfc9111.html#name-no-cache-2
  270. case "no-cache": this.Flags |= CacheFlags.NoCache; break;
  271. // https://www.rfc-editor.org/rfc/rfc5861.html#section-3
  272. case "stale-while-revalidate": this.StaleWhileRevalidate = kvp.HasValue ? kvp.Value.ToUInt32(0) : 0; break;
  273. // https://www.rfc-editor.org/rfc/rfc5861.html#section-4
  274. case "stale-if-error": this.StaleIfError = kvp.HasValue ? kvp.Value.ToUInt32(0) : 0; break;
  275. }
  276. }
  277. }
  278. }
  279. }
  280. }
  281. internal sealed class CacheMetadataContentParser : IDiskContentParser<CacheMetadataContent>
  282. {
  283. public const int PrecisionShift = 24;
  284. public void Encode(Stream stream, CacheMetadataContent content)
  285. {
  286. stream.EncodeUnsignedVariableByteInteger(content.MaxAge);
  287. stream.WriteLengthPrefixedString(content.ETag);
  288. stream.EncodeSignedVariableByteInteger(content.LastModified.ToBinary() >> PrecisionShift);
  289. stream.EncodeSignedVariableByteInteger(content.Expires.ToBinary() >> PrecisionShift);
  290. stream.EncodeUnsignedVariableByteInteger(content.Age);
  291. stream.EncodeSignedVariableByteInteger(content.Date.ToBinary() >> PrecisionShift);
  292. stream.EncodeUnsignedVariableByteInteger((byte)content.Flags);
  293. stream.EncodeUnsignedVariableByteInteger(content.StaleWhileRevalidate);
  294. stream.EncodeUnsignedVariableByteInteger(content.StaleIfError);
  295. stream.EncodeSignedVariableByteInteger(content.RequestTime.ToBinary() >> PrecisionShift);
  296. stream.EncodeSignedVariableByteInteger(content.ResponseTime.ToBinary() >> PrecisionShift);
  297. }
  298. public CacheMetadataContent Parse(Stream stream, int length)
  299. {
  300. CacheMetadataContent content = new CacheMetadataContent();
  301. content.MaxAge = (uint)stream.DecodeUnsignedVariableByteInteger();
  302. content.ETag = stream.ReadLengthPrefixedString();
  303. content.LastModified = DateTime.FromBinary(stream.DecodeSignedVariableByteInteger() << PrecisionShift);
  304. content.Expires = DateTime.FromBinary(stream.DecodeSignedVariableByteInteger() << PrecisionShift);
  305. content.Age = (uint)stream.DecodeUnsignedVariableByteInteger();
  306. content.Date = DateTime.FromBinary(stream.DecodeSignedVariableByteInteger() << PrecisionShift);
  307. content.Flags = (CacheFlags)stream.DecodeUnsignedVariableByteInteger();
  308. content.StaleWhileRevalidate = (uint)stream.DecodeUnsignedVariableByteInteger();
  309. content.StaleIfError = (uint)stream.DecodeUnsignedVariableByteInteger();
  310. content.RequestTime = DateTime.FromBinary(stream.DecodeSignedVariableByteInteger() << PrecisionShift);
  311. content.ResponseTime = DateTime.FromBinary(stream.DecodeSignedVariableByteInteger() << PrecisionShift);
  312. return content;
  313. }
  314. }
  315. internal sealed class CacheMetadataIndexingService : IndexingService<CacheMetadataContent, CacheMetadata>
  316. {
  317. private AVLTree<UnityEngine.Hash128, int> index_Hash = new AVLTree<UnityEngine.Hash128, int>(new Hash128Comparer());
  318. public override void Index(CacheMetadata metadata)
  319. {
  320. base.Index(metadata);
  321. this.index_Hash.Add(metadata.Hash, metadata.Index);
  322. }
  323. public override void Remove(CacheMetadata metadata)
  324. {
  325. base.Remove(metadata);
  326. this.index_Hash.Remove(metadata.Hash);
  327. }
  328. public override void Clear()
  329. {
  330. base.Clear();
  331. this.index_Hash.Clear();
  332. }
  333. public override IEnumerable<int> GetOptimizedIndexes() => this.index_Hash.WalkHorizontal();
  334. public bool ContainsHash(UnityEngine.Hash128 hash) => this.index_Hash.ContainsKey(hash);
  335. public List<int> FindByHash(UnityEngine.Hash128 hash) => this.index_Hash.Find(hash);
  336. }
  337. internal sealed class CacheMetadataService : MetadataService<CacheMetadata, CacheMetadataContent>
  338. {
  339. public CacheMetadataService(IndexingService<CacheMetadataContent, CacheMetadata> indexingService, IEmptyMetadataIndexFinder<CacheMetadata> emptyMetadataIndexFinder)
  340. : base(indexingService, emptyMetadataIndexFinder)
  341. {
  342. }
  343. public override CacheMetadata CreateFrom(Stream stream)
  344. {
  345. return base.CreateFrom(stream);
  346. }
  347. public CacheMetadata Create(UnityEngine.Hash128 hash, CacheMetadataContent value, int filePos, int length)
  348. {
  349. var result = base.CreateDefault(value, filePos, length, (content, metadata) => {
  350. metadata.Hash = hash;
  351. });
  352. return result;
  353. }
  354. }
  355. internal sealed class CacheDatabaseOptions : DatabaseOptions
  356. {
  357. public CacheDatabaseOptions() : base("CacheDatabase")
  358. {
  359. base.UseHashFile = false;
  360. }
  361. }
  362. internal sealed class HTTPCacheDatabase : Database<CacheMetadataContent, CacheMetadata, CacheMetadataIndexingService, CacheMetadataService>
  363. {
  364. public HTTPCacheDatabase(string directory)
  365. : this(directory, new CacheDatabaseOptions(), new CacheMetadataIndexingService())
  366. {
  367. }
  368. private HTTPCacheDatabase(string directory,
  369. DatabaseOptions options,
  370. CacheMetadataIndexingService indexingService)
  371. : base(directory, options, indexingService, new CacheMetadataContentParser(), new CacheMetadataService(indexingService, new FindDeletedMetadataIndexFinder<CacheMetadata>()))
  372. {
  373. }
  374. public CacheMetadataContent FindByHashAndUpdateRequestTime(UnityEngine.Hash128 hash, LoggingContext context)
  375. {
  376. if (HTTPManager.Logger.IsDiagnostic)
  377. HTTPManager.Logger.Verbose(nameof(HTTPCacheDatabase), $"{nameof(FindByHashAndUpdateRequestTime)}({hash})", context);
  378. if (!hash.isValid)
  379. return default;
  380. using var _ = new WriteLock(this.rwlock);
  381. var (content, metadata) = FindContentAndMetadata(hash);
  382. if (content != null)
  383. {
  384. content.RequestTime = DateTime.Now;
  385. UpdateMetadataAndContent(metadata, content);
  386. }
  387. return content;
  388. }
  389. public bool TryAcquireWriteLock(Hash128 hash, Dictionary<string, List<string>> headers, LoggingContext context)
  390. {
  391. if (HTTPManager.Logger.IsDiagnostic)
  392. HTTPManager.Logger.Verbose(nameof(HTTPCacheDatabase), $"{nameof(TryAcquireWriteLock)}({hash}, {headers?.Count})", context);
  393. if (!hash.isValid)
  394. return false;
  395. using var _ = new WriteLock(this.rwlock);
  396. // FindMetadata filters out logically deleted entries, what we need here because we want to load it too.
  397. var metadata = FindMetadata(hash);
  398. CacheMetadataContent content = null;
  399. if (metadata != null)
  400. {
  401. if (HTTPManager.Logger.IsDiagnostic)
  402. HTTPManager.Logger.Verbose(nameof(HTTPCacheDatabase), $"{nameof(TryAcquireWriteLock)} - Metadata found: {metadata}", context);
  403. if (metadata.Lock != LockTypes.Unlocked)
  404. return false;
  405. metadata.Lock = LockTypes.Write;
  406. content = this.FromMetadata(metadata);
  407. content.From(headers);
  408. UpdateMetadataAndContent(metadata, content);
  409. }
  410. else
  411. {
  412. if (HTTPManager.Logger.IsDiagnostic)
  413. HTTPManager.Logger.Verbose(nameof(HTTPCacheDatabase), $"{nameof(TryAcquireWriteLock)} - Creating new DB entry", context);
  414. content = new CacheMetadataContent();
  415. content.RequestTime = DateTime.Now;
  416. content.From(headers);
  417. (int filePos, int length) = this.DiskManager.Append(content);
  418. metadata = this.MetadataService.Create(hash, content, filePos, length);
  419. metadata.Lock = LockTypes.Write;
  420. }
  421. FlagDirty(1);
  422. return true;
  423. }
  424. public bool Update(Hash128 hash, Dictionary<string, List<string>> headers, LoggingContext context)
  425. {
  426. if (HTTPManager.Logger.IsDiagnostic)
  427. HTTPManager.Logger.Verbose(nameof(HTTPCacheDatabase), $"{nameof(Update)}({hash}, {headers?.Count})", context);
  428. if (!hash.isValid)
  429. return false;
  430. using var _ = new WriteLock(this.rwlock);
  431. var (content, metadata) = FindContentAndMetadata(hash);
  432. if (content != null)
  433. {
  434. content.From(headers);
  435. content.ResponseTime = DateTime.Now;
  436. UpdateMetadataAndContent(metadata, content);
  437. }
  438. return content != null;
  439. }
  440. public void ReleaseWriteLock(Hash128 hash, ulong length, LoggingContext context)
  441. {
  442. if (HTTPManager.Logger.IsDiagnostic)
  443. HTTPManager.Logger.Verbose(nameof(HTTPCacheDatabase), $"{nameof(ReleaseWriteLock)}({hash}, {length:N0})", context);
  444. if (!hash.isValid)
  445. return;
  446. using var _ = new WriteLock(this.rwlock);
  447. var (content, metadata) = FindContentAndMetadata(hash);
  448. if (content == null)
  449. {
  450. HTTPManager.Logger.Warning(nameof(HTTPCacheDatabase), $"{nameof(ReleaseWriteLock)} - Couldn't find content!", context);
  451. return;
  452. }
  453. if (metadata.Lock != LockTypes.Write)
  454. HTTPManager.Logger.Error(nameof(HTTPCacheDatabase), $"{nameof(ReleaseWriteLock)} - Is NOT Write Locked! {metadata}", context);
  455. metadata.Lock = LockTypes.Unlocked;
  456. if (content != null)
  457. {
  458. metadata.ContentLength = length;
  459. var now = DateTime.Now;
  460. metadata.LastAccessTime = now;
  461. content.ResponseTime = now;
  462. UpdateMetadataAndContent(metadata, content);
  463. }
  464. FlagDirty(1);
  465. }
  466. public bool TryAcquireReadLock(Hash128 hash, LoggingContext context)
  467. {
  468. if (HTTPManager.Logger.IsDiagnostic)
  469. HTTPManager.Logger.Verbose(nameof(HTTPCacheDatabase), $"{nameof(TryAcquireReadLock)}({hash})", context);
  470. if (!hash.isValid)
  471. return false;
  472. using var _ = new WriteLock(this.rwlock);
  473. var metadata = FindMetadata(hash);
  474. if (metadata == null)
  475. return false;
  476. if (metadata.Lock == LockTypes.Write)
  477. return false;
  478. metadata.Lock = LockTypes.Read;
  479. // we are behind a write lock, it's safe to increment it like this
  480. metadata.ReadLockCount++;
  481. if (HTTPManager.Logger.IsDiagnostic)
  482. HTTPManager.Logger.Verbose(nameof(HTTPCacheDatabase), $"{nameof(TryAcquireReadLock)} - {metadata}", context);
  483. return true;
  484. }
  485. public void ReleaseReadLock(Hash128 hash, LoggingContext context)
  486. {
  487. if (HTTPManager.Logger.IsDiagnostic)
  488. HTTPManager.Logger.Verbose(nameof(HTTPCacheDatabase), $"{nameof(ReleaseReadLock)}({hash})", context);
  489. if (!hash.isValid)
  490. return;
  491. using var _ = new WriteLock(this.rwlock);
  492. var metadata = FindMetadata(hash);
  493. if (metadata == null)
  494. {
  495. HTTPManager.Logger.Warning(nameof(HTTPCacheDatabase), $"{nameof(ReleaseReadLock)} - Couldn't find metadata!", context);
  496. return;
  497. }
  498. if (metadata.Lock != LockTypes.Read)
  499. HTTPManager.Logger.Warning(nameof(HTTPCacheDatabase), $"{nameof(ReleaseReadLock)} - Is NOT Locked!", context);
  500. if (metadata.ReadLockCount == 0)
  501. HTTPManager.Logger.Error(nameof(HTTPCacheDatabase), $"{nameof(ReleaseReadLock)} - ReadLockCount already zero!", context);
  502. if (--metadata.ReadLockCount == 0)
  503. metadata.Lock = LockTypes.Unlocked;
  504. if (HTTPManager.Logger.IsDiagnostic)
  505. HTTPManager.Logger.Verbose(nameof(HTTPCacheDatabase), $"{nameof(ReleaseReadLock)} - {metadata}", context);
  506. }
  507. internal ulong Delete(Hash128 hash, LoggingContext context)
  508. {
  509. if (HTTPManager.Logger.IsDiagnostic)
  510. HTTPManager.Logger.Verbose(nameof(HTTPCacheDatabase), $"{nameof(Delete)}({hash})", context);
  511. if (!hash.isValid)
  512. return 0;
  513. using var _ = new WriteLock(this.rwlock);
  514. // Don't use FindMetadata, because it would return null for a logically deleted metadata
  515. // so DeleteMetadata wouldn't be called!
  516. var byHash = this.IndexingService.FindByHash(hash);
  517. if (byHash == null || byHash.Count == 0)
  518. return 0;
  519. var metadata = this.MetadataService.Metadatas[byHash[0]];
  520. if (metadata == null)
  521. return 0;
  522. var contentLength = metadata.ContentLength;
  523. base.DeleteMetadata(metadata);
  524. return contentLength;
  525. }
  526. public void EnterWriteLock(LoggingContext context)
  527. {
  528. if (HTTPManager.Logger.IsDiagnostic)
  529. HTTPManager.Logger.Verbose(nameof(HTTPCacheDatabase), $"{nameof(EnterWriteLock)}()", context);
  530. this.rwlock.EnterWriteLock();
  531. }
  532. public void ExitWriteLock(LoggingContext context)
  533. {
  534. if (HTTPManager.Logger.IsDiagnostic)
  535. HTTPManager.Logger.Verbose(nameof(HTTPCacheDatabase), $"{nameof(ExitWriteLock)}()", context);
  536. this.rwlock.ExitWriteLock();
  537. }
  538. public void UpdateLastAccessTime(Hash128 hash, LoggingContext context)
  539. {
  540. if (HTTPManager.Logger.IsDiagnostic)
  541. HTTPManager.Logger.Verbose(nameof(HTTPCacheDatabase), $"{nameof(UpdateLastAccessTime)}({hash})", context);
  542. if (!hash.isValid)
  543. return;
  544. using var _ = new WriteLock(this.rwlock);
  545. var metadata = FindMetadata(hash);
  546. if (metadata != null)
  547. {
  548. metadata.LastAccessTime = DateTime.Now;
  549. FlagDirty(1);
  550. }
  551. }
  552. private CacheMetadata FindMetadata(Hash128 hash)
  553. {
  554. var byHash = this.IndexingService.FindByHash(hash);
  555. if (byHash == null || byHash.Count == 0)
  556. return null;
  557. var metadata = this.MetadataService.Metadatas[byHash[0]];
  558. if (metadata != null && metadata.IsDeleted)
  559. return null;
  560. return metadata;
  561. }
  562. public (CacheMetadataContent, CacheMetadata) FindContentAndMetadataLocked(Hash128 hash)
  563. {
  564. using var _ = new WriteLock(this.rwlock);
  565. return FindContentAndMetadata(hash);
  566. }
  567. private (CacheMetadataContent, CacheMetadata) FindContentAndMetadata(Hash128 hash)
  568. {
  569. var byHash = this.IndexingService.FindByHash(hash);
  570. if (byHash == null || byHash.Count == 0)
  571. return (null, null);
  572. var metadata = this.MetadataService.Metadatas[byHash[0]];
  573. if (metadata != null && metadata.IsDeleted)
  574. return (null, null);
  575. var content = this.FromMetadataIndex(metadata.Index);
  576. return (content, metadata);
  577. }
  578. private void UpdateMetadataAndContent(Metadata metadata, CacheMetadataContent content)
  579. {
  580. this.DiskManager.SaveChanged(metadata, content);
  581. FlagDirty(1);
  582. }
  583. }
  584. }