cache.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 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.expire(e), ServerConfig.cacheParams.entryTimeoutInMs);
  42. this.entries.remove(e);
  43. this.entries.addFirst(e.value);
  44. }
  45. private expire(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.expire(e, false);
  51. }
  52. add(item: T) {
  53. const key = this.keyGetter(item);
  54. const approximateSize = this.sizeGetter(item);
  55. if (this.entryMap.has(key)) this.dispose(this.entryMap.get(key)!);
  56. if (ServerConfig.cacheParams.maxApproximateSizeInBytes < this.approximateSize + approximateSize) {
  57. if (this.entries.last) this.dispose(this.entries.last);
  58. }
  59. this.approximateSize += approximateSize;
  60. const entry: CacheEntry<T> = { key, approximateSize, timeoutId: void 0, item };
  61. const e = this.entries.addFirst(entry);
  62. this.entryMap.set(key, e);
  63. this.refresh(e);
  64. ConsoleLogger.log('Cache', `${key} added.`);
  65. return item;
  66. }
  67. has(key: string) {
  68. return this.entryMap.has(key);
  69. }
  70. get(key: string) {
  71. if (!this.entryMap.has(key)) return void 0;
  72. let e = this.entryMap.get(key)!;
  73. this.refresh(e);
  74. ConsoleLogger.log('Cache', `${key} accessed.`);
  75. return e.value.item;
  76. }
  77. constructor(private keyGetter: (i: T) => string, private sizeGetter: (i: T) => number) {
  78. }
  79. }