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.
memcached-mock
Advanced tools
A mock implementation of memcached to use as a replacement in tests.
This is an in-memory implementation. It implements the documented, public API of the memcached
module (and known aliases). The interface is the same, however, no network calls are made. Values are stored in memory and discarded when the process exits.
Install the module with npm:
npm install memcached-mock
Use memcached-mock
the same as if it were memcached
:
var Memcached = require('memcached-mock');
var memcached = new Memcached('localhost:11211');
memcached.set("hello", "world!", 60, function(err) {
if (!err)
memcached.get("hello", function(err, data) {
console.log(data); // prints: world!
});
});
If your code does not allow you to easily replace the instance of Memcached being used for testing, you may choose to use a tool like proxyquire. That would look something like the following.
lib/cache.js:
var Memcached = require('memcached');
// ...
test/unit/cache.js:
var proxyquire = require('proxyquire')
, memcachedMock = require('memcached-mock')
;
module.exports.testSomething = function(done) {
var cache = proxyquire('../../lib/cache', {memcached: memcachedMock});
// continue with test...
}
This module is intended to work as a drop-in replacement in simple use cases. It allows you to provide a known, deterministic state for your tests. It might also serve as a starting point for more complex interactions. Some simulations, however, may require extra work.
Methods for accessing server information (such as stats
or settings
), for example, are mostly stubs that always return the same data. If you'd like to return different data, you'll need to provide your own replacements for those. Similarly, the methods for getting and setting cache values are fully functional, but if you'd like to simulate something beyond a connection to a single cache cluster, at a minimum you'll need to set up your Memcached instances to use the appropriate cache object for their server connection settings. By default, server strings are not examined and all instances share the same cache store. See documentation on the cache()
method for more details.
The work of making any cache updates is done synchronously. So, for example, take this code:
var memcached = new Memcached("127.0.0.1:11211");
memcached.set("foo", "bar", 0, function() { /* ... */ });
memcached.get("foo", function(err, data) {
console.log("foo=%s", data); // prints: foo=bar
});
That will work because the cached has been updated by the time the call to the set
method returns. Note, however, that callbacks are always invoked asynchronously. This is essential for testing the behavior your code will see from an actual connection to memcached. That means that this code will not work:
var memcached = new Memcached("127.0.0.1:11211");
memcached.flush(function() {
memcached.set("bar", "foo", 0, function() { /* ... */ });
});
memcached.get("bar", function(err, data) {
console.log("bar=%s", data); // bar has not been set yet!
});
You may choose to exploit this fact in your tests. You can, for example, safely flush a cache in your setup or teardown code without waiting for the callback to be invoked.
serverLocations
: String, Array, or Object Simulated connectionsoptions
: Object Client optionsConstructs a new mock Memcached instance. This simulates the interface of Memcached, but most settings do not have an effect. No connections are established with a server, and the value of serverLocations
does not change the behavior of the mock. The only option that does have an effect is maxExpiration
, which alters how TTL values are interpreted.
key
: String Name of the keyvalue
: Mixed Value to associate with the keyttl
: Number Expiration time for the key in secondscallback
: Function On completion callbackAdd a new mapping if the key does not yet exist in the cache.
key
: String Name of the keyvalue
: Mixed The value to appendcallback
: Function On completion callbackAppend data to an existing key's value. The key must already exist in the cache.
This method is not part of the memcached
interface. It provides direct access to the underlying in-memory cache. Values in the cache object are also objects with the following properties:
This can be used to directly alter or verify the contents of the cache. For example, if in a unit test you wanted to verify that the key "session-1" contained the object {userId: 2}
, that verification code might look like:
var cache = memcached.cache();
assert.deepEqual(cache["session-1"].value, {userId: 2});
cacheObject
: Object The cache object to useThis method is not part of the memcached
interface. It allows individual mock instances of Memcached to be configured to use a specific cache object instead of the default global cache.
This allows you to write code like:
var memcachedA = new Memcached('192.168.0.1:11211');
var memcachedB = new Memcached('192.168.0.2:11211');
var memcachedC = new Memcached('192.168.0.2:11211');
var cache1 = {};
var cache2 = {};
memcachedA.cache(cache1); // set the cache object
memcachedB.cache(cache2);
memcachedC.cache(cache2);
This will set up memcachedA
to use cache1
, and memcachedB
and memcachedC
to use cache2
, mimicking the behavior their server settings would normally have.
server
: String Server to return the dump ofslabid
: Number The slab id to dumplimit
: Number The maximum number of results to returncallback
: Function On completion callbackProvides a list of the keys in the cache. This mock method does return a listing of the actual keys, modeled as a single slab. All arguments besides callback
are ignored.
key
: String Name of the keyvalue
: Mixed Value to associate with the keycas
: String The CAS id to comparettl
: Number Expiration time for the key in secondscallback
: Function On completion callbackUpdate a mapping, but only if the CAS id of key
matches cas
. The CAS id of a key changes every time the key is updated.
key
: String Name of the keyamount
: Number Amount to decrement the value bycallback
: Function On completion callbackDecrement a key's value by amount
. This is only successful if the key already exists.
key
: String Name of the keycallback
: Function On completion callbackRemove a key from the cache.
Alias of del()
.
Terminate the server connection. This method has no effect.
callback
: Function On completion callbackFlush all keys from the cache.
Alias of flush()
.
key
: String Name of the keycallback
: Function On completion callbackRetrieve the value associated with key
. If key
is an array, this will automatically perform a getMulti
call instead.
key
: String Name of the keycallback
: Function On completion callbackRetrieve the value associated with key
and its CAS id.
keys
: Array Array of key namescallback
: Function On completion callbackRetrieve the value associated with multiple keys in a single call.
key
: String Name of the keyamount
: Number Amount to increment the value bycallback
: Function On completion callbackIncrement a key's value by amount
. This is only successful if the key already exists.
callback
: Function On completion callbackObtain information about items stored in the server. This returns correct information about the number of items contained in the cache. All other information is stub data.
key
: String Name of the keyvalue
: Mixed The value to prependcallback
: Function On completion callbackPrepend data to an existing key's value. The key must already exist in the cache.
key
: String Name of the keyvalue
: Mixed New value to associate with the keyttl
: Number Expiration time for the key in secondscallback
: Function On completion callbackReplace the existing value of key
with value
. This is only successful if key
already exists in the cache.
key
: String Name of the keyvalue
: Mixed Value to associate with the keyttl
: Number Expiration time for the key in secondscallback
: Function On completion callbackSet the value associated with key
in the cache. This works whether the key already exists in the cache or not.
callback
: Function On completion callbackRetrieve server settings. This returns stub data for each server given in the constructor.
callback
: Function On completion callbackRetrieve server slab information. This returns stub data showing one slab for each server given in the constructor.
callback
: Function On completion callbackRetrieve server stats. This returns stub data for each server given in the constructor.
Alias of items()
.
Alias of settings()
.
Alias of slabs()
.
callback
: Function On completion callbackRetrieve server version. This returns stub data for each server given in the constructor. The current version of the module reports itself as version 1.4.20.
FAQs
A mock implementation of memcached to use as a replacement in tests
The npm package memcached-mock receives a total of 3,397 weekly downloads. As such, memcached-mock popularity was classified as popular.
We found that memcached-mock 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.