toshihiko-memcached v1.0.1
Toshihiko Memcached (Cache Layout)
The memcached support for Toshihiko as an addon.
Installation
$ npm install toshihiko-memcached --saveHow to Use
When you define a Toshihiko, you could pass the object into cache option:
var T = require("toshihiko");
var toshihiko = new T.Toshihiko("database", "username", "", {
cache: {
name: "memcached",
servers: [ "localhost:11211", ... ],
options: { prefix: "_" }
}
});
namemust bememcachedand then Toshihiko will search for the packagetoshihiko-memcached.
serversmay be a string or an array stands for the server addresses.
optionsis the options for memcached, you can refer to https://github.com/3rd-Eden/node-memcached#options.
Otherwise, you may create this object by yourself and pass the created object into cached:
var Memcached = require("toshihiko-memcached");
var object = Memcached.create(SERVRES, OPTIONS);
var toshihiko = new T.Toshihiko(DATABASE, USERNAME, PASSWORD, {
cache: object
});or
var Memcached = require("toshihiko-memcached");
var object = new Memcached(SERVRES, OPTIONS);
var toshihiko = new T.Toshihiko(DATABASE, USERNAME, PASSWORD, {
cache: object
});And then you may enjoy the cache layer of Toshihiko!
Customize Key Generate Function
A new feature for memcached is that you can custom your memcached key generate function now!
You may pass the function at the very beginning:
new Memcached(servers, { custormizeKey: function(db, table, keys) { return ...; } });Another way is you can pass throw the function below:
memcached.setCustomizeKeyFunc(function(db, table, keys) { return ...; });You should pay attention to db, table and keys which stand for database name, table name, primary keys with their value.
keysmaybe a single value (whentypeof keys !== "object"); it maybe an object contains key-value pairskey name -> value.Eg.
{ "userId": 12, "boardId": 12 }
So here's an example customize function:
function(db, table, keys) {
var base = this.prefix + db + "_" + table;
if(typeof keys !== "object") return base + ":" + keys;
for(var key in keys) {
base += ":";
base += key;
base += keys[key];
}
return base;
}Contribution
You're welcome to make pull requests or issues!
「雖然我覺得不怎麼可能有人會關注我」