Sha256: 9c434871d32f57f7c165b6eb3707eb516340404c9c7c218c1b9846b22ec6b7b2

Contents?: true

Size: 1.45 KB

Versions: 11

Compression:

Stored size: 1.45 KB

Contents

'use strict'

const LRU = require('lru-cache')

const MAX_SIZE = 50 * 1024 * 1024 // 50MB
const MAX_AGE = 3 * 60 * 1000

const MEMOIZED = new LRU({
  max: MAX_SIZE,
  maxAge: MAX_AGE,
  length: (entry, key) => key.startsWith('key:') ? entry.data.length : entry.length
})

module.exports.clearMemoized = clearMemoized

function clearMemoized () {
  const old = {}
  MEMOIZED.forEach((v, k) => {
    old[k] = v
  })
  MEMOIZED.reset()
  return old
}

module.exports.put = put

function put (cache, entry, data, opts) {
  pickMem(opts).set(`key:${cache}:${entry.key}`, { entry, data })
  putDigest(cache, entry.integrity, data, opts)
}

module.exports.put.byDigest = putDigest

function putDigest (cache, integrity, data, opts) {
  pickMem(opts).set(`digest:${cache}:${integrity}`, data)
}

module.exports.get = get

function get (cache, key, opts) {
  return pickMem(opts).get(`key:${cache}:${key}`)
}

module.exports.get.byDigest = getDigest

function getDigest (cache, integrity, opts) {
  return pickMem(opts).get(`digest:${cache}:${integrity}`)
}

class ObjProxy {
  constructor (obj) {
    this.obj = obj
  }

  get (key) {
    return this.obj[key]
  }

  set (key, val) {
    this.obj[key] = val
  }
}

function pickMem (opts) {
  if (!opts || !opts.memoize) {
    return MEMOIZED
  } else if (opts.memoize.get && opts.memoize.set) {
    return opts.memoize
  } else if (typeof opts.memoize === 'object') {
    return new ObjProxy(opts.memoize)
  } else {
    return MEMOIZED
  }
}

Version data entries

11 entries across 9 versions & 5 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/cacache/lib/memoization.js
tang-0.2.1 spec/tang_app/node_modules/cacache/lib/memoization.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/cacache/lib/memoization.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/cacache/lib/memoization.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/compression-webpack-plugin/node_modules/cacache/lib/memoization.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/cacache/lib/memoization.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/cacache/lib/memoization.js
tang-0.2.0 spec/tang_app/node_modules/cacache/lib/memoization.js
tang-0.1.0 spec/tang_app/node_modules/cacache/lib/memoization.js
tang-0.0.9 spec/tang_app/node_modules/cacache/lib/memoization.js
enju_library-0.3.8 spec/dummy/node_modules/cacache/lib/memoization.js