summaryrefslogtreecommitdiff
path: root/cherrypy/lib/caching.py
blob: 3f70b62f9a15dac3c5dfb6dd4fde991f497eab46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import Queue
import threading
import time

import cherrypy
from cherrypy.lib import cptools


class MemoryCache:
    
    def __init__(self):
        self.clear()
        self.expirationQueue = Queue.Queue()
        t = threading.Thread(target=self.expireCache, name='expireCache')
        self.expirationThread = t
        t.setDaemon(True)
        t.start()
    
    def clear(self):
        """Reset the cache to its initial, empty state."""
        self.cache = {}
        self.totPuts = 0
        self.totGets = 0
        self.totHits = 0
        self.totExpires = 0
        self.totNonModified = 0
        self.cursize = 0
    
    def _key(self):
        return cherrypy.request.config.get("tools.caching.key", cherrypy.request.browser_url)
    key = property(_key)
    
    def _maxobjsize(self):
        return cherrypy.request.config.get("tools.caching.maxobjsize", 100000)
    maxobjsize = property(_maxobjsize)
    
    def _maxsize(self):
        return cherrypy.request.config.get("tools.caching.maxsize", 10000000)
    maxsize = property(_maxsize)
    
    def _maxobjects(self):
        return cherrypy.request.config.get("tools.caching.maxobjects", 1000)
    maxobjects = property(_maxobjects)
    
    def expireCache(self):
        while True:
            expirationTime, objSize, objKey = self.expirationQueue.get(block=True, timeout=None)
            # expireCache runs in a separate thread which the servers are
            # not aware of. It's possible that "time" will be set to None
            # arbitrarily, so we check "while time" to avoid exceptions.
            # See tickets #99 and #180 for more information.
            while time and (time.time() < expirationTime):
                time.sleep(0.1)
            try:
                del self.cache[objKey]
                self.totExpires += 1
                self.cursize -= objSize
            except KeyError:
                # the key may have been deleted elsewhere
                pass
    
    def get(self):
        """
        If the content is in the cache, returns a tuple containing the 
        expiration time, the lastModified response header and the object 
        (rendered as a string); returns None if the key is not found.
        """
        self.totGets += 1
        cacheItem = self.cache.get(self.key, None)
        if cacheItem:
            self.totHits += 1
            return cacheItem
        else:
            return None
    
    def put(self, lastModified, obj):
        # Size check no longer includes header length
        objSize = len(obj[2])
        totalSize = self.cursize + objSize
        
        # checks if there's space for the object
        if ((objSize < self.maxobjsize) and 
            (totalSize < self.maxsize) and 
            (len(self.cache) < self.maxobjects)):
            # add to the expirationQueue & cache
            try:
                expirationTime = time.time() + cherrypy.request.config.get("tools.caching.delay", 600)
                objKey = self.key
                self.expirationQueue.put((expirationTime, objSize, objKey))
                self.cache[objKey] = (expirationTime, lastModified, obj)
                self.totPuts += 1
                self.cursize += objSize
            except Queue.Full:
                # can't add because the queue is full
                return


def init(cache_class=None):
    if cache_class is None:
        cache_class = MemoryCache
    cherrypy._cache = cache_class()

def get():
    # Ignore POST, PUT, DELETE.
    # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.10.
    invalid = cherrypy.config.get("tools.caching.invalid_methods",
                                  ("POST", "PUT", "DELETE"))
    if cherrypy.request.method in invalid:
        cherrypy.request.cached = c = False
    else:
        cacheData = cherrypy._cache.get()
        cherrypy.request.cached = c = bool(cacheData)
    
    if c:
        expirationTime, lastModified, obj = cacheData
        s, cherrypy.response.header_list, b = obj
        try:
            cptools.validate_since()
        except cherrypy.HTTPError, x:
            if x.status == 304:
                cherrypy._cache.totNonModified += 1
            raise
        
        # serve it & get out from the request
        cherrypy.response.status = s
        cherrypy.response.body = b
    return c

def tee_output():
    if cherrypy.request.cached:
        return
    
    response = cherrypy.response
    output = []
    def tee(body):
        """Tee response.body into a list."""
        for chunk in body:
            output.append(chunk)
            yield chunk
        # Might as well do this here; why cache if the body isn't consumed?
        if response.headers.get('Pragma', None) != 'no-cache':
            # save the cache data
            lastModified = response.headers.get('Last-Modified', None)
            body = ''.join([chunk for chunk in output])
            cherrypy._cache.put(lastModified,
                                (response.status, response.header_list, body))
    response.body = tee(response.body)


# CherryPy interfaces. Pick one.

def enable(**kwargs):
    """Compile-time decorator (turn on the tool in config)."""
    def wrapper(f):
        if not hasattr(f, "_cp_config"):
            f._cp_config = {}
        f._cp_config["tools.caching.on"] = True
        for k, v in kwargs.iteritems():
            f._cp_config["tools.caching." + k] = v
        return f
    return wrapper

def _setup():
    """Hook caching into cherrypy.request using the given conf."""
    conf = cherrypy.request.toolmap.get("caching", {})
    if not getattr(cherrypy, "_cache", None):
        init(conf.get("class", None))
    def wrapper():
        if get():
            cherrypy.request.handler = None
        else:
            # Note the devious technique here of adding hooks on the fly
            cherrypy.request.hooks.attach('before_finalize', tee_output)
    cherrypy.request.hooks.attach('before_main', wrapper)