Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Commit

Permalink
use memcached as the cache backend
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfrank committed Sep 26, 2016
1 parent 8d172a4 commit d78edf3
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions evelink/cache/memcached.py
@@ -0,0 +1,36 @@
import pickle
import time
import memcache

from evelink import api

class MemcachedCache(api.APICache):
"""An implementation of APICache using memcached."""

def __init__(self, serverList, prefix='el_'):
"""
serverList could be ['127.0.0.1:11211']
prefix allows multiple applications to share the same memcached instance if necessary
"""
super(MemcachedCache, self).__init__()
self.mc = memcache.Client(serverList, debug=0)
self.prefix = prefix


def get(self, key):
prefix_key = self.prefix + key
result = self.mc.get(prefix_key)
if not result:
return None
value, expiration = result
if expiration < time.time():
self.mc.delete(prefix_key)
return None
return value

def put(self, key, value, duration):
prefix_key = self.prefix + key
expiration = time.time() + duration
res = self.mc.set(prefix_key, (value, expiration), time=duration)
return res

0 comments on commit d78edf3

Please sign in to comment.