Skip to content

Instantly share code, notes, and snippets.

@minskmaz
Created September 26, 2015 23:49
Show Gist options
  • Save minskmaz/41a33b82dbc49aaa083e to your computer and use it in GitHub Desktop.
Save minskmaz/41a33b82dbc49aaa083e to your computer and use it in GitHub Desktop.
pooled memcache connection with zope utility for dependency injection
import Queue, memcache
from contextlib import contextmanager
from zope.interface import implements, Interface
from zope.component import getGlobalSiteManager
gsm = getGlobalSiteManager()
memcache.Client = type('Client', (object,), dict(memcache.Client.__dict__))
# Client.__init__ references local, so need to replace that, too
class Local(object): pass
memcache.local = Local
class PoolClient(object):
'''Pool of memcache clients that has the same API as memcache.Client'''
def __init__(self, pool_size, pool_timeout, *args, **kwargs):
self.pool_timeout = pool_timeout
self.queue = Queue.Queue()
for _i in range(pool_size):
self.queue.put(memcache.Client(*args, **kwargs))
#print "pool_size:", pool_size, ", Queue_size:", self.queue.qsize()
@contextmanager
def reserve( self ):
''' Reference: http://sendapatch.se/projects/pylibmc/pooling.html#pylibmc.ClientPool'''
client = self.queue.get(timeout=self.pool_timeout)
try:
yield client
finally:
self.queue.put( client )
#print "Queue_size:", self.queue.qsize()
class IMemcachedConnection(Interface):
"""zope without the zodb rules"""
pass
class MemcachePool(object):
implements(IMemcachedConnection)
"""
http://stackoverflow.com/questions/19665235/memcache-client-with-connection-pool-for-python#22520633
"""
def __init__(self):
self.mc_client_pool = PoolClient( 20, 0, ['127.0.0.1:11211'] )
def set(self, key, val):
with self.mc_client_pool.reserve() as mc_client:
mc_client.set(key, val)
def get(self, key):
with self.mc_client_pool.reserve() as mc_client:
mc_client.get(key)
def __call__(self):
return self.mc_client_pool
memcached = MemcachePool()
gsm.registerUtility(memcached, IMemcachedConnection, 'mcpool')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment