Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: switch to local custom LRUCache #3388

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

zonemeen
Copy link
Collaborator

No description provided.

@zhangyx1998
Copy link
Contributor

zhangyx1998 commented Jan 4, 2024

Will it help if LRUCache works like a function wrapper? Just like python's @cache decorator provided by functools.

e.g.

export function cached<K extends any[], V>(
  fn: (...args: K) => V,
  {
    serialize = JSON.stringify,
    max
  }: {
    serialize: (args: K) => string
    max?: number
  }
): (...args: K) => V {
  const cache = new LRUCache<string, V>(max)
  return Object.assign(
    (...args: K) => {
      const k = serialize(args)
      if (!cache.has(k)) cache.set(k, fn(...args))
      return cache.get(k)!
    },
    { cache } // fn.cache?.clear()
  )
}

It should provide an non-intrusive way of implementing cache:

import { cached } from '...'
import { createFn } from '...'

fn = config.cache ? cached(createFn(...)) : createFn(...)

fn.cache?.clear()

@github-actions github-actions bot added the stale label Feb 15, 2024
@github-actions github-actions bot removed the stale label Apr 28, 2024
@github-actions github-actions bot added the stale label Jun 9, 2024
@github-actions github-actions bot removed the stale label Jul 17, 2024
@github-actions github-actions bot added the stale label Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants