4 ms·
You can use a closure to pass in the configuration. def configure_memcache(client_ip, port): client = pymemcache.client.Client((client_ip, port))
by vngzs 7y ago
You can use a closure to pass in the configuration.
def configure_memcache(client_ip, port):
client = pymemcache.client.Client((client_ip, port))
@ring.memcache(client, expire=60)
def get_url(url):
return requests.get(url).content
return get_url
Then in your code which imports the above library:
get_url = configure_memcache('127.0.0.1', 11211)
result = get_url('https://www.google.com')
- sametmax 7y agoThis assumes you define get_url().
- coleifer 7y agoI'd rather have a sane API. def configure_ring(): if DEBUG: return Ring(backend='debug') else: return Ring(backend='memcache', ...) ring = configure_ring() @ring.cache(expire=60) def get_url(...): ... Tons of other libraries out there that implement this exact pattern.
- youknowone 7y agoagree