cache.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { ConsoleLogger } from '../../../mol-util/console-logger'
  7. import { LinkedList } from '../../../mol-data/generic';
  8. import { ModelServerConfig as ServerConfig } from '../config';
  9. interface CacheEntry<T> {
  10. key: string,
  11. approximateSize: number,
  12. timeoutId: NodeJS.Timer | undefined,
  13. item: T
  14. }
  15. type CacheNode<T> = LinkedList.Node<CacheEntry<T>>
  16. export interface CacheParams {
  17. useCache: boolean,
  18. maxApproximateSizeInBytes: number, // = 2 * 1014 * 1024 * 1024; // 2 GB
  19. entryTimeoutInMs: number // = 10 * 60 * 1000; // 10 minutes
  20. }
  21. export class Cache<T> {
  22. private entries = LinkedList<CacheEntry<T>>();
  23. private entryMap = new Map<string, CacheNode<T>>();
  24. private approximateSize = 0;
  25. private clearTimeout(e: CacheNode<T>) {
  26. if (typeof e.value.timeoutId !== 'undefined') {
  27. clearTimeout(e.value.timeoutId);
  28. e.value.timeoutId = void 0;
  29. }
  30. }
  31. private dispose(e: CacheNode<T>) {
  32. this.clearTimeout(e);
  33. if (e.inList) {
  34. this.entries.remove(e);
  35. this.approximateSize -= e.value.approximateSize;
  36. }
  37. this.entryMap.delete(e.value.key);
  38. }
  39. private refresh(e: CacheNode<T>) {
  40. this.clearTimeout(e);
  41. e.value.timeoutId = setTimeout(() => this.expireNode(e), ServerConfig.cacheEntryTimeoutMs);
  42. this.entries.remove(e);
  43. this.entries.addFirst(e.value);
  44. }
  45. private expireNode(e: CacheNode<T>, notify = true) {
  46. if (notify) ConsoleLogger.log('Cache', `${e.value.key} expired.`);
  47. this.dispose(e);
  48. }
  49. expireAll() {
  50. for (let e = this.entries.first; e; e = e.next) this.expireNode(e, false);
  51. }
  52. expire(key: string) {
  53. const entry = this.entryMap.get(key);
  54. if (!entry) return;
  55. this.expireNode(entry);
  56. }
  57. add(item: T) {
  58. const key = this.keyGetter(item);
  59. const approximateSize = this.sizeGetter(item);
  60. if (this.entryMap.has(key)) this.dispose(this.entryMap.get(key)!);
  61. if (ServerConfig.cacheMaxSizeInBytes < this.approximateSize + approximateSize) {
  62. if (this.entries.last) this.dispose(this.entries.last);
  63. }
  64. this.approximateSize += approximateSize;
  65. const entry: CacheEntry<T> = { key, approximateSize, timeoutId: void 0, item };
  66. const e = this.entries.addFirst(entry);
  67. this.entryMap.set(key, e);
  68. this.refresh(e);
  69. ConsoleLogger.log('Cache', `${key} added.`);
  70. return item;
  71. }
  72. has(key: string) {
  73. return this.entryMap.has(key);
  74. }
  75. get(key: string) {
  76. if (!this.entryMap.has(key)) return void 0;
  77. let e = this.entryMap.get(key)!;
  78. this.refresh(e);
  79. ConsoleLogger.log('Cache', `${key} accessed.`);
  80. return e.value.item;
  81. }
  82. constructor(private keyGetter: (i: T) => string, private sizeGetter: (i: T) => number) {
  83. }
  84. }