annotate MoinMoin/caching.py @ 680:d6556513c4d3

add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 21 May 2006 16:54:45 +0200
parents 051f79718bdd
children 2d5883e4b82c
rev   line source
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
2 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
3 MoinMoin caching module
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
4
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
5 @copyright: 2001-2004 by Jürgen Hermann <jh@web.de>
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
6 @license: GNU GPL, see COPYING for details.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
7 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
8
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
9 import os
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
10 from MoinMoin import config
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
11 from MoinMoin.util import filesys
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
12
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
13 locking = 1
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
14 if locking:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
15 from MoinMoin.util import lock
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
16
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
17 class CacheEntry:
680
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
18 def __init__(self, request, arena, key, scope='page_or_wiki'):
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
19 """ init a cache entry
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
20 @param request: the request object
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
21 @param arena: either a string or a page object, when we want to use
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
22 page local cache area
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
23 @param key: under which key we access the cache content
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
24 """
412
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
25 self.request = request
680
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
26 if scope == 'page_or_wiki': # XXX split and refactor later
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
27 if isinstance(arena, str):
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
28 self.arena_dir = os.path.join(request.cfg.cache_dir, request.cfg.siteid, arena)
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
29 filesys.makeDirs(self.arena_dir)
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
30 else: # arena is in fact a page object
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
31 self.arena_dir = arena.getPagePath('cache', check_create=1)
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
32 elif scope == 'farm':
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
33 self.arena_dir = os.path.join(request.cfg.cache_dir, '__common__', arena)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
34 filesys.makeDirs(self.arena_dir)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
35 self.key = key
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
36 if locking:
412
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
37 self.lock_dir = os.path.join(self.arena_dir, '__lock__')
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
38 self.rlock = lock.ReadLock(self.lock_dir, 60.0)
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
39 self.wlock = lock.WriteLock(self.lock_dir, 60.0)
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
40
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
41 def _filename(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
42 return os.path.join(self.arena_dir, self.key)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
43
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
44 def exists(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
45 return os.path.exists(self._filename())
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
46
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
47 def mtime(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
48 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
49 return os.path.getmtime(self._filename())
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
50 except (IOError, OSError):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
51 return 0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
52
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
53 def needsUpdate(self, filename, attachdir=None):
412
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
54 if not self.exists():
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
55 return 1
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
56
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
57 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
58 ctime = os.path.getmtime(self._filename())
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
59 ftime = os.path.getmtime(filename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
60 except os.error:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
61 return 1
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
62
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
63 needsupdate = ftime > ctime
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
64
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
65 # if a page depends on the attachment dir, we check this, too:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
66 if not needsupdate and attachdir:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
67 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
68 ftime2 = os.path.getmtime(attachdir)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
69 except os.error:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
70 ftime2 = 0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
71 needsupdate = ftime2 > ctime
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
72
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
73 return needsupdate
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
74
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
75 def copyto(self, filename):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
76 import shutil
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
77 if not locking or locking and self.wlock.acquire(1.0):
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
78 try:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
79 shutil.copyfile(filename, self._filename())
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
80 try:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
81 os.chmod(self._filename(), 0666 & config.umask)
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
82 except OSError:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
83 pass
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
84 finally:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
85 if locking:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
86 self.wlock.release()
412
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
87 else:
463
051f79718bdd fixing yet another bug in error logging calls
Thomas Waldmann <tw@waldmann-edv.de>
parents: 458
diff changeset
88 self.request.log("Can't acquire write lock in %s" % self.lock_dir)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
89
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
90 def update(self, content, encode=False):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
91 if encode:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
92 content = content.encode(config.charset)
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
93 if not locking or locking and self.wlock.acquire(1.0):
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
94 try:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
95 f = open(self._filename(), 'wb')
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
96 f.write(content)
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
97 f.close()
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
98 try:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
99 os.chmod(self._filename(), 0666 & config.umask)
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
100 except OSError:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
101 pass
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
102 finally:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
103 if locking:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
104 self.wlock.release()
412
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
105 else:
463
051f79718bdd fixing yet another bug in error logging calls
Thomas Waldmann <tw@waldmann-edv.de>
parents: 458
diff changeset
106 self.request.log("Can't acquire write lock in %s" % self.lock_dir)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
107
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
108 def remove(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
109 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
110 os.remove(self._filename())
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
111 except OSError:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
112 pass
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
113
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
114 def content(self, decode=False):
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
115 if not locking or locking and self.rlock.acquire(1.0):
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
116 try:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
117 f = open(self._filename(), 'rb')
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
118 data = f.read()
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
119 f.close()
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
120 finally:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
121 if locking:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
122 self.rlock.release()
412
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
123 else:
463
051f79718bdd fixing yet another bug in error logging calls
Thomas Waldmann <tw@waldmann-edv.de>
parents: 458
diff changeset
124 self.request.log("Can't acquire read lock in %s" % self.lock_dir)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
125 if decode:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
126 data = data.decode(config.charset)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
127 return data
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
128