memcached-mock
Advanced tools
Comparing version 0.0.1 to 0.0.2
131
index.js
@@ -45,2 +45,11 @@ /* | ||
/** | ||
* Return the equivalent ttl for a key | ||
*/ | ||
function keyttl(memcached, key) { | ||
return cache[key] && cache[key].expires ? | ||
((cache[key].expires - Date.now()) / 1000) : | ||
0; | ||
} | ||
/** | ||
* Return the normalized value for a cache entry | ||
@@ -58,3 +67,3 @@ */ | ||
var result = {cas: entry.cas}; | ||
var result = {cas: String(entry.cas)}; | ||
result[key] = entry.value; | ||
@@ -204,2 +213,122 @@ | ||
/** | ||
* Add a key, but only if it does not already exist | ||
*/ | ||
add: function(key, value, ttl, callback) { | ||
var exists = expire(this, key); | ||
if (!exists) | ||
setkey(this, key, value, ttl); | ||
invoke(callback, {self: this, | ||
type: 'add', | ||
args: arguments, | ||
names: ['key', 'value', 'lifetime', 'callback']}, | ||
(!exists ? undefined : notStored()), | ||
(!exists ? true : false)); | ||
}, | ||
/** | ||
* Update a key, but only if its CAS id matches the provided value | ||
*/ | ||
cas: function(key, value, cas, ttl, callback) { | ||
var entry = expire(this, key); | ||
var success = false; | ||
if (entry && String(entry.cas) === cas) { | ||
setkey(this, key, value, ttl); | ||
success = true; | ||
} | ||
invoke(callback, {self: this, | ||
type: 'cas', | ||
args: arguments, | ||
names: ['key', 'value', 'cas', 'lifetime', 'callback']}, | ||
undefined, success); | ||
}, | ||
/** | ||
* Append a string value to a key, but only if the key already exists | ||
*/ | ||
append: function(key, appendValue, callback) { | ||
expire(this, key); | ||
if (cache[key]) | ||
setkey(this, key, String(value(cache[key])) + appendValue, keyttl(this, key)); | ||
invoke(callback, {self: this, | ||
type: 'append', | ||
args: arguments, | ||
names: ['key', 'value', 'callback']}, | ||
(cache[key] ? undefined : notStored()), | ||
(cache[key] ? true : false)); | ||
}, | ||
/** | ||
* Prepend a string value to a key, but only if the key already exists | ||
*/ | ||
prepend: function(key, prependValue, callback) { | ||
expire(this, key); | ||
if (cache[key]) | ||
setkey(this, key, String(prependValue) + value(cache[key]), keyttl(this, key)); | ||
invoke(callback, {self: this, | ||
type: 'prepend', | ||
args: arguments, | ||
names: ['key', 'value', 'callback']}, | ||
(cache[key] ? undefined : notStored()), | ||
(cache[key] ? true : false)); | ||
}, | ||
/** | ||
* Increment a value by an amount, but only if the key already exists | ||
*/ | ||
incr: function(key, amount, callback) { | ||
expire(this, key); | ||
if (cache[key]) | ||
setkey(this, key, Number(value(cache[key])) + amount, keyttl(this, key)); | ||
invoke(callback, {self: this, | ||
type: 'incr', | ||
args: arguments, | ||
names: ['key', 'value', 'callback']}, | ||
undefined, | ||
(cache[key] ? value(cache[key]) : false)); | ||
}, | ||
/** | ||
* Decrement a value by an amount, but only if the key already exists | ||
*/ | ||
decr: function(key, amount, callback) { | ||
expire(this, key); | ||
if (cache[key]) | ||
setkey(this, key, Number(value(cache[key])) - amount, keyttl(this, key)); | ||
invoke(callback, {self: this, | ||
type: 'decr', | ||
args: arguments, | ||
names: ['key', 'value', 'callback']}, | ||
undefined, | ||
(cache[key] ? value(cache[key]) : false)); | ||
}, | ||
/** | ||
* Delete a key from the cache | ||
*/ | ||
del: function(key, callback) { | ||
var exists = expire(this, key); | ||
if (exists) | ||
delete cache[key]; | ||
invoke(callback, {self: this, | ||
type: 'delete', | ||
args: arguments, | ||
names: ['key', 'callback']}, | ||
undefined, (exists ? true : false)); | ||
}, | ||
/** | ||
* Flush the contents of the cache | ||
@@ -206,0 +335,0 @@ */ |
{ | ||
"name": "memcached-mock", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A mock implementation of memcached to use as a replacement in tests", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -7,2 +7,7 @@ # memcached-mock | ||
* add(key, value, ttl, callback) | ||
* append(key, value, callback) | ||
* cas(key, value, cas, ttl, callback) | ||
* decr(key, amount, callback) | ||
* del(key, callback) | ||
* flush(callback) | ||
@@ -12,2 +17,4 @@ * get(key, callback) | ||
* getMulti(keys, callback) | ||
* incr(key, amount, callback) | ||
* prepend(key, value, callback) | ||
* replace(key, value, ttl, callback) | ||
@@ -14,0 +21,0 @@ * set(key, value, ttl, callback) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11442
284
47