Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
The ultimate Javascript cache module.
Unobtrusive and failsafe caching of anything.
Caches functions instead of data.
npm install hamster
var hamster = require('hamster');
// Cache a function
var getData = hamster(aFunctionThatTakesLotsOfTime);
// Call the function like you normally would
getData('yeah', resultHandler);
When you cache a function using hamster(fn)
, a function is returned. You
can use this function in exactly the same way as the original. No other part of
your code should ever have to care about that the function is cached.
Also the cached function itself should not and need not be modified with caching in mind. The function should do what it does, callers should do what they do, while Hamster automagically takes care of the cache.
Note
Do not use Hamster with functions that are based on side-effects. That is, if it modifies an argument object or doesn't return the result, or if the result varies depending on non-argument variables (eg global vars or properties on its object).
A typical example of this would be trying to cache an express/connect middleware directly. That won't work since a middleware just modifies an object, that also is unique to every request. Instead, rewrite the function to only require what arguments it needs. For example:
// Do not:
app.use(hamster(getUserData));
// Do:
getUserData = hamster(function(id){...});
app.use(function (req, res, next) {
getUserData(req.params.id, function (err, result) {
req.user = result;
next();
});
});
npm install hamster
See releases for browserified source files.
hamster
A global instance of Hamster (see hamster() below).
hamster = new hamster.Hamster([options])
Creates a new instance of hamster with some customizable options (see options below).
cache = hamster(fn, [options])
An instance of Hamster, managing cached functions. Call it with a function to create a cache for that function. Returns a function that should replace the original function.
cache = hamster.sync()
A shorthand method for calling hamster(fn, { async: false })
.
cache = hamster.async()
A shorthand method for calling hamster(fn, { async: true })
.
cache()
Just use it as you would use the original function.
cache.original()
A reference to the original function, if you need to circumvent the cache.
cache.clear([key])
Clear all cached results (no key) or the result for a specific set of arguments.
{
async: true, // Set to false to enable caching synchronous functions
ttl: 10000, // How long to wait before the cached result is cleared, in milliseconds
maxSize: 50, // Max number of cached results, the least recently used will be dropped when exeeding this limit
keys: [] // Possibility to create a new cache only for certain arguments/properties
}
A note on options.keys. Let's say you supply the entire req object but for caching reasons the funciton is only interested in req.params.id. Then the following would consider requests to be the same if anything but req.params.id changes.
// 0 for the first argument
cache = hamster(fn, { keys: ['0.params.id'] });
// Will only request a post from the server the first time
var getPost = hamster(function (id, callback) {
$.ajax({
url: '/posts/' + id,
success: callback
});
}, {
ttl: 0
});
$('.toc a').on('click', function(e) {
e.preventDefault();
getPost($(this).attr('data-id'), function (data) {
renderPost(data);
});
});
postSchema.statics.getFull = hamster(function (id, callback) {
this.findById(id).populate('author').exec(callback);
});
// ... used in routing like normal:
app.get('/:id', function (req, res) {
postModel.getFull(req.params.id, function (post) {
res.render('post', post);
});
});
See the test folder for more examples.
Want to store the caches in redis or localStorage instead of in-memory? Go ahead and implement .store, .hasCache, .getCache, .addTimeout, and possibly any other methods on a cache instance. I will probably implement a neater api for this later.
Create an issue first so I know what you're doing. Pull request to the develop branch. Test everything.
AKA implemented when/if needed.
MIT
Copyright (C) 2013 Andreas Hultgren
FAQs
The ultimate Javascript cache module. Cache the result of any function.
We found that hamster demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.