akidee / memo.js

Sophisticated function memoization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

                                       _
   ____ ___  ___  ____ ___  ____      (_)____
  / __ `__ \/ _ \/ __ `__ \/ __ \    / / ___/
 / / / / / /  __/ / / / / / /_/ /   / (__  )
/_/ /_/ /_/\___/_/ /_/ /_/\____(_)_/ /____/  v0.2.2
                                /___/




memo.js: Sophisticated function memoization (node.js)

  - Simple API
  - Supports async and sync functions
  - Deals with the "thundering herd problem": Guarantees max 1 execution for a unique function call. When several identic calls are made at once, only the first one is executed, while the other ones are pushed to an event emitter.
  - Backend interface to plug in the storage engine of your choice




Installation

npm install memo




How to use

var memo = require('memo')
var asyncMemo = memo(
	myFunction,
	options
)

myFunction is any sync or async function.
options is an object that can have the following keys:

  - 'context': myFunction's context (this), default: null
  - 'hashFunc': Returns the cache key, must be a string, default: JSON.stringify
  - 'cache': module id of the backend storage. Default: './caches/simple'
  - 'isSync': Must be true, if myFunction is sync, default: false - Anyway, memo() always returns an async function

Further options are passed to the cache constructor. The following options can be defined for the only backend storage, that is currently available: an in-memory cache with optional cache invalidation ('./caches/simple'):

  - 'maxLength': Maximum number of cache objects. When the cache is full, the least frequently accessed objects are removed. Default: 0 (no limit)
  - 'maxAge': Maximum age (in seconds) of a cached object. After this time, the object is removed from the cache

asyncMemo is an async function that references the wrapped myFunction, and it can be called in the same way as myFunction is called, besides the fact that it is always async. (This is necessary, because the backend storage includes async calls.)

It is highly recommended to use node.js v0.6.7+ to prevent hash collision attacks!




Tested in

  node.js 0.4-latest




Contribute!

I am glad to receive your contributions to support more backends, for example Redis and other scalable key/value stores. See /src/caches to see the structure of a cache. Next to all tests, run /lib/test/caches/simple_stress.js , too.

The code is in /src (CoffeeScript). If you want to contribute, do:

npm install memo --dev

This installs CoffeeScript.
While or at least after writing code fire

./watch

in your bash to make CoffeeScript compile the latest version into the lib directory

About

Sophisticated function memoization

License:MIT License


Languages

Language:JavaScript 56.7%Language:CoffeeScript 42.4%Language:Shell 0.9%