comparison 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
comparison
equal deleted inserted replaced
401:61e50f3aaf68 402:c04edbe29fac
8 8
9 import os 9 import os
10 from MoinMoin import config 10 from MoinMoin import config
11 from MoinMoin.util import filesys 11 from MoinMoin.util import filesys
12 12
13 locking = 1
14 if locking:
15 from MoinMoin.util import lock
16
13 class CacheEntry: 17 class CacheEntry:
14 def __init__(self, request, arena, key): 18 def __init__(self, request, arena, key):
15 """ init a cache entry 19 """ init a cache entry
16 @param request: the request object 20 @param request: the request object
17 @param arena: either a string or a page object, when we want to use 21 @param arena: either a string or a page object, when we want to use
23 filesys.makeDirs(self.arena_dir) 27 filesys.makeDirs(self.arena_dir)
24 else: # arena is in fact a page object 28 else: # arena is in fact a page object
25 cache_dir = None 29 cache_dir = None
26 self.arena_dir = arena.getPagePath('cache', check_create=1) 30 self.arena_dir = arena.getPagePath('cache', check_create=1)
27 self.key = key 31 self.key = key
28 32 if locking:
33 lock_dir = os.path.join(self.arena_dir, '__lock__')
34 self.rlock = lock.ReadLock(lock_dir, 60.0)
35 self.wlock = lock.WriteLock(lock_dir, 60.0)
36
29 def _filename(self): 37 def _filename(self):
30 return os.path.join(self.arena_dir, self.key) 38 return os.path.join(self.arena_dir, self.key)
31 39
32 def exists(self): 40 def exists(self):
33 return os.path.exists(self._filename()) 41 return os.path.exists(self._filename())
59 67
60 return needsupdate 68 return needsupdate
61 69
62 def copyto(self, filename): 70 def copyto(self, filename):
63 import shutil 71 import shutil
64 shutil.copyfile(filename, self._filename()) 72 if not locking or locking and self.wlock.acquire(1.0):
65 73 try:
66 try: 74 shutil.copyfile(filename, self._filename())
67 os.chmod(self._filename(), 0666 & config.umask) 75 try:
68 except OSError: 76 os.chmod(self._filename(), 0666 & config.umask)
69 pass 77 except OSError:
78 pass
79 finally:
80 if locking:
81 self.wlock.release()
70 82
71 def update(self, content, encode=False): 83 def update(self, content, encode=False):
72 if encode: 84 if encode:
73 content = content.encode(config.charset) 85 content = content.encode(config.charset)
74 open(self._filename(), 'wb').write(content) 86 if not locking or locking and self.wlock.acquire(1.0):
75 87 try:
76 try: 88 f = open(self._filename(), 'wb')
77 os.chmod(self._filename(), 0666 & config.umask) 89 f.write(content)
78 except OSError: 90 f.close()
79 pass 91 try:
92 os.chmod(self._filename(), 0666 & config.umask)
93 except OSError:
94 pass
95 finally:
96 if locking:
97 self.wlock.release()
80 98
81 def remove(self): 99 def remove(self):
82 try: 100 try:
83 os.remove(self._filename()) 101 os.remove(self._filename())
84 except OSError: 102 except OSError:
85 pass 103 pass
86 104
87 def content(self, decode=False): 105 def content(self, decode=False):
88 data = open(self._filename(), 'rb').read() 106 if not locking or locking and self.rlock.acquire(1.0):
107 try:
108 f = open(self._filename(), 'rb')
109 data = f.read()
110 f.close()
111 finally:
112 if locking:
113 self.rlock.release()
89 if decode: 114 if decode:
90 data = data.decode(config.charset) 115 data = data.decode(config.charset)
91 return data 116 return data
92 117