changeset 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 61e50f3aaf68
children 689396849b0a
files ChangeLog MoinMoin/caching.py
diffstat 2 files changed, 53 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Jan 27 18:47:39 2006 +0000
+++ b/ChangeLog	Sat Jan 28 23:11:15 2006 +0000
@@ -2,6 +2,20 @@
 # arch-tag: automatic-ChangeLog--arch@arch.thinkmo.de--2003-archives/moin--main--1.5
 #
 
+2006-01-29 00:11:15 GMT	Thomas Waldmann <tw@waldmann-edv.de>	patch-406
+
+    Summary:
+      add locking to caching module
+    Revision:
+      moin--main--1.5--patch-406
+
+    add locking to caching module
+    
+
+    modified files:
+     ChangeLog MoinMoin/caching.py
+
+
 2006-01-27 19:47:39 GMT	Thomas Waldmann <tw@waldmann-edv.de>	patch-405
 
     Summary:
--- a/MoinMoin/caching.py	Fri Jan 27 18:47:39 2006 +0000
+++ b/MoinMoin/caching.py	Sat Jan 28 23:11:15 2006 +0000
@@ -10,6 +10,10 @@
 from MoinMoin import config
 from MoinMoin.util import filesys
 
+locking = 1
+if locking:
+    from MoinMoin.util import lock
+    
 class CacheEntry:
     def __init__(self, request, arena, key):
         """ init a cache entry
@@ -25,7 +29,11 @@
             cache_dir = None
             self.arena_dir = arena.getPagePath('cache', check_create=1)
         self.key = key
-
+        if locking:
+            lock_dir = os.path.join(self.arena_dir, '__lock__')
+            self.rlock = lock.ReadLock(lock_dir, 60.0)
+            self.wlock = lock.WriteLock(lock_dir, 60.0)
+        
     def _filename(self):
         return os.path.join(self.arena_dir, self.key)
 
@@ -61,22 +69,32 @@
 
     def copyto(self, filename):
         import shutil
-        shutil.copyfile(filename, self._filename())
-
-        try:
-            os.chmod(self._filename(), 0666 & config.umask)
-        except OSError:
-            pass
+        if not locking or locking and self.wlock.acquire(1.0):
+            try:
+                shutil.copyfile(filename, self._filename())
+                try:
+                    os.chmod(self._filename(), 0666 & config.umask)
+                except OSError:
+                    pass
+            finally:
+                if locking:
+                    self.wlock.release()
 
     def update(self, content, encode=False):
         if encode:
             content = content.encode(config.charset)
-        open(self._filename(), 'wb').write(content)
-
-        try:
-            os.chmod(self._filename(), 0666 & config.umask)
-        except OSError:
-            pass
+        if not locking or locking and self.wlock.acquire(1.0):
+            try:
+                f = open(self._filename(), 'wb')
+                f.write(content)
+                f.close()
+                try:
+                    os.chmod(self._filename(), 0666 & config.umask)
+                except OSError:
+                    pass
+            finally:
+                if locking:
+                    self.wlock.release()
 
     def remove(self):
         try:
@@ -85,7 +103,14 @@
             pass
 
     def content(self, decode=False):
-        data = open(self._filename(), 'rb').read()
+        if not locking or locking and self.rlock.acquire(1.0):
+            try:
+                f = open(self._filename(), 'rb')
+                data = f.read()
+                f.close()
+            finally:
+                if locking:
+                    self.rlock.release()
         if decode:
             data = data.decode(config.charset)
         return data