public
Description: Turtles all the way down
Homepage: http://simonwillison.net/2009/May/19/djng/
Clone URL: git://github.com/simonw/djng.git
djng / example_services_incomplete.py
100644 42 lines (34 sloc) 0.898 kb
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
from djng import services
from djng.services.cache import CacheConfigure
 
# Default service configuration
services.configure('cache', CacheConfigure(
    in_memory = True,
))
# Or maybe this:
# services.cache.configure(CacheConfigure(in_memory = True))
# Or even:
# services.cache.configure(in_memory = True)
# Or...
# services.default('cache', InMemoryCache())
# Or...
# services.configure('cache', InMemoryCache())
 
def app(request):
    from djng.services.cache import cache
    counter = cache.get('counter')
    if not counter:
        counter = 1
    else:
        counter += 1
    cache.set('counter', counter)
    print counter
 
app(None)
app(None)
 
# Middleware that reconfigures service for the duration of the request
app = services.wrap(app, 'cache', InMemoryCache())
 
# Or...
app = services.wrap(app,
    cache = InMemoryCache(),
)
 
 
app(None)
app(None)
app(None)