All files / src/cache IAgingCacheOptions.ts

100% Statements 9/9
100% Branches 4/4
100% Functions 3/3
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86      1x       1x       1x             1x         1x         1x       1x                                                                                     1x 9x                  
/**
 * The algorithm for selecting which element should be replaced first in a cache
 */
export enum AgingCacheReplacementPolicy {
  /**
   * Replace items in a First-in First-out manner
   */
  FIFO,
  /**
   * Replace items by Least Recently Used
   */
  LRU,
}
 
/**
 * In a distributed environment, multiple instances could write to cache at once. This option
 * determines what should happen if an exiting entry is found in a higher level cache.
 */
export enum AgingCacheWriteMode {
  /**
   * When a higher level cache has a key, refresh the lower level keys and only overwrite if
   * the force option is supplied
   */
  RefreshAlways,
  /**
   * When our entry is newer, then allow it to take precedence and overwrite the higher level
   * caches. Refresh the lower level caches if older
   */
  OverwriteAged,
  /**
   * Unconditionally overwrite the value that's stored in higher level caches
   */
  OverwriteAlways,
}
 
/**
 * The set of options used to construct an aging cache
 */
export interface IAgingCacheOptions {
  /**
   * The maximum number of entries to store in the cache, undefined for no max
   * FIFO: Check the lowest level for maximum
   */
  maxEntries?: number;
  /**
   * During a purge, the maximum value of the age marker to keep entries, varies by algorithm
   * FIFO: The maximum time to keep entries in minutes, undefined for no limit
   */
  ageLimit?: number;
  /**
   * The interval to check for old entries in seconds
   */
  purgeInterval: number;
  /**
   * The order elements should be replaced when purging stale entries
   */
  replacementPolicy: AgingCacheReplacementPolicy;
  /**
   * Determine when a value should be overwritten in the storage hierarchy on set
   */
  setMode: AgingCacheWriteMode;
  /**
   * Determine when a value should be overwritten in the storage hierarchy on delete
   */
  deleteMode: AgingCacheWriteMode;
  /**
   * When evicting stale entries, delete from layers below this level. When not set,
   * delete from all levels, thus, making this a cache only with no persistence layer.
   */
  evictAtLevel?: number;
}
 
/**
 * @return Options for a default FIFO cache
 */
export function getDefaultAgingCacheOptions(): IAgingCacheOptions {
  return {
    maxEntries: undefined,
    ageLimit: 200,
    purgeInterval: 60,
    replacementPolicy: AgingCacheReplacementPolicy.FIFO,
    setMode: AgingCacheWriteMode.OverwriteAged,
    deleteMode: AgingCacheWriteMode.OverwriteAged,
  };
}