annotate MoinMoin/caching.py @ 1638:5070d0358544

caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 22 Oct 2006 20:48:23 +0200
parents 47a173ae5401
children c0cb0884f7ec
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
1316
c4f6af5ee295 Mute the tempnam warning in the caching module.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1309
diff changeset
10 import warnings
c4f6af5ee295 Mute the tempnam warning in the caching module.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1309
diff changeset
11
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
12 # cPickle can encode normal and Unicode strings
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
13 # see http://docs.python.org/lib/node66.html
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
14 try:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
15 import cPickle as pickle
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
16 except ImportError:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
17 import pickle
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
18
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
19 # Set pickle protocol, see http://docs.python.org/lib/node64.html
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
20 PICKLE_PROTOCOL = pickle.HIGHEST_PROTOCOL
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
21
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
22 from MoinMoin import config
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
23 from MoinMoin.util import filesys, lock
931
141083b64fb1 whitespace and minor style changes only
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 902
diff changeset
24
1316
c4f6af5ee295 Mute the tempnam warning in the caching module.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1309
diff changeset
25 # filter the tempname warning because we create the tempfile only in directories
c4f6af5ee295 Mute the tempnam warning in the caching module.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1309
diff changeset
26 # where only we should have write access initially
c4f6af5ee295 Mute the tempnam warning in the caching module.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1309
diff changeset
27 warnings.filterwarnings("ignore", "tempnam.*security", RuntimeWarning, "MoinMoin.caching")
c4f6af5ee295 Mute the tempnam warning in the caching module.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1309
diff changeset
28
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
29 class CacheError(Exception):
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
30 """ raised if we have trouble reading or writing to the cache """
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
31 pass
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
32
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
33 class CacheEntry:
1638
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
34 def __init__(self, request, arena, key, scope='page_or_wiki', do_locking=True, use_pickle=False):
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
35 """ 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
36 @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
37 @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
38 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
39 @param key: under which key we access the cache content
902
6cc24670fd0f Speeded up linkto: search by avoiding read locks on pagelinks retrieval.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 683
diff changeset
40 @param lock: if there should be a lock, normally True
681
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
41 @param scope: the scope where we are caching:
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
42 'item' - an item local cache
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
43 'wiki' - a wiki local cache
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
44 'farm' - a cache for the whole farm
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
45 """
412
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
46 self.request = request
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
47 self.key = key
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
48 self.locking = do_locking
1638
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
49 self.use_pickle = use_pickle
683
a2beb43407ea deprecate usage of CacheEntry without explicit scope
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 681
diff changeset
50 if scope == 'page_or_wiki': # XXX DEPRECATED, remove later
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
51 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
52 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
53 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
54 self.arena_dir = arena.getPagePath('cache', check_create=1)
681
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
55 elif scope == 'item': # arena is a Page instance
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
56 # we could move cache out of the page directory and store it to cache_dir
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
57 self.arena_dir = arena.getPagePath('cache', check_create=1)
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
58 elif scope == 'wiki':
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
59 self.arena_dir = os.path.join(request.cfg.cache_dir, request.cfg.siteid, arena)
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
60 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
61 self.arena_dir = os.path.join(request.cfg.cache_dir, '__common__', arena)
1429
1d55eff67af6 check for non-existing dir before calling os.makedirs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1428
diff changeset
62 if not os.path.exists(self.arena_dir):
1428
8a89649ab5f0 remove util.filesys.make[dD]irs - not needed any more due to os.umask() use
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1317
diff changeset
63 os.makedirs(self.arena_dir)
902
6cc24670fd0f Speeded up linkto: search by avoiding read locks on pagelinks retrieval.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 683
diff changeset
64 if self.locking:
412
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
65 self.lock_dir = os.path.join(self.arena_dir, '__lock__')
1309
238c43cb8877 lock: LazyReadLock, LazyWriteLock == do nothing. Only for POSIX OS and POSIX style of code using that kind of locks (see comments there).
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1307
diff changeset
66 self.rlock = lock.LazyReadLock(self.lock_dir, 60.0)
238c43cb8877 lock: LazyReadLock, LazyWriteLock == do nothing. Only for POSIX OS and POSIX style of code using that kind of locks (see comments there).
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1307
diff changeset
67 self.wlock = lock.LazyWriteLock(self.lock_dir, 60.0)
931
141083b64fb1 whitespace and minor style changes only
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 902
diff changeset
68
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
69 def _filename(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
70 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
71
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
72 def _tmpfilename(self):
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
73 return os.tempnam(self.arena_dir, self.key)
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
74
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
75 def exists(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
76 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
77
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
78 def mtime(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
79 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
80 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
81 except (IOError, OSError):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
82 return 0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
83
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
84 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
85 if not self.exists():
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
86 return 1
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
87
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
88 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
89 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
90 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
91 except os.error:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
92 return 1
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
93
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
94 needsupdate = ftime > ctime
931
141083b64fb1 whitespace and minor style changes only
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 902
diff changeset
95
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
96 # 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
97 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
98 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
99 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
100 except os.error:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
101 ftime2 = 0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
102 needsupdate = ftime2 > ctime
931
141083b64fb1 whitespace and minor style changes only
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 902
diff changeset
103
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
104 return needsupdate
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
105
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
106 def copyto(self, filename):
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
107 # currently unused function
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
108 import shutil
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
109 tmpfname = self._tmpfilename()
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
110 fname = self._filename()
902
6cc24670fd0f Speeded up linkto: search by avoiding read locks on pagelinks retrieval.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 683
diff changeset
111 if not self.locking or self.locking and self.wlock.acquire(1.0):
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
112 try:
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
113 shutil.copyfile(filename, tmpfname)
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
114 # this is either atomic or happening with real locks set:
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
115 filesys.rename(tmpfname, fname)
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
116 finally:
902
6cc24670fd0f Speeded up linkto: search by avoiding read locks on pagelinks retrieval.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 683
diff changeset
117 if self.locking:
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
118 self.wlock.release()
412
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
119 else:
463
051f79718bdd fixing yet another bug in error logging calls
Thomas Waldmann <tw@waldmann-edv.de>
parents: 458
diff changeset
120 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
121
1638
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
122 def update(self, content):
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
123 try:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
124 tmpfname = self._tmpfilename()
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
125 fname = self._filename()
1638
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
126 if self.use_pickle:
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
127 content = pickle.dumps(content, PICKLE_PROTOCOL)
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
128 if not self.locking or self.locking and self.wlock.acquire(1.0):
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
129 try:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
130 # we do not write content to old inode, but to a new file
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
131 # se we don't need to lock when we just want to read the file
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
132 # (at least on POSIX, this works)
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
133 f = open(tmpfname, 'wb')
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
134 f.write(content)
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
135 f.close()
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
136 # this is either atomic or happening with real locks set:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
137 filesys.rename(tmpfname, fname)
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
138 finally:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
139 if self.locking:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
140 self.wlock.release()
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
141 else:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
142 self.request.log("Can't acquire write lock in %s" % self.lock_dir)
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
143 except (pickle.PicklingError, IOError, ValueError), err:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
144 raise CacheError(str(err))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
145
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
146 def remove(self):
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
147 if not self.locking or self.locking and self.wlock.acquire(1.0):
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
148 try:
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
149 try:
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
150 os.remove(self._filename())
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
151 except OSError:
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
152 pass
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
153 finally:
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
154 if self.locking:
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
155 self.wlock.release()
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
156 else:
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
157 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
158
1638
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
159 def content(self):
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
160 try:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
161 if not self.locking or self.locking and self.rlock.acquire(1.0):
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
162 try:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
163 f = open(self._filename(), 'rb')
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
164 data = f.read()
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
165 f.close()
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
166 finally:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
167 if self.locking:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
168 self.rlock.release()
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
169 else:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
170 self.request.log("Can't acquire read lock in %s" % self.lock_dir)
1638
5070d0358544 caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1637
diff changeset
171 if self.use_pickle:
1637
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
172 data = pickle.loads(data)
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
173 return data
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
174 except (pickle.UnpicklingError, IOError, EOFError, ValueError), err:
47a173ae5401 caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1429
diff changeset
175 raise CacheError(str(err))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
176