Source

storage-ng / storage / memcached.py

# Copyright: 2011 MoinMoin:ThomasWaldmann
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
MoinMoin - memcached "storage" (rather a CACHE, non-persistent, in RAM)

Note: does not support iteration.
"""


from __future__ import absolute_import, division

from StringIO import StringIO

import memcache

from storage import MutableStorageBase, BytesMutableStorageBase, FileMutableStorageBase

class _Storage(MutableStorageBase):
    """
    A simple dict-based in-memory storage. No persistence!
    """
    def __init__(self, servers=['localhost:11211'], debug=0):
        self.servers = servers
        self.debug = debug

    def create(self):
        pass

    def destroy(self):
        pass

    def open(self):
        self._mc = memcache.Client(self.servers, debug=self.debug)

    def close(self):
        self._mc.disconnect_all()

    def __iter__(self):
        # memcached does not support this
        return iter([])

    def __delitem__(self, key):
        self._mc.delete(key)


class BytesStorage(_Storage, BytesMutableStorageBase):
    def __getitem__(self, key):
        value = self._mc.get(key)
        if value is None:
            raise KeyError(key)
        return value

    def __setitem__(self, key, value):
        self._mc.set(key, value)


class FileStorage(_Storage, FileMutableStorageBase):
    def __getitem__(self, key):
        value = self._mc.get(key)
        if value is None:
            raise KeyError(key)
        return StringIO(value)

    def __setitem__(self, key, stream):
        self._mc.set(key, stream.read())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.