This repository has been archived by the owner on Jul 14, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|