comparison MoinMoin/caching.py @ 412:481c432b3d11

fix some error handlers, logging for caching lock failures imported from: moin--main--1.5--patch-416
author Thomas Waldmann <tw@waldmann-edv.de>
date Fri, 03 Feb 2006 20:12:31 +0000
parents c04edbe29fac
children 4110fdc3c1ff
comparison
equal deleted inserted replaced
411:6e2114f8e942 412:481c432b3d11
20 @param request: the request object 20 @param request: the request object
21 @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
22 page local cache area 22 page local cache area
23 @param key: under which key we access the cache content 23 @param key: under which key we access the cache content
24 """ 24 """
25 self.request = request
25 if isinstance(arena, str): 26 if isinstance(arena, str):
26 self.arena_dir = os.path.join(request.cfg.cache_dir, arena) 27 self.arena_dir = os.path.join(request.cfg.cache_dir, arena)
27 filesys.makeDirs(self.arena_dir) 28 filesys.makeDirs(self.arena_dir)
28 else: # arena is in fact a page object 29 else: # arena is in fact a page object
29 cache_dir = None 30 cache_dir = None
30 self.arena_dir = arena.getPagePath('cache', check_create=1) 31 self.arena_dir = arena.getPagePath('cache', check_create=1)
31 self.key = key 32 self.key = key
32 if locking: 33 if locking:
33 lock_dir = os.path.join(self.arena_dir, '__lock__') 34 self.lock_dir = os.path.join(self.arena_dir, '__lock__')
34 self.rlock = lock.ReadLock(lock_dir, 60.0) 35 self.rlock = lock.ReadLock(self.lock_dir, 60.0)
35 self.wlock = lock.WriteLock(lock_dir, 60.0) 36 self.wlock = lock.WriteLock(self.lock_dir, 60.0)
36 37
37 def _filename(self): 38 def _filename(self):
38 return os.path.join(self.arena_dir, self.key) 39 return os.path.join(self.arena_dir, self.key)
39 40
40 def exists(self): 41 def exists(self):
45 return os.path.getmtime(self._filename()) 46 return os.path.getmtime(self._filename())
46 except (IOError, OSError): 47 except (IOError, OSError):
47 return 0 48 return 0
48 49
49 def needsUpdate(self, filename, attachdir=None): 50 def needsUpdate(self, filename, attachdir=None):
50 if not self.exists(): return 1 51 if not self.exists():
52 return 1
51 53
52 try: 54 try:
53 ctime = os.path.getmtime(self._filename()) 55 ctime = os.path.getmtime(self._filename())
54 ftime = os.path.getmtime(filename) 56 ftime = os.path.getmtime(filename)
55 except os.error: 57 except os.error:
77 except OSError: 79 except OSError:
78 pass 80 pass
79 finally: 81 finally:
80 if locking: 82 if locking:
81 self.wlock.release() 83 self.wlock.release()
84 else:
85 request.log("Can't acquire write lock in %s", self.lock_dir)
82 86
83 def update(self, content, encode=False): 87 def update(self, content, encode=False):
84 if encode: 88 if encode:
85 content = content.encode(config.charset) 89 content = content.encode(config.charset)
86 if not locking or locking and self.wlock.acquire(1.0): 90 if not locking or locking and self.wlock.acquire(1.0):
93 except OSError: 97 except OSError:
94 pass 98 pass
95 finally: 99 finally:
96 if locking: 100 if locking:
97 self.wlock.release() 101 self.wlock.release()
102 else:
103 request.log("Can't acquire write lock in %s", self.lock_dir)
98 104
99 def remove(self): 105 def remove(self):
100 try: 106 try:
101 os.remove(self._filename()) 107 os.remove(self._filename())
102 except OSError: 108 except OSError:
109 data = f.read() 115 data = f.read()
110 f.close() 116 f.close()
111 finally: 117 finally:
112 if locking: 118 if locking:
113 self.rlock.release() 119 self.rlock.release()
120 else:
121 request.log("Can't acquire read lock in %s", self.lock_dir)
114 if decode: 122 if decode:
115 data = data.decode(config.charset) 123 data = data.decode(config.charset)
116 return data 124 return data
117 125