Пример #1
0
def get_from_cache(cache_location, cache_params, name, key, GEVENT_MONKEY_PATCH=False):
    if GEVENT_MONKEY_PATCH:
        # Import Gevent and monkey patch
        try:
            from gevent import monkey
            monkey.patch_all()
        except:
            print "gevent monkey patch failed"
    # Import Django Cache (mozilla/django-memcached-pool)
    #from django.core.cache import cache, caches, get_cache
    #from django.core.cache import caches
    # Get Tile Cache
    cache = None
    item = None
    try:
        from umemcache import MemcachedError
        from memcachepool.cache import UMemcacheCache
        cache = UMemcacheCache(cache_location, cache_params)
        #cache = caches['tiles']
    except:
        cache = None

    if cache:
        try:
            item = cache.get(key)
	except socket_error, e:
            print e
            item = None        
        except MemcachedError, e:
            print e
            item = None
Пример #2
0
def commit_to_cache(cache_location, cache_params, key, obj, GEVENT_MONKEY_PATCH=False):
    if GEVENT_MONKEY_PATCH:
        # Import Gevent and monkey patch
        try:
            from gevent import monkey
            monkey.patch_all()
        except:
            print "gevent monkey patch failed"

    # Import Django Cache (mozilla/django-memcached-pool)
    #from django.core.cache import cache, caches, get_cache
    #from django.core.cache import caches
    # Get Tile Cache
    cache = None
    success = False
    try:
        from umemcache import MemcachedError
        from memcachepool.cache import UMemcacheCache
        cache = UMemcacheCache(cache_location, cache_params)
        #cache = caches['tiles']
    except:
        cache = None

    if cache:
        try:
            cache.set(key, obj)
            success = True
        except MemcachedError, e:
            print e
            success = False
Пример #3
0
    def test_incr_decr(self):
        # Testing incr and decr operations
        from memcachepool.cache import UMemcacheCache

        # creating the cache class
        cache = UMemcacheCache('127.0.0.1:11211', {})
        cache.set('a', 1)
        cache.incr('a', 1)
        self.assertEquals(cache.get('a'), 2)
        cache.decr('a', 1)
        self.assertEquals(cache.get('a'), 1)
Пример #4
0
    def test_types(self):
        # Testing if correct types are returned
        from memcachepool.cache import UMemcacheCache

        # creating the cache class
        cache = UMemcacheCache('127.0.0.1:11211', {})
        cache.set('a', int(1))
        self.assertEquals(cache.get('a'), 1)
        self.assertTrue(isinstance(cache.get('a'), int))

        cache.set('a', long(1))
        self.assertEquals(cache.get('a'), 1)
        self.assertTrue(isinstance(cache.get('a'), long))
    def test_incr_decr(self):
        # Testing incr and decr operations
        from memcachepool.cache import UMemcacheCache

        # creating the cache class
        cache = UMemcacheCache('127.0.0.1:11211', {})
        cache.set('a', 1)
        cache.incr('a', 1)
        self.assertEquals(cache.get('a'), 2)
        cache.decr('a', 1)
        self.assertEquals(cache.get('a'), 1)
Пример #6
0
def connect_to_cache(name):
    # Import Gevent and monkey patch
    from gevent import monkey
    monkey.patch_all()
    # Import Django Cache (mozilla/django-memcached-pool)
    #from django.core.cache import cache, caches, get_cache
    #from django.core.cache import caches
    # Get Tile Cache
    cache = None
    try:
        from memcachepool.cache import UMemcacheCache
        cache = UMemcacheCache(settings.CACHES[name]['LOCATION'], {})
        cache.get('')
    except:
        cache = None
    return cache
    def test_pool(self):
        from memcachepool.cache import UMemcacheCache

        # creating the cache class
        cache = UMemcacheCache('127.0.0.1:11211', {})

        # simple calls
        cache.set('a', '1')
        self.assertEqual(cache.get('a'), '1')

        # should support any type and deal with serialization
        # like python-memcached does
        cache.set('a', 1)
        self.assertEqual(cache.get('a'), 1)
        cache.delete('a')
        self.assertEqual(cache.get('a'), None)
    def test_types(self):
        # Testing if correct types are returned
        from memcachepool.cache import UMemcacheCache

        # creating the cache class
        cache = UMemcacheCache('127.0.0.1:11211', {})
        cache.set('a', int(1))
        self.assertEquals(cache.get('a'), 1)
        self.assertTrue(isinstance(cache.get('a'), int))

        cache.set('a', long(1))
        self.assertEquals(cache.get('a'), 1)
        self.assertTrue(isinstance(cache.get('a'), long))
    def test_loadbalance(self):
        from memcachepool.cache import UMemcacheCache

        # creating the cache class with two backends (one is off)
        params = {'SOCKET_TIMEOUT': 1, 'BLACKLIST_TIME': 1}
        cache = UMemcacheCache('127.0.0.1:11214;127.0.0.2:11213', params)

        # the load balancer should blacklist both IPs.
        # and return an error
        self.assertRaises(socket.error, cache.set, 'a', '1')
        self.assertTrue(len(cache._blacklist), 2)

        # wait for two seconds.
        time.sleep(1.1)

        # calling _pick_server should purge the blacklist
        cache._pick_server()
        self.assertEqual(len(cache._blacklist), 0)
Пример #10
0
    def test_loadbalance(self):
        from memcachepool.cache import UMemcacheCache

        # creating the cache class with two backends (one is off)
        params = {'SOCKET_TIMEOUT': 1, 'BLACKLIST_TIME': 1}
        cache = UMemcacheCache('127.0.0.1:11214;127.0.0.2:11213', params)

        # the load balancer should blacklist both IPs.
        # and return an error
        self.assertRaises(socket.error, cache.set, 'a', '1')
        self.assertTrue(len(cache._blacklist), 2)

        # wait for two seconds.
        time.sleep(1.1)

        # calling _pick_server should purge the blacklist
        cache._pick_server()
        self.assertEqual(len(cache._blacklist), 0)
Пример #11
0
    def test_many(self):
        # make sure all the 'many' APIs work
        from memcachepool.cache import UMemcacheCache

        # creating the cache class
        cache = UMemcacheCache('127.0.0.1:11211', {})

        cache.set_many({'a': 1, 'b': 2})

        res = cache.get_many(['a', 'b']).values()
        self.assertTrue(1 in res)
        self.assertTrue(2 in res)

        cache.delete_many(['a', 'b'])
        self.assertEqual(cache.get_many(['a', 'b']), {})
Пример #12
0
def get_from_cache(name, key):
    # Import Gevent and monkey patch
    from gevent import monkey
    monkey.patch_all()
    # Import Django Cache (mozilla/django-memcached-pool)
    #from django.core.cache import cache, caches, get_cache
    #from django.core.cache import caches
    # Get Tile Cache
    cache = None
    item = None
    try:
        from memcachepool.cache import UMemcacheCache
        cache = UMemcacheCache(settings.CACHES[name]['LOCATION'], settings.CACHES[name])
        #cache = caches['tiles']
    except:
        cache = None

    if cache:
        try:
            item = cache.get(key)
        except umemcache.MemcachedError, e:
            print e
            item = None
Пример #13
0
def check_cache_availability(cache_location, cache_params, GEVENT_MONKEY_PATCH=False):
    if GEVENT_MONKEY_PATCH:
        # Import Gevent and monkey patch
        try:
            from gevent import monkey
            monkey.patch_all()
        except:
            print "gevent monkey patch failed"

    available = False
    cache = None
    try:
        from umemcache import MemcachedError
        from memcachepool.cache import UMemcacheCache
        cache = UMemcacheCache(cache_location, cache_params)
        #cache = caches['tiles']
        #from django.core.cache import caches
        #tilecache = caches[cache]
        cache.get('')
        available = True
    except MemcachedError, e:
        print e
        available = False
Пример #14
0
def commit_to_cache(name, key, obj):
    # Import Gevent and monkey patch
    from gevent import monkey
    monkey.patch_all()
    # Import Django Cache (mozilla/django-memcached-pool)
    #from django.core.cache import cache, caches, get_cache
    #from django.core.cache import caches
    # Get Tile Cache
    cache = None
    success = False
    try:
        from memcachepool.cache import UMemcacheCache
        cache = UMemcacheCache(settings.CACHES[name]['LOCATION'], settings.CACHES[name]['OPTIONS'])
        #cache = caches['tiles']
    except:
        cache = None

    if cache:
        try:
            cache.set(key, obj)
            success = True
        except:
            success = False
    return success
    def test_many(self):
        # make sure all the 'many' APIs work
        from memcachepool.cache import UMemcacheCache

        # creating the cache class
        cache = UMemcacheCache('127.0.0.1:11211', {})

        cache.set_many({'a': 1, 'b': 2})

        res = cache.get_many(['a', 'b']).values()
        self.assertTrue(1 in res)
        self.assertTrue(2 in res)

        cache.delete_many(['a', 'b'])
        self.assertEqual(cache.get_many(['a', 'b']), {})
Пример #16
0
 def __init__(self,
              key,
              codec,
              host=None,
              port=None,
              client_type="pymemcache",
              which="all",
              which_index=0):
     super(GeoWatchStoreMemcached, self).__init__("memcached", key, codec,
                                                  which, which_index)
     self.client_type = client_type
     if host and port:
         if self.client_type == "umemcache":
             # try:
             if 1 == 1:
                 cache_params = {
                     'BACKEND': 'memcachepool.cache.UMemcacheCache',
                     'LOCATION': host + ":" + str(port),
                     'OPTIONS': {
                         'MAX_POOL_SIZE': 40,
                         'BLACKLIST_TIME': 60,
                         'SOCKET_TIMEOUT': 60,
                         'MAX_ITEM_SIZE': 1000 * 1000 * 1000
                     }
                 }
                 # from umemcache import MemcachedError
                 from memcachepool.cache import UMemcacheCache
                 self._client = UMemcacheCache(host + ":" + str(port),
                                               cache_params)
             # except:
             #    self._client = None
         elif self.client_type == "pymemcache":
             try:
                 from pymemcache.client.base import Client
                 self._client = Client(
                     (host, port), default_noreply=False
                 )  # Set default_noreply to not wait for response
             except:
                 self._client = None
     else:
         print "Could not create GeoWatch client for S3 Backend.  Missing parameters."
Пример #17
0
    def test_pool(self):
        from memcachepool.cache import UMemcacheCache

        # creating the cache class
        cache = UMemcacheCache('127.0.0.1:11211', {})

        # simple calls
        cache.set('a', '1')
        self.assertEqual(cache.get('a'), '1')

        # should support any type and deal with serialization
        # like python-memcached does
        cache.set('a', 1)
        self.assertEqual(cache.get('a'), 1)
        cache.delete('a')
        self.assertEqual(cache.get('a'), None)