annotate MoinMoin/support/werkzeug/contrib/cache.py @ 4609:246ba4eecab2

updated werkzeug to 0.5.pre20090228
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 28 Feb 2009 00:08:31 +0100
parents c689dfa55de1
children 8de563c487be
rev   line source
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
2 """
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
3 werkzeug.contrib.cache
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
4 ~~~~~~~~~~~~~~~~~~~~~~
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
5
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
6 The main problem with dynamic Web sites is, well, they're dynamic. Each
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
7 time a user requests a page, the webserver executes a lot of code, queries
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
8 the database, renders templates until the visitor gets the page he sees.
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
9
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
10 This is a lot more expensive than just loading a file from the file system
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
11 and sending it to the visitor.
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
12
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
13 For most Web applications, this overhead isn't a big deal but once it
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
14 becomes, you will be glad to have a cache system in place.
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
15
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
16 How Caching Works
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
17 =================
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
18
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
19 Caching is pretty simple. Basically you have a cache object lurking around
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
20 somewhere that is connected to a remote cache or the file system or
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
21 something else. When the request comes in you check if the current page
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
22 is already in the cache and if, you're returning it. Otherwise you generate
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
23 the page and put it into the cache. (Or a fragment of the page, you don't
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
24 have to cache the full thing)
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
25
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
26 Here a simple example of how to cache a sidebar for a template::
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
27
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
28 def get_sidebar(user):
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
29 identifier = 'sidebar_for/user%d' % user.id
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
30 value = cache.get(identifier)
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
31 if value is not None:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
32 return value
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
33 value = generate_sidebar_for(user=user)
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
34 cache.set(identifier, value, timeout=60 * 5)
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
35 return value
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
36
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
37 Creating a Cache Object
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
38 =======================
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
39
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
40 To create a cache object you just import the cache system of your choice
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
41 from the cache module and instanciate it. Then you can start working
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
42 with that object:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
43
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
44 >>> from werkzeug.contrib.cache import SimpleCache
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
45 >>> c = SimpleCache()
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
46 >>> c.set("foo", "value")
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
47 >>> c.get("foo")
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
48 'value'
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
49 >>> c.get("missing") is None
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
50 True
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
51
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
52 Please keep in mind that you have to create the cache and put it somewhere
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
53 you have access to it (either as a module global you can import or if you
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
54 put it onto your WSGI application).
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
55
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
56 :copyright: (c) 2009 by the Werkzeug Team, see AUTHORS for more details.
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
57 :license: BSD, see LICENSE for more details.
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
58 """
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
59 import os
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
60 import re
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
61 try:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
62 from hashlib import md5
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
63 except ImportError:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
64 from md5 import new as md5
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
65 from itertools import izip
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
66 from time import time
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
67 from cPickle import loads, dumps, load, dump, HIGHEST_PROTOCOL
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
68
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
69
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
70 class BaseCache(object):
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
71 """Baseclass for the cache systems. All the cache systems implement this
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
72 API or a superset of it.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
73
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
74 :param default_timeout: the default timeout that is used if no timeout is
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
75 specified on :meth:`set`.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
76 """
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
77
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
78 def __init__(self, default_timeout=300):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
79 self.default_timeout = default_timeout
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
80
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
81 def get(self, key):
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
82 """Looks up key in the cache and returns it. If the key does not
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
83 exist `None` is returned instead.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
84
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
85 :param key: the key to be looked up.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
86 """
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
87 return None
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
88
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
89 def delete(self, key):
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
90 """Deletes `key` from the cache. If it does not exist in the cache
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
91 nothing happens.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
92
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
93 :param key: the key to delete.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
94 """
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
95 pass
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
96
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
97 def get_many(self, *keys):
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
98 """Returns a list of keys. For each key a item in the list is
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
99 created. Example::
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
100
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
101 foo, bar = cache.get_many("foo", "bar")
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
102
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
103 If a key can't be looked up `None` is returned for that key
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
104 instead.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
105
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
106 :param keys: The function accepts multiple keys as positional
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
107 arguments.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
108 """
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
109 return map(self.get, keys)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
110
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
111 def get_dict(self, *keys):
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
112 """Works like :meth:`get_many` but returns a dict::
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
113
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
114 d = cache.get_dict("foo", "bar")
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
115 foo = d["foo"]
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
116 bar = d["bar"]
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
117
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
118 :param keys: The function accepts multiple keys as positional
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
119 arguments.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
120 """
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
121 return dict(izip(keys, self.get_many(*keys)))
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
122
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
123 def set(self, key, value, timeout=None):
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
124 """Adds or overrides a key in the cache.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
125
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
126 :param key: the key to set
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
127 :param value: the value for the key
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
128 :param timeout: the cache timeout for the key or the default
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
129 timeout if not specified.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
130 """
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
131 pass
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
132
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
133 def add(self, key, value, timeout=None):
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
134 """Works like :meth:`set` but does not override already existing
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
135 values.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
136
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
137 :param key: the key to set
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
138 :param value: the value for the key
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
139 :param timeout: the cache timeout for the key or the default
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
140 timeout if not specified.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
141 """
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
142 pass
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
143
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
144 def set_many(self, mapping, timeout=None):
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
145 """Sets multiple keys and values from a dict.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
146
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
147 :param mapping: a dict with the values to set.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
148 :param timeout: the cache timeout for the key or the default
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
149 timeout if not specified.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
150 """
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
151 for key, value in mapping.iteritems():
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
152 self.set(key, value, timeout)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
153
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
154 def delete_many(self, *keys):
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
155 """Deletes multiple keys at once.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
156
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
157 :param keys: The function accepts multiple keys as positional
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
158 arguments.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
159 """
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
160 for key in keys:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
161 self.delete(key)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
162
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
163 def clear(self):
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
164 """Clears the cache. Keep in mind that not all caches support
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
165 clearning of the full cache.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
166 """
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
167 pass
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
168
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
169 def inc(self, key, delta=1):
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
170 """Increments the value of a key by `delta`. If the key does
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
171 not yet exist it is initialized with `delta`.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
172
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
173 For supporting caches this is an atomic operation.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
174
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
175 :param key: the key to increment.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
176 :param delta: the delta to add.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
177 """
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
178 self.set(key, (self.get(key) or 0) + delta)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
179
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
180 def dec(self, key, delta=1):
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
181 """Decrements the value of a key by `delta`. If the key does
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
182 not yet exist it is initialized with `-delta`.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
183
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
184 For supporting caches this is an atomic operation.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
185
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
186 :param key: the key to increment.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
187 :param delta: the delta to subtract.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
188 """
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
189 self.set(key, (self.get(key) or 0) - delta)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
190
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
191
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
192 class NullCache(BaseCache):
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
193 """A cache that doesn't cache. This can be useful for unit testing.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
194
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
195 :param default_timeout: a dummy parameter that is ignored but exists
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
196 for API compatibility with other caches.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
197 """
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
198
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
199
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
200 class SimpleCache(BaseCache):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
201 """Simple memory cache for single process environments. This class exists
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
202 mainly for the development server and is not 100% thread safe. It tries
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
203 to use as many atomic operations as possible and no locks for simplicity
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
204 but it could happen under heavy load that keys are added multiple times.
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
205
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
206 :param threshold: the maximum number of items the cache stores before
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
207 it starts deleting some.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
208 :param default_timeout: the default timeout that is used if no timeout is
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
209 specified on :meth:`~BaseCache.set`.
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
210 """
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
211
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
212 def __init__(self, threshold=500, default_timeout=300):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
213 BaseCache.__init__(self, default_timeout)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
214 self._cache = {}
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
215 self.clear = self._cache.clear
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
216 self._threshold = threshold
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
217
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
218 def _prune(self):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
219 if len(self._cache) > self._threshold:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
220 now = time()
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
221 for idx, (key, (expires, _)) in enumerate(self._cache.items()):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
222 if expires <= now or idx % 3 == 0:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
223 self._cache.pop(key, None)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
224
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
225 def get(self, key):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
226 now = time()
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
227 expires, value = self._cache.get(key, (0, None))
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
228 if expires > time():
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
229 return loads(value)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
230
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
231 def set(self, key, value, timeout=None):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
232 if timeout is None:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
233 timeout = self.default_timeout
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
234 self._prune()
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
235 self._cache[key] = (time() + timeout, dumps(value, HIGHEST_PROTOCOL))
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
236
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
237 def add(self, key, value, timeout=None):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
238 if timeout is None:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
239 timeout = self.default_timeout
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
240 if len(self._cache) > self._threshold:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
241 self._prune()
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
242 item = (time() + timeout, dumps(value, HIGHEST_PROTOCOL))
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
243 self._cache.setdefault(key, item)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
244
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
245 def delete(self, key):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
246 self._cache.pop(key, None)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
247
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
248
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
249 _test_memcached_key = re.compile(r'[^\x00-\x21\xff]{1,250}$').match
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
250
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
251 class MemcachedCache(BaseCache):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
252 """A cache that uses memcached as backend.
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
253
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
254 The first argument can either be a list or tuple of server addresses
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
255 in which case Werkzeug tries to import the memcache module and connect
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
256 to it, or an object that resembles the API of a :class:`memcache.Client`.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
257
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
258 Implementation notes: This cache backend works around some limitations in
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
259 memcached to simplify the interface. For example unicode keys are encoded
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
260 to utf-8 on the fly. Methods such as :meth:`~BaseCache.get_dict` return
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
261 the keys in the same format as passed. Furthermore all get methods
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
262 silently ignore key errors to not cause problems when untrusted user data
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
263 is passed to the get methods which is often the case in web applications.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
264
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
265 :param servers: a list or tuple of server addresses or alternatively
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
266 a :class:`memcache.Client` or a compatible client.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
267 :param default_timeout: the default timeout that is used if no timeout is
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
268 specified on :meth:`~BaseCache.set`.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
269 :param key_prefix: a prefix that is added before all keys. This makes it
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
270 possible to use the same memcached server for different
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
271 applications. Keep in mind that
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
272 :meth:`~BaseCache.clear` will also clear keys with a
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
273 different prefix.
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
274 """
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
275
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
276 def __init__(self, servers, default_timeout=300, key_prefix=None):
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
277 BaseCache.__init__(self, default_timeout)
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
278 if isinstance(servers, (list, tuple)):
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
279 try:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
280 import cmemcache as memcache
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
281 is_cmemcache = True
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
282 except ImportError:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
283 try:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
284 import memcache
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
285 is_cmemcache = False
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
286 except ImportError:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
287 raise RuntimeError('no memcache module found')
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
288
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
289 # cmemcache has a bug that debuglog is not defined for the
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
290 # client. Whenever pickle fails you get a weird AttributError.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
291 if is_cmemcache:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
292 client = memcache.Client(map(str, servers))
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
293 try:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
294 client.debuglog = lambda *a: None
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
295 except:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
296 pass
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
297 else:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
298 client = memcache.Client(servers, False, HIGHEST_PROTOCOL)
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
299 else:
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
300 client = servers
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
301
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
302 self._client = client
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
303 self.key_prefix = key_prefix
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
304
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
305 def get(self, key):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
306 if isinstance(key, unicode):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
307 key = key.encode('utf-8')
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
308 if self.key_prefix:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
309 key = self.key_prefix + key
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
310 # memcached doesn't support keys longer than that. Because often
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
311 # checks for so long keys can occour because it's tested from user
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
312 # submitted data etc we fail silently for getting.
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
313 if _test_memcached_key(key):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
314 return self._client.get(key)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
315
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
316 def get_dict(self, *keys):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
317 key_mapping = {}
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
318 have_encoded_keys = False
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
319 for idx, key in enumerate(keys):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
320 if isinstance(key, unicode):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
321 encoded_key = key.encode('utf-8')
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
322 have_encoded_keys = True
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
323 else:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
324 encoded_key = key
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
325 if self.key_prefix:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
326 encoded_key = self.key_prefix + encoded_key
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
327 if _test_memcached_key(key):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
328 key_mapping[encoded_key] = key
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
329 # the keys call here is important because otherwise cmemcache
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
330 # does ugly things. What exaclty I don't know, i think it does
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
331 # Py_DECREF but quite frankly i don't care.
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
332 d = rv = self._client.get_multi(key_mapping.keys())
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
333 if have_encoded_keys or self.key_prefix:
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
334 rv = {}
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
335 for key, value in d.iteritems():
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
336 rv[key_mapping[key]] = value
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
337 if len(rv) < len(keys):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
338 for key in keys:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
339 if key not in rv:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
340 rv[key] = None
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
341 return rv
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
342
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
343 def add(self, key, value, timeout=None):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
344 if timeout is None:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
345 timeout = self.default_timeout
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
346 if isinstance(key, unicode):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
347 key = key.encode('utf-8')
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
348 if self.key_prefix:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
349 key = self.key_prefix + key
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
350 self._client.add(key, value, timeout)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
351
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
352 def set(self, key, value, timeout=None):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
353 if timeout is None:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
354 timeout = self.default_timeout
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
355 if isinstance(key, unicode):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
356 key = key.encode('utf-8')
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
357 if self.key_prefix:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
358 key = self.key_prefix + key
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
359 self._client.set(key, value, timeout)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
360
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
361 def get_many(self, *keys):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
362 d = self.get_dict(*keys)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
363 return [d[key] for key in keys]
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
364
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
365 def set_many(self, mapping, timeout=None):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
366 if timeout is None:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
367 timeout = self.default_timeout
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
368 new_mapping = {}
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
369 for key, value in mapping.iteritems():
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
370 if isinstance(key, unicode):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
371 key = key.encode('utf-8')
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
372 if self.key_prefix:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
373 key = self.key_prefix + key
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
374 new_mapping[key] = value
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
375 self._client.set_multi(new_mapping, timeout)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
376
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
377 def delete(self, key):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
378 if isinstance(key, unicode):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
379 key = key.encode('utf-8')
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
380 if self.key_prefix:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
381 key = self.key_prefix + key
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
382 if _test_memcached_key(key):
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
383 self._client.delete(key)
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
384
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
385 def delete_many(self, *keys):
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
386 new_keys = []
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
387 for key in keys:
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
388 if isinstance(key, unicode):
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
389 key = key.encode('utf-8')
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
390 if self.key_prefix:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
391 key = self.key_prefix + key
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
392 if _test_memcached_key(key):
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
393 new_keys.append(key)
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
394 self._client.delete_multi(new_keys)
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
395
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
396 def clear(self):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
397 self._client.flush_all()
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
398
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
399 def inc(self, key, delta=1):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
400 if isinstance(key, unicode):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
401 key = key.encode('utf-8')
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
402 if self.key_prefix:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
403 key = self.key_prefix + key
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
404 self._client.incr(key, delta)
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
405
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
406 def dec(self, key, delta=1):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
407 if isinstance(key, unicode):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
408 key = key.encode('utf-8')
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
409 if self.key_prefix:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
410 key = self.key_prefix + key
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
411 self._client.decr(key, delta)
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
412
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
413
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
414 class GAEMemcachedCache(MemcachedCache):
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
415 """Connects to the Google appengine memcached Cache.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
416
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
417 :param default_timeout: the default timeout that is used if no timeout is
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
418 specified on :meth:`~BaseCache.set`.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
419 :param key_prefix: a prefix that is added before all keys. This makes it
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
420 possible to use the same memcached server for different
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
421 applications. Keep in mind that
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
422 :meth:`~BaseCache.clear` will also clear keys with a
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
423 different prefix.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
424 """
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
425
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
426 def __init__(self, default_timeout=300, key_prefix=None):
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
427 from google.appengine.api import memcache
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
428 MemcachedCache.__init__(self, memcache.Client(),
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
429 default_timeout, key_prefix)
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
430
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
431
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
432 class FileSystemCache(BaseCache):
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
433 """A cache that stores the items on the file system. This cache depends
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
434 on being the only user of the `cache_dir`. Make absolutely sure that
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
435 nobody but this cache stores files there or otherwise the chace will
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
436 randomely delete files therein.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
437
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
438 :param cache_dir: the directory where cached files are stored.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
439 :param threshold: the maximum number of items the cache stores before
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
440 it starts deleting some.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
441 :param default_timeout: the default timeout that is used if no timeout is
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
442 specified on :meth:`~BaseCache.set`.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
443 """
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
444
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
445 def __init__(self, cache_dir, threshold=500, default_timeout=300):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
446 BaseCache.__init__(self, default_timeout)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
447 self._path = cache_dir
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
448 self._threshold = threshold
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
449 if not os.path.exists(self._path):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
450 os.makedirs(self._path)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
451
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
452 def _prune(self):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
453 entries = os.listdir(self._path)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
454 if len(entries) > self._threshold:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
455 now = time()
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
456 for idx, key in enumerate(entries):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
457 try:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
458 f = file(self._get_filename(key))
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
459 if load(f) > now and idx % 3 != 0:
4301
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
460 f.close()
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
461 continue
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
462 except:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
463 f.close()
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
464 self.delete(key)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
465
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
466 def _get_filename(self, key):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
467 hash = md5(key).hexdigest()
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
468 return os.path.join(self._path, hash)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
469
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
470 def get(self, key):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
471 filename = self._get_filename(key)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
472 try:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
473 f = file(filename, 'rb')
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
474 try:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
475 if load(f) >= time():
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
476 return load(f)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
477 finally:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
478 f.close()
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
479 os.remove(filename)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
480 except:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
481 return None
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
482
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
483 def add(self, key, value, timeout=None):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
484 filename = self._get_filename(key)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
485 if not os.path.exists(filename):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
486 self.set(key, value, timeout)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
487
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
488 def set(self, key, value, timeout=None):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
489 if timeout is None:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
490 timeout = self.default_timeout
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
491 filename = self._get_filename(key)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
492 self._prune()
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
493 try:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
494 f = file(filename, 'wb')
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
495 try:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
496 dump(int(time() + timeout), f, 1)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
497 dump(value, f, HIGHEST_PROTOCOL)
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
498 finally:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
499 f.close()
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
500 except (IOError, OSError):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
501 pass
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
502
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
503 def delete(self, key):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
504 try:
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
505 os.remove(self._get_filename(key))
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
506 except (IOError, OSError):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
507 pass
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
508
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
509 def clear(self):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
510 for key in os.listdir(self._path):
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
511 self.delete(key)