source: OpenRLabs-Git/deploy/rlabs-docker/web2py-rlabs/gluon/contrib/gae_memcache.py

main
Last change on this file was 42bd667, checked in by David Fuertes <dfuertes@…>, 4 years ago

Historial Limpio

  • Property mode set to 100755
File size: 1.8 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""
5Developed by Robin Bhattacharyya (memecache for GAE)
6Released under the web2py license (LGPL)
7
8from gluon.contrib.gae_memcache import MemcacheClient
9cache.ram=cache.disk=MemcacheClient(request)
10"""
11
12import time
13from google.appengine.api.memcache import Client
14
15
16class MemcacheClient(object):
17
18    client = Client()
19
20    def __init__(self, request, default_time_expire = 300):
21        self.request = request
22        self.default_time_expire = default_time_expire
23
24    def initialize(self):
25        pass
26
27    def __call__(
28        self,
29        key,
30        f,
31        time_expire=None,
32    ):
33        if time_expire is None:
34            time_expire = self.default_time_expire
35
36        key = '%s/%s' % (self.request.application, key)
37        value = None
38        obj = self.client.get(key) if time_expire != 0 else None
39        if obj:
40            value = obj[1]
41        elif f is not None:
42            value = f()
43            self.client.set(key, (time.time(), value), time=time_expire)
44        return value
45
46    def increment(self, key, value=1):
47        key = '%s/%s' % (self.request.application, key)
48        obj = self.client.get(key)
49        if obj:
50            value = obj[1] + value
51        self.client.set(key, (time.time(), value))
52        return value
53
54    def incr(self, key, value=1):
55        return self.increment(key, value)
56
57    def clear(self, key=None):
58        if key:
59            key = '%s/%s' % (self.request.application, key)
60            self.client.delete(key)
61        else:
62            self.client.flush_all()
63
64    def delete(self, *a, **b):
65        return self.client.delete(*a, **b)
66
67    def get(self, *a, **b):
68        return self.client.get(*a, **b)
69
70    def set(self, *a, **b):
71        return self.client.set(*a, **b)
72
73    def flush_all(self, *a, **b):
74        return self.client.delete(*a, **b)
Note: See TracBrowser for help on using the repository browser.