Example #1
0
class MemcachedCacheStore(AbstractCacheStore):

    servers = ("127.0.0.1:11211")

    def __init__(self, servers=None, debug=False):
        if servers is None:
            servers = self.servers
        from memcache import Client as MemcachedClient
        self._client = MemcachedClient(servers, debug)

    def set(self, key, val, time=0):
        self._client.set(key, val, time)

    def add(self, key, val, time=0):
        res = self._client.add(key, val, time)
        if not res:
            raise Error("a value for key %r is already in the cache" % key)
        self._data[key] = (val, time)

    def replace(self, key, val, time=0):
        res = self._client.replace(key, val, time)
        if not res:
            raise Error("a value for key %r is already in the cache" % key)
        self._data[key] = (val, time)

    def delete(self, key):
        res = self._client.delete(key, time=0)
        if not res:
            raise KeyError(key)

    def get(self, key):
        val = self._client.get(key)
        if val is None:
            raise KeyError(key)
        return val

    def clear(self):
        self._client.flush_all()
Example #2
0
class MemcachedCacheStore(AbstractCacheStore):
    servers = ('127.0.0.1:11211')
    def __init__(self, servers=None, debug=False):
        if servers is None:
            servers = self.servers
        from memcache import Client as MemcachedClient
        self._client = MemcachedClient(servers, debug)

    def set(self, key, val, time=0):
        self._client.set(key, val, time)

    def add(self, key, val, time=0):
        res = self._client.add(key, val, time)        
        if not res:
            raise Error('a value for key %r is already in the cache'%key)
        self._data[key] = (val, time)

    def replace(self, key, val, time=0):
        res = self._client.replace(key, val, time)        
        if not res:
            raise Error('a value for key %r is already in the cache'%key)
        self._data[key] = (val, time)

    def delete(self, key):
        res = self._client.delete(key, time=0)        
        if not res:
            raise KeyError(key)
        
    def get(self, key):
        val = self._client.get(key)
        if val is None:
            raise KeyError(key)
        else:
            return val

    def clear(self):
        self._client.flush_all()        
Example #3
0
class MemcacheFeatureStorage(FeatureStorage):
    PREFIX = 'georest_buckets'

    support_version = False

    def __init__(self, hosts):
        """ Feature storage implemented in Memcache

        :param list hosts: list of hosts
            1. Strings of the form C{"host:port"}
            2. Tuples of the form C{("host:port", weight)}
        :rtype :class:`MemcacheFeatureStorage`
        """
        self._client = Client(servers=hosts)

    def create_bucket(self, name, overwrite=False, **kwargs):
        bucket_name = self._make_bucket_name(name)

        timestamp = time.time()
        try:
            add_ok = self._client.add(key=bucket_name, val=timestamp)
        except Exception as e:
            raise StorageInternalError(message='add error', e=e)

        if not add_ok:
            if overwrite:
                try:
                    rep_ok = self._client.replace(
                        key=bucket_name, val=timestamp)
                except Exception as e:
                    raise StorageInternalError('replace error', e=e)

                if not rep_ok:
                    raise StorageInternalError(message='failed to replace')
            else:
                raise DuplicatedBucket(name)

        return MemcacheFeatureBucket(name, self._client, str(timestamp))

    def get_bucket(self, name):
        bucket_name = self._make_bucket_name(name)

        try:
            timestamp = self._client.get(bucket_name)
        except Exception as e:
            raise StorageInternalError(message='get error', e=e)

        if not timestamp:
            raise BucketNotFound(name)

        return MemcacheFeatureBucket(name, self._client, str(timestamp))

    def delete_bucket(self, name):
        bucket_name = self._make_bucket_name(name)

        try:
            delete_ok = self._client.delete(bucket_name)
        except Exception as e:
            raise StorageInternalError(message='delete error', e=e)

        if not delete_ok:
            raise BucketNotFound(name)

        return True

    def has_bucket(self, name):
        bucket_name = self._make_bucket_name(name)

        try:
            get_ok = self._client.get(bucket_name)
        except Exception as e:
            raise StorageInternalError(message='get error', e=e)

        return get_ok is not None

    def close(self):
        pass

    def _make_bucket_name(self, name):
        if isinstance(name, unicode):
            name = name.encode('utf-8')
        return '.'.join((self.PREFIX, name))
Example #4
0
from memcache import Client
"""
安装memcache时,遇到的问题,提示libevent
解决 yum install libevent
     yum install libevent-devel

"""

# 这里是个list,可以吧memcache集群这么搞
MC_SERVERS = ['192.168.52.3:11211', '192.168.52.3:11212']

CONN = Client(MC_SERVERS)

status = CONN.set('key1', 'val2', 0)
print(status)

status = CONN.delete('key')
print(status)

status = CONN.add('key', 'val', 20)
print(status)

status = CONN.replace('key', 'val1', 0)
print(status)

status = CONN.append('key', ',val2')
print(status)

data = CONN.get('key')
print(data)