FeaturesThis module is highly inspired by cache-service-redis
cache-service
and superagent-cache
.mset()
allows you to set expirations on a per key, per function call, and/or per cache-service-memcached
instance basis.Require and instantiate
var superagent = require('superagent');
var CacheServiceMemcached = require('cache-service-memcached');
var memcachedCache = new CacheServiceMemcached('127.0.0.1:11211');
var defaults = {cacheWhenEmpty: false, expiration: 900};
Â
require('superagent-cache')(superagent, memcachedCache, defaults);
Cache!
Cache Module Configuration Optionsvar lifetime = 300;Â
memcachedCache.set('key', 'value', lifetime);
cache-service-memcached
's constructor takes an optional config object with any number of the following properties:
The expiration to include when executing cache set commands. Can be overridden via .set()
's optional expiraiton param.
The object that is passed to memcached as 2nd parameter. Memcached client options
backgroundRefreshIntervalvar options = {
  memcachedOptions: {
    maxKeySize: 50,
    maxValue: 20000000
  }
}
How frequently should all background refresh-enabled keys be scanned to determine whether they should be refreshed. For a more thorough explanation on background refresh
, see the Using Background Refresh section.
The maximum ttl a scanned background refresh-enabled key can have without triggering a refresh. This number should always be greater than backgroundRefreshInterval
.
Whether to throw an exception if backgroundRefreshInterval
is greater than backgroundRefreshMinTtl
. Setting this property to false is highly discouraged.
Although this is a memcached wrapper, its API differs in some small cases from memcached's own API both because the memcached API is sometimes dumb and because all cache-service
-compatible cache modules match cache-service
's API.
Retrieve a value by a given key.
Retrieve the values belonging to a series of keys. If a key is not found, it will not be in response
.
See the Using Background Refresh section for more about the
refresh
andcallback
params.
Set a value by a given key.
Set multiple values to multiple keys
This function exposes a heirarchy of expiration values as follows:
expiration
property of a key that also contains a cacheValue
property will override all other expirations. (This means that, if you are caching an object, the string 'cacheValue' is a reserved property name within that object.)cacheValue
and expiration
as properties is not present, the expiration
provided to the .mset()
argument list will be used.defaultExpiration
will be applied.Delete a key or an array of keys and their associated values.
Flush all keys and values.
This is the underlying Memcached client instance. If needed, you can access memcachedClient
functions that haven't abstracted in the cache plugin.
With a typical cache setup, you're left to find the perfect compromise between having a long expiration so that users don't have to suffer through the worst case load time, and a short expiration so data doesn't get stale. cache-service-memcached
eliminates the need to worry about users suffering through the longest wait time by automatically refreshing keys for you. Here's how it works:
How do I turn it on?
cache-service-memcached
employs an intelligent background refresh algorithm that makes it so only one dyno executes a background refresh for any given key. You should feel confident that you will not encounter multiple dynos refreshing a single key.
By default, background refresh is off. It will turn itself on the first time you pass a refresh
param to .set()
.
There are three options you can manipulate. See the API section for more information about them.
backgroundRefreshInterval
backgroundRefreshMinTtl
backgroundRefreshIntervalCheck
Background refresh is exposed via the .set()
command as follows:
cacheModule.set('key', 'value', 300, refresh, cb);
If you want to pass refresh
, you must also pass cb
because if only four params are passed, cache-service-memcached
will assume the fourth param is cb
.
The refresh
param MUST be a function that accepts key
and a callback function that accepts err
and response
as follows:
var refresh = function(key, cb){
  var response = goGetData();
  cb(null, response);
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4