{"version":3,"sources":["../../../../src/cache/facades/local-cache.ts","../../../../src/serializers/json.ts","../../../../src/cache/cache-entry/cache-entry.ts"],"sourcesContent":["import { CacheEntry } from '../cache-entry/cache-entry';\nimport type { Logger, L1CacheDriver } from '../../types/main';\nimport type { CacheEntryOptions } from '../cache-entry/cache-entry-options';\n\n/**\n * LocalCache is a wrapper around a CacheDriver that provides a\n * some handy methods for interacting with a local cache ( in-memory )\n */\nexport class LocalCache {\n  #driver: L1CacheDriver;\n  #logger: Logger;\n\n  constructor(driver: L1CacheDriver, logger: Logger) {\n    this.#driver = driver;\n    this.#logger = logger.child({ context: 'mastercache.localCache' });\n  }\n\n  /**\n   * Get an item from the local cache\n   */\n  get(key: string, options: CacheEntryOptions) {\n    /**\n     * Try to get the item from the local cache\n     */\n    this.#logger.trace({ key, opId: options.id }, 'try getting local cache item');\n    const value = this.#driver.get(key);\n\n    /**\n     * If the item is not found, return undefined\n     */\n    if (value === undefined) {\n      this.#logger.trace({ key, opId: options.id }, 'local cache item not found');\n      return;\n    }\n\n    return CacheEntry.fromDriver(key, value);\n  }\n\n  /**\n   * Set a new item in the local cache\n   */\n  set(key: string, value: string, options: CacheEntryOptions) {\n    /**\n     * If grace period is disabled and Physical TTL is 0 or less, we can just delete the item.\n     */\n    if (!options.isGracePeriodEnabled && options.physicalTtl && options.physicalTtl <= 0) {\n      return this.delete(key, options);\n    }\n\n    /**\n     * Save the item to the local cache\n     */\n    this.#logger.trace({ key, value, opId: options.id }, 'saving local cache item');\n    this.#driver.set(key, value, options.physicalTtl);\n  }\n\n  /**\n   * Delete an item from the local cache\n   */\n  delete(key: string, options?: CacheEntryOptions) {\n    this.#logger.trace({ key, opId: options?.id }, 'deleting local cache item');\n    return this.#driver.delete(key);\n  }\n\n  /**\n   * Make an item logically expire in the local cache\n   *\n   * That means that the item will be expired but kept in the cache\n   * in order to be able to return it to the user if the remote cache\n   * is down and the grace period is enabled\n   */\n  logicallyExpire(key: string) {\n    this.#logger.trace({ key }, 'logically expiring local cache item');\n\n    const value = this.#driver.get(key);\n    if (value === undefined) return;\n\n    const newEntry = CacheEntry.fromDriver(key, value).expire().serialize();\n    return this.#driver.set(key, newEntry, this.#driver.getRemainingTtl(key));\n  }\n\n  /**\n   * Delete many item from the local cache\n   */\n  deleteMany(keys: string[], options: CacheEntryOptions) {\n    this.#logger.trace({ keys, options, opId: options.id }, 'deleting local cache items');\n    this.#driver.deleteMany(keys);\n  }\n\n  /**\n   * Create a new namespace for the local cache\n   */\n  namespace(namespace: string) {\n    return this.#driver.namespace(namespace) as L1CacheDriver;\n  }\n\n  /**\n   * Check if an item exists in the local cache\n   */\n  has(key: string) {\n    return this.#driver.has(key);\n  }\n\n  /**\n   * Clear the local cache\n   */\n  clear() {\n    return this.#driver.clear();\n  }\n\n  /**\n   * Disconnect from the local cache\n   */\n  disconnect() {\n    return this.#driver.disconnect();\n  }\n}\n","import type { CacheSerializer } from '../types/main';\n\n/**\n * Simple class to serialize and deserialize values using JSON\n */\nexport class JsonSerializer implements CacheSerializer {\n  serialize(value: unknown) {\n    return JSON.stringify(value);\n  }\n\n  deserialize(value: string) {\n    return JSON.parse(value);\n  }\n}\n","import { JsonSerializer } from '../../serializers/json';\n\n/**\n * Represents a cache entry stored inside a cache driver.\n */\nexport class CacheEntry {\n  /**\n   * The key of the cache item.\n   */\n  #key: string;\n\n  /**\n   * The value of the item.\n   */\n  #value: any;\n\n  /**\n   * The logical expiration is the time in miliseconds when the item\n   * will be considered expired. But, if grace period is enabled,\n   * the item will still be available for a while.\n   */\n  #logicalExpiration: number;\n\n  #earlyExpiration: number;\n\n  static #serializer = new JsonSerializer();\n\n  constructor(key: string, item: Record<string, any>) {\n    this.#key = key;\n    this.#value = item.value;\n    this.#logicalExpiration = item.logicalExpiration;\n    this.#earlyExpiration = item.earlyExpiration;\n  }\n\n  getValue() {\n    return this.#value;\n  }\n\n  getKey() {\n    return this.#key;\n  }\n\n  getLogicalExpiration() {\n    return this.#logicalExpiration;\n  }\n\n  getEarlyExpiration() {\n    return this.#earlyExpiration;\n  }\n\n  isLogicallyExpired() {\n    return Date.now() >= this.#logicalExpiration;\n  }\n\n  isEarlyExpired() {\n    if (!this.#earlyExpiration) {\n      return false;\n    }\n\n    if (this.isLogicallyExpired()) {\n      return false;\n    }\n\n    return Date.now() >= this.#earlyExpiration;\n  }\n\n  static fromDriver(key: string, item: string) {\n    return new CacheEntry(key, this.#serializer.deserialize(item));\n  }\n\n  applyFallbackDuration(duration: number) {\n    this.#logicalExpiration += duration;\n    this.#earlyExpiration = 0;\n    return this;\n  }\n\n  expire() {\n    this.#logicalExpiration = Date.now() - 100;\n    this.#earlyExpiration = 0;\n    return this;\n  }\n\n  serialize() {\n    return CacheEntry.#serializer.serialize({\n      value: this.#value,\n      logicalExpiration: this.#logicalExpiration,\n      earlyExpiration: this.#earlyExpiration,\n    });\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKO,IAAM,iBAAN,MAAgD;AAAA,EACrD,UAAU,OAAgB;AACxB,WAAO,KAAK,UAAU,KAAK;AAAA,EAC7B;AAAA,EAEA,YAAY,OAAe;AACzB,WAAO,KAAK,MAAM,KAAK;AAAA,EACzB;AACF;;;ACRO,IAAM,aAAN,MAAM,YAAW;AAAA;AAAA;AAAA;AAAA,EAItB;AAAA;AAAA;AAAA;AAAA,EAKA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA;AAAA,EAEA;AAAA,EAEA,OAAO,cAAc,IAAI,eAAe;AAAA,EAExC,YAAY,KAAa,MAA2B;AAClD,SAAK,OAAO;AACZ,SAAK,SAAS,KAAK;AACnB,SAAK,qBAAqB,KAAK;AAC/B,SAAK,mBAAmB,KAAK;AAAA,EAC/B;AAAA,EAEA,WAAW;AACT,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,SAAS;AACP,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,uBAAuB;AACrB,WAAO,KAAK;AAAA,EACd;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