11cb0ef41Sopenharmony_ci'use strict'
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst { LRUCache } = require('lru-cache')
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst MEMOIZED = new LRUCache({
61cb0ef41Sopenharmony_ci  max: 500,
71cb0ef41Sopenharmony_ci  maxSize: 50 * 1024 * 1024, // 50MB
81cb0ef41Sopenharmony_ci  ttl: 3 * 60 * 1000, // 3 minutes
91cb0ef41Sopenharmony_ci  sizeCalculation: (entry, key) => key.startsWith('key:') ? entry.data.length : entry.length,
101cb0ef41Sopenharmony_ci})
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cimodule.exports.clearMemoized = clearMemoized
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cifunction clearMemoized () {
151cb0ef41Sopenharmony_ci  const old = {}
161cb0ef41Sopenharmony_ci  MEMOIZED.forEach((v, k) => {
171cb0ef41Sopenharmony_ci    old[k] = v
181cb0ef41Sopenharmony_ci  })
191cb0ef41Sopenharmony_ci  MEMOIZED.clear()
201cb0ef41Sopenharmony_ci  return old
211cb0ef41Sopenharmony_ci}
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_cimodule.exports.put = put
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cifunction put (cache, entry, data, opts) {
261cb0ef41Sopenharmony_ci  pickMem(opts).set(`key:${cache}:${entry.key}`, { entry, data })
271cb0ef41Sopenharmony_ci  putDigest(cache, entry.integrity, data, opts)
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cimodule.exports.put.byDigest = putDigest
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cifunction putDigest (cache, integrity, data, opts) {
331cb0ef41Sopenharmony_ci  pickMem(opts).set(`digest:${cache}:${integrity}`, data)
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cimodule.exports.get = get
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cifunction get (cache, key, opts) {
391cb0ef41Sopenharmony_ci  return pickMem(opts).get(`key:${cache}:${key}`)
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_cimodule.exports.get.byDigest = getDigest
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_cifunction getDigest (cache, integrity, opts) {
451cb0ef41Sopenharmony_ci  return pickMem(opts).get(`digest:${cache}:${integrity}`)
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciclass ObjProxy {
491cb0ef41Sopenharmony_ci  constructor (obj) {
501cb0ef41Sopenharmony_ci    this.obj = obj
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  get (key) {
541cb0ef41Sopenharmony_ci    return this.obj[key]
551cb0ef41Sopenharmony_ci  }
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  set (key, val) {
581cb0ef41Sopenharmony_ci    this.obj[key] = val
591cb0ef41Sopenharmony_ci  }
601cb0ef41Sopenharmony_ci}
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_cifunction pickMem (opts) {
631cb0ef41Sopenharmony_ci  if (!opts || !opts.memoize) {
641cb0ef41Sopenharmony_ci    return MEMOIZED
651cb0ef41Sopenharmony_ci  } else if (opts.memoize.get && opts.memoize.set) {
661cb0ef41Sopenharmony_ci    return opts.memoize
671cb0ef41Sopenharmony_ci  } else if (typeof opts.memoize === 'object') {
681cb0ef41Sopenharmony_ci    return new ObjProxy(opts.memoize)
691cb0ef41Sopenharmony_ci  } else {
701cb0ef41Sopenharmony_ci    return MEMOIZED
711cb0ef41Sopenharmony_ci  }
721cb0ef41Sopenharmony_ci}
73