question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Would be great to have a TTL

See original GitHub issue

That way cache entries will automatically expire and not explode memory usage. This is especially important on the server.

This would likely involve you keeping a separate “shadow cache” that kept the created times of the cacheKeys.

Obviously this would be fractionally slower so a simple opt-in (by say {TTL: [any non-zero value in ms]} in the options) to this code path is probably all you need.

If you’re interested I can submit a PR 😉

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:7
  • Comments:5

github_iconTop GitHub Comments

2reactions
RedShift1commented, Nov 2, 2018

You can just do this in your own cache handler, no need to extend fast-memoize for this.

1reaction
ecerronicommented, Mar 22, 2021

@leefsmp

yarn add ecerroni/fast-memoize.js
// import
const memoize = require('fast-memoize')
const Keyv = require('keyv')
const Cache = require('map-expire/MapExpire')

// create a custom cache adapter
const storageAdapter = new Keyv({
  ttl: 1000, // in milliseconds, expires the entry after 1s
  store: new Cache([], {
    capacity: 1000,
    // duration: 1000, // in millisecond, default expiration time. Not need to set it if ttl is already set
  }),
})

// wrap the storage in a type of cache fast-memoize expects
const cache = {
  create: function create() {
    return storageAdapter
  },
}

(async => {
  // function
  let myFn = () => { ... }

  myFn = memoize(myFn, { cache })
  const memoizedFn = myFn

  await memoizedFn() // cached fn will be removed after 1 second
})()
Read more comments on GitHub >

github_iconTop Results From Across the Web

What Is TTL (And How Do You Choose the Right One)? - Kinsta
Hopefully, by now you will have a good idea of what TTL value you want for your site. Now we can see how...
Read more >
What is a good TTL for DNS? - NsLookup
The ability to survive a short outage caused by Internet DDoS attack, network issue, or DNS provider outage means that the service will...
Read more >
What is DNS TTL + Best Practices - Varonis
DNS TTLs are vital to websites that make constant changes and updates frequently. By having a lower TTL, you can ensure that you...
Read more >
TTL vs Manual Flash in the Studio - YouTube
TTL vs Manual Flash in the Studio: Take and Make Great ... Gavin finds the results are a real eye opener but will...
Read more >
TTL Vs. Manual Flash: Why Is One Always Better Than the ...
Someone will ask if they should use manual or TTL flash when they ... This is great when starting out with flash because...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found