Skip to content

Wei-Zou/memcache-client-memoizer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

memcache-client-memoizer

A function memoizer using memcache-client.

travis npm

Install:

npm i memcache-client-memoizer

API

memoizer(options)

Arguments

  • options: object. Required. An object with the following keys:
    • client: memcache-client instance. Required. A memcache-client instance.
    • fn: Function. Required. The function to memoize, must return a Promise.
    • keyFn: (args to fn) => 'key-string'. Required. A function which returns a string cache-key for memcached. This function is called with the same arguments as fn, allowing you to create a dynamic cache-key, for example:
      const exampleKeyFn = ({ name, color }) => `${name}:${color}`
    • setOptions: object. Optional. memcached-client command options.

Note:

Rejected promises are not memoized - since that's probably not what you want :)

Example:

const MemcacheClient = require('memcache-client')
const memoizer = require('memcache-client-memoizer')

const client = new MemcacheClient({ server: 'localhost:11211' })
const fnToMemoize = ({ name, color }) => Promise.resolve({ name, color })

const memoizedFn = memoizer({
  client,
  fn: fnToMemoize,
  keyFn: ({ name, color }) => `${name}:${color}`
})

memoizedFn({name: 'Max', color: 'blue'})
  .then((result) => { ... })  // cache miss, fill cache, returns {name: 'Max', color: 'blue'}

// later on...
memoizedFn({name: 'Max', color: 'blue'})
  .then((result) => { ... })  // cache hit, returns {name: 'Max', color: 'blue'}

About

Memoizes promise-returning functions via memcache-client

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%