annotate MoinMoin/caching.py @ 402:c04edbe29fac

add locking to caching module imported from: moin--main--1.5--patch-406
author Thomas Waldmann <tw@waldmann-edv.de>
date Sat, 28 Jan 2006 23:11:15 +0000
parents 77665d8e2254
children 481c432b3d11
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:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
18 def __init__(self, request, arena, key):
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 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
25 if isinstance(arena, str):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
26 self.arena_dir = os.path.join(request.cfg.cache_dir, arena)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
27 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
28 else: # arena is in fact a page object
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
29 cache_dir = None
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
30 self.arena_dir = arena.getPagePath('cache', check_create=1)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
31 self.key = key
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
32 if locking:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
33 lock_dir = os.path.join(self.arena_dir, '__lock__')
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
34 self.rlock = lock.ReadLock(lock_dir, 60.0)
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
35 self.wlock = lock.WriteLock(lock_dir, 60.0)
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
36
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
37 def _filename(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
38 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
39
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
40 def exists(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
41 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
42
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
43 def mtime(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
44 try:
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.getmtime(self._filename())
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
46 except (IOError, OSError):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
47 return 0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
48
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
49 def needsUpdate(self, filename, attachdir=None):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
50 if not self.exists(): return 1
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
51
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
52 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
53 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
54 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
55 except os.error:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
56 return 1
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
57
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
58 needsupdate = ftime > ctime
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
59
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
60 # 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
61 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
62 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
63 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
64 except os.error:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
65 ftime2 = 0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
66 needsupdate = ftime2 > ctime
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
67
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
68 return needsupdate
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
69
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
70 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
71 import shutil
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
72 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
73 try:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
74 shutil.copyfile(filename, self._filename())
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
75 try:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
76 os.chmod(self._filename(), 0666 & config.umask)
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
77 except OSError:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
78 pass
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
79 finally:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
80 if locking:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
81 self.wlock.release()
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
82
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
83 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
84 if encode:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
85 content = content.encode(config.charset)
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
86 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
87 try:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
88 f = open(self._filename(), 'wb')
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
89 f.write(content)
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
90 f.close()
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
91 try:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
92 os.chmod(self._filename(), 0666 & config.umask)
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
93 except OSError:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
94 pass
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
95 finally:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
96 if locking:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
97 self.wlock.release()
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
98
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
99 def remove(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
100 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
101 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
102 except OSError:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
103 pass
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
104
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
105 def content(self, decode=False):
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
106 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
107 try:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
108 f = open(self._filename(), 'rb')
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
109 data = f.read()
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
110 f.close()
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
111 finally:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
112 if locking:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
113 self.rlock.release()
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
114 if decode:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
115 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
116 return data
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
117