Over a million developers have joined DZone.

Fake Memcache Server

DZone's Guide to

Fake Memcache Server

Free Resource
A small fake memcache, mimics the 'import memcache' module Useful for testing without a real memcache server to connect to, nowhere near complete (missing methods like flush_all()) for example, although if someone was to take the time...

class Memcache(object):
    >>> mc = Memcache()
    >>> mc.set('joe', 4)
    >>> mc.get('joe')

    cache = {}

    def set(self, key, value):
        assert(isinstance(key, basestring))
        self.cache[key] = value

    def set_multi(self, keys_values):
        for key in keys_values:
            assert(isinstance(key, basestring))

    def get(self, key):
        assert(isinstance(key, basestring))
        return self.cache.get(key, None)

    def get_multi(self, key_list, key_prefix=''):
        for key in key_list:
            assert(isinstance(key, basestring))
        result = {}
        for key in key_list:
            ckey = key_prefix+key
            if ckey in self.cache:
                result[key] = self.cache[ckey]
        return result

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}