Files
web2py/gluon/contrib/gae_memcache.py
T
Loren McGinnis 378a696bfe related to issue 1708 - bugfix to allow no expiration for gae_memcache
When gae_memcache passed expiration time to memcache Client
(see commit 59290534bc),
logic is no longer needed to calculate time delta, since memcache
handles that for us.  Also, time_expire=0 was expiring values
immediately, instead of the documented behavior where 0 signified
no expiration.

Kept timestamp in value for backwards compatibility (is this
necessary since cached data is transient?)
2014-01-12 22:52:52 -07:00

68 lines
1.6 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Developed by Robin Bhattacharyya (memecache for GAE)
Released under the web2py license (LGPL)
from gluon.contrib.gae_memcache import MemcacheClient
cache.ram=cache.disk=MemcacheClient(request)
"""
import time
from google.appengine.api.memcache import Client
class MemcacheClient(object):
client = Client()
def __init__(self, request):
self.request = request
def __call__(
self,
key,
f,
time_expire=300,
):
key = '%s/%s' % (self.request.application, key)
value = None
obj = self.client.get(key)
if obj:
value = obj[1]
elif f is not None:
value = f()
self.client.set(key, (time.time(), value), time=time_expire)
return value
def increment(self, key, value=1):
key = '%s/%s' % (self.request.application, key)
obj = self.client.get(key)
if obj:
value = obj[1] + value
self.client.set(key, (time.time(), value))
return value
def incr(self, key, value=1):
return self.increment(key, value)
def clear(self, key=None):
if key:
key = '%s/%s' % (self.request.application, key)
self.client.delete(key)
else:
self.client.flush_all()
def delete(self, *a, **b):
return self.client.delete(*a, **b)
def get(self, *a, **b):
return self.client.get(*a, **b)
def set(self, *a, **b):
return self.client.set(*a, **b)
def flush_all(self, *a, **b):
return self.client.delete(*a, **b)