Example #1
0
class CacheClass(MemcCacheClass):
    def __init__(self, server, params):
        MemcCacheClass.__init__(self, server, params)
        self._cache = MemClient(server.split(';'), pickleProtocol=getattr(settings, 'CACHE_PICKLE_PROTOCOL', 0))

    def add(self, key, value, timeout=0):
        """ dodano debugowanie """

        ### value to utf8
        if isinstance(value, unicode):
            value = value.encode('utf-8')

        ### debug
        srv,k = _parsekey(key)
        if _ispiedkey(k):
            # wylicz size
            size = _calcsize(value)
            log.debug('ADD %s %s %s',k, srv, size)

        ### add
        return self._cache.add(gm_smart_str(key),
                               value,
                               timeout or self.default_timeout,
                               getattr(settings, 'CACHE_MIN_COMPRESSION_LEN', 0)
                               )

    def get(self, key, default=None):
        """
        Dodane logowanie wybranych kluczy (missing)

        info: brak klucza takiego i takiego
        debug: kazde odpytanie do cache
        """
        ### get value
        val = self._cache.get(gm_smart_str(key))

        ### debug
        srv,k = _parsekey(key)
        if _ispiedkey(k):
            # wylicz size
            size = _calcsize(val)
            # log
            if val is None:
                log.info("%s %s %s %s", 'MISS', k, srv, size)
            else:
                log.debug("%s %s %s %s", 'HIT', k, srv, size)

        # return value
        if val is None:
            return default
        else:
            if isinstance(val, basestring):
                return smart_unicode(val)
            else:
                return val


    def set(self, key, value, timeout=0, zip=True):
        """
        Dodany ficzer z min_compression_len, aby mozliwa byla kompresja duzych kluczy.
        """
        ### value to utf8
        if isinstance(value, unicode):
            value = value.encode('utf-8')

        srv,k = _parsekey(key)
        if _ispiedkey(k):
            # wylicz size
            size = _calcsize(value)
            # log
            log.debug("%s %s %s %s", 'SET', k, srv, size)

        ### set
        return self._cache.set(gm_smart_str(key),
                               value,
                               timeout or self.default_timeout,
                               getattr(settings, 'CACHE_MIN_COMPRESSION_LEN', 0) if zip else 0)

    def delete(self, key):
        """ normlane kasowanie + DEBUG + zwracanie wartosci """
        ### debug
        srv,k = _parsekey(key)
        if _ispiedkey(k):
            log.debug('DELETE %s %s',k, srv)

        ### delete
        return self._cache.delete(gm_smart_str(key))

    def get_many(self, keys):
        """ get many z obslugi sledzenia kluczy """
        spiedkeys = [k for k in  keys if _ispiedkey(k)]
        if spiedkeys:
            log.debug('GETMULTI %s', ','.join(spiedkeys) )
        return self._cache.get_multi(map(gm_smart_str, keys))
Example #2
0
from hash_ring import MemcacheRing
mc=MemcacheRing(['127.0.0.1:11212'])
mc.set('hello','world')
print(mc.get('hello'))
Example #3
0
class CacheClass(MemcCacheClass):
    def __init__(self, server, params):
        MemcCacheClass.__init__(self, server, params)
        self._cache = MemClient(server.split(';'),
                                pickleProtocol=getattr(
                                    settings, 'CACHE_PICKLE_PROTOCOL', 0))

    def add(self, key, value, timeout=0):
        """ dodano debugowanie """

        ### value to utf8
        if isinstance(value, unicode):
            value = value.encode('utf-8')

        ### debug
        srv, k = _parsekey(key)
        if _ispiedkey(k):
            # wylicz size
            size = _calcsize(value)
            log.debug('ADD %s %s %s', k, srv, size)

        ### add
        return self._cache.add(
            gm_smart_str(key), value, timeout or self.default_timeout,
            getattr(settings, 'CACHE_MIN_COMPRESSION_LEN', 0))

    def get(self, key, default=None):
        """
        Dodane logowanie wybranych kluczy (missing)

        info: brak klucza takiego i takiego
        debug: kazde odpytanie do cache
        """
        ### get value
        val = self._cache.get(gm_smart_str(key))

        ### debug
        srv, k = _parsekey(key)
        if _ispiedkey(k):
            # wylicz size
            size = _calcsize(val)
            # log
            if val is None:
                log.info("%s %s %s %s", 'MISS', k, srv, size)
            else:
                log.debug("%s %s %s %s", 'HIT', k, srv, size)

        # return value
        if val is None:
            return default
        else:
            if isinstance(val, basestring):
                return smart_unicode(val)
            else:
                return val

    def set(self, key, value, timeout=0, zip=True):
        """
        Dodany ficzer z min_compression_len, aby mozliwa byla kompresja duzych kluczy.
        """
        ### value to utf8
        if isinstance(value, unicode):
            value = value.encode('utf-8')

        srv, k = _parsekey(key)
        if _ispiedkey(k):
            # wylicz size
            size = _calcsize(value)
            # log
            log.debug("%s %s %s %s", 'SET', k, srv, size)

        ### set
        return self._cache.set(
            gm_smart_str(key), value, timeout or self.default_timeout,
            getattr(settings, 'CACHE_MIN_COMPRESSION_LEN', 0) if zip else 0)

    def delete(self, key):
        """ normlane kasowanie + DEBUG + zwracanie wartosci """
        ### debug
        srv, k = _parsekey(key)
        if _ispiedkey(k):
            log.debug('DELETE %s %s', k, srv)

        ### delete
        return self._cache.delete(gm_smart_str(key))

    def get_many(self, keys):
        """ get many z obslugi sledzenia kluczy """
        spiedkeys = [k for k in keys if _ispiedkey(k)]
        if spiedkeys:
            log.debug('GETMULTI %s', ','.join(spiedkeys))
        return self._cache.get_multi(map(gm_smart_str, keys))