0.1.1 • Published 10 years ago

tec v0.1.1

Weekly downloads
6
License
-
Repository
github
Last release
10 years ago

Build Status NPM version #tec: Time Eviction Cache#

tec module is a time eviction cache with an integration via proxy injection that makes its work transparent. The usage is very simple, inject the cache into a target function and let tec keep track of the returned values from the target function for you.

No more checking for cache hits and misses that pollutes your business logic. tec handles it under the hood and retrieves the information again when internal values have been evicted. It works for both synchronous and asynchronous target functions.

Example

var tec = require('tec');
var mymodule = {
	sum:function(x,y){
		console.log("sum(" + x + "," + y + ")");
		return x+y;
	}
}
tec.cache(mymodule,"sum",1000);//1 second eviction

mymodule.sum(5,5);
mymodule.sum(5,5);

The first time that the sum function is called tec routes the call to the target function, holds the value returned in memory so the next calls are served from within tec itself. After the timeout the value is evicted, so a future call will be rerouted again to the source. This feature can be very handy for asynchronous fetches to a database or remote system to lower down the number of requests.

Following this line you can find the an example with an asynchronous target function.

var tec = require('tec');
var mymodule = {
	sum:function(x,y){
		setTimeout(function(){
			cb(null, x+y);
		},5000);
	}
}
tec.cache(mymodule,"sum",1000);//1 second eviction

mymodule.sum(5,5,function(err,val){
	//val served from source
	mymodule.sum(5,5,function(err,val){
        //val served from internal cache
	});	
});

To run the tests:

$ grunt nodeunit

License

Copyright (c) 2014 Enric Cecilla Licensed under the MIT license.