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.

Callback for cache access

See original GitHub issue

I’m trying to add Prometheus metrics around my cache use. The metrics I’d like are:

  • count of accesses
  • count of hits
  • count of evictions

Hit rate can then be hits / accesses, and eviction rate evictions / accesses.

This is what I have now;

// ... init metrics counters

return memoizee(myFunction, {
  max: 250,
  dispose() {
    evictionCounter.inc()
  },
})

Having another callback, in addition to dispose which gets called after every access and has (hit: boolean) as the parameter would be really useful.

For example:

function onAccess(hit: boolean) {
  accessCounter.inc()
  if (hit) {
    hitCounter.inc()
  }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ThomWrightcommented, Mar 16, 2021

That solution seems to work, thanks. For reference, here is what I did:

const memoizeeExtensions = require("memoizee/lib/registered-extensions")
memoizeeExtensions.metrics = function(
  _: unknown,
  conf: EventEmitter,
  options: memoizee.Options<Parse>,
) {
  const postfix =
    (options.async && memoizeeExtensions.async) ||
    (options.promise && memoizeeExtensions.promise)
      ? "async"
      : ""

  conf.on("set" + postfix, (id: string) => {
    queryCounter.inc()
  })
  conf.on("get" + postfix, (id: string) => {
    queryCounter.inc()
    hitsCounter.inc()
  })
  conf.on("delete" + postfix, (id: string) => {
    evictionCounter.inc()
  })
}

return memoizee(parse, {
  primitive: true,
  max: 250,
  metrics: true,
})

To get the types to work I also did this:

// typings/memoizee/index.d.ts
import "memoizee"

declare module "memoizee" {
  export interface Options<F extends (...args: any[]) => any> {
    // To turn on our own extension
    metrics?: boolean
  }
}
0reactions
ThomWrightcommented, Mar 15, 2021

Great, thank you. I’ll have a look at the extensions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Caching Access Checks - Win32 apps | Microsoft Learn
When an application performs an access check by calling the AuthzAccessCheck function, the results of that access check can be cached.
Read more >
Background Callback Caching | Dash for Python Documentation
Background callbacks support caching callback function results, which saves and reuses the results of the function if it is called multiple times with...
Read more >
How to avoid caching on access callbacks? - Drupal Answers
I am using Views Access Callback module to set my custom callback access check. I have written a custom module and implemented the...
Read more >
CBFS Cache | Cache Library - Callback Technologies
CBFS Cache is a completely self-contained file caching solution. Just insert it between your application's local and remote data access logic, and it...
Read more >
Cache a value fetched from a listener callback - Stack Overflow
When doStuff() is called for the first time, cacheStorage is empty, so getStatePromise() will fetch the State object, populate the cache, and ...
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