From: <mol...@us...> - 2010-06-10 11:24:19
|
Revision: 2638 http://openutils.svn.sourceforge.net/openutils/?rev=2638&view=rev Author: molaschi Date: 2010-06-10 11:24:11 +0000 (Thu, 10 Jun 2010) Log Message: ----------- [CACHE-1] replaced lock between getting cache content and flushing by disabling cache while flushing Modified Paths: -------------- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheManager.java Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java 2010-06-09 17:14:21 UTC (rev 2637) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java 2010-06-10 11:24:11 UTC (rev 2638) @@ -68,8 +68,6 @@ private String basePath = "fscache"; - private Object lock = new Object(); - private boolean flushOnStop = false; private boolean reloadAtStartup; @@ -82,6 +80,8 @@ private Voter gzipVoter; + private volatile boolean active = true; + /** * Logger. */ @@ -321,27 +321,23 @@ boolean gzipable = gzipVoter != null && gzipVoter.vote(request) > 0; - synchronized (lock) + boolean miss = !contents.containsKey(getKey(request)); + if (miss) { + cacheMisses++; + } + else + { + cacheHits++; + } - boolean miss = !contents.containsKey(getKey(request)); - if (miss) - { - cacheMisses++; - } - else - { - cacheHits++; - } - - if (miss) - { - String fileName = getCacheDir() + getKey(request); - contents.put(getKey(request), FSCachedItem.createNew(fileName, gzipable)); - cachePuts++; - } - return contents.get(getKey(request)); + if (miss) + { + String fileName = getCacheDir() + getKey(request); + contents.put(getKey(request), FSCachedItem.createNew(fileName, gzipable)); + cachePuts++; } + return contents.get(getKey(request)); } /** @@ -452,7 +448,9 @@ */ public void flush() { - synchronized (lock) + active = false; + + try { // wait that every rw operation finished for (String key : contents.keySet()) @@ -471,6 +469,18 @@ cacheDir.delete(); } } + finally + { + active = true; + } } + /** + * {@inheritDoc} + */ + public boolean isActive() + { + return active; + } + } Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java 2010-06-09 17:14:21 UTC (rev 2637) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java 2010-06-10 11:24:11 UTC (rev 2638) @@ -120,7 +120,7 @@ throws IOException, ServletException { // null check for broken configurations, needed - if (cacheManager != null) + if (cacheManager != null && cacheManager.isActive()) { doFilterForContent(request, response, chain); } Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheManager.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheManager.java 2010-06-09 17:14:21 UTC (rev 2637) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheManager.java 2010-06-10 11:24:11 UTC (rev 2638) @@ -41,4 +41,6 @@ void start(); void stop(); + + boolean isActive(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2010-09-09 21:25:47
|
Revision: 3013 http://openutils.svn.sourceforge.net/openutils/?rev=3013&view=rev Author: fgiust Date: 2010-09-09 21:25:40 +0000 (Thu, 09 Sep 2010) Log Message: ----------- javadoc cleanup Modified Paths: -------------- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheResponseWrapper.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/voters/AllInOneCacheVoter.java Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java 2010-09-09 21:13:25 UTC (rev 3012) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java 2010-09-09 21:25:40 UTC (rev 3013) @@ -68,7 +68,7 @@ private String basePath = "fscache"; - private boolean flushOnStop = false; + private boolean flushOnStop; private boolean reloadAtStartup; @@ -283,7 +283,7 @@ { uri += "root"; } - String uriTokens[] = StringUtils.split(uri, "/"); + String[] uriTokens = StringUtils.split(uri, "/"); for (int i = 0; i < uriTokens.length; i++) { try Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java 2010-09-09 21:13:25 UTC (rev 3012) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java 2010-09-09 21:25:40 UTC (rev 3013) @@ -46,7 +46,7 @@ * @author Fabrizio Giustina * @version $Id$ */ -public class FSCachedItem extends LockableCacheContent +public final class FSCachedItem extends LockableCacheContent { private String filename; @@ -236,7 +236,8 @@ return true; } - size = sizeGzip = 0; + size = 0; + sizeGzip = 0; lastModified = System.currentTimeMillis(); releaseLockToWrite(); @@ -286,15 +287,15 @@ } } - private static final int GZIP_MAGIC_NUMBER_BYTE_1 = 31; - - private static final int GZIP_MAGIC_NUMBER_BYTE_2 = -117; - /** * Checks the first two bytes of the candidate byte array for the magic number 0x677a. */ private boolean isGZipped(byte[] candidate) { + + final int GZIP_MAGIC_NUMBER_BYTE_1 = 31; + final int GZIP_MAGIC_NUMBER_BYTE_2 = -117; + if (candidate == null || candidate.length < 2) { return false; Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java 2010-09-09 21:13:25 UTC (rev 3012) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java 2010-09-09 21:25:40 UTC (rev 3013) @@ -33,15 +33,16 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import net.sourceforge.openutils.mgnlsimplecache.managers.CachedItem; import net.sourceforge.openutils.mgnlsimplecache.managers.CacheHeaders; import net.sourceforge.openutils.mgnlsimplecache.managers.CacheManager; import net.sourceforge.openutils.mgnlsimplecache.managers.CacheResponseWrapper; +import net.sourceforge.openutils.mgnlsimplecache.managers.CachedItem; import org.apache.commons.io.IOUtils; /** + * Main cache filter, to be configured in magnolia filter chain. * @author Manuel Molaschi * @author Fabrizio Giustina * @version $Id$ Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheResponseWrapper.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheResponseWrapper.java 2010-09-09 21:13:25 UTC (rev 3012) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheResponseWrapper.java 2010-09-09 21:25:40 UTC (rev 3013) @@ -44,9 +44,9 @@ private CachedItem content; - boolean redirect; + private boolean redirect; - boolean error; + private boolean error; public CacheResponseWrapper(HttpServletResponse response, CachedItem content, OutputStream cachingStream) { @@ -215,30 +215,35 @@ this.stream2 = stream2; } + @Override public void write(int value) throws IOException { stream1.write(value); stream2.write(value); } + @Override public void write(byte[] value) throws IOException { stream1.write(value); stream2.write(value); } + @Override public void write(byte[] b, int off, int len) throws IOException { stream1.write(b, off, len); stream2.write(b, off, len); } + @Override public void flush() throws IOException { stream1.flush(); stream2.flush(); } + @Override public void close() throws IOException { try Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/voters/AllInOneCacheVoter.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/voters/AllInOneCacheVoter.java 2010-09-09 21:13:25 UTC (rev 3012) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/voters/AllInOneCacheVoter.java 2010-09-09 21:25:40 UTC (rev 3013) @@ -45,27 +45,54 @@ { // just to make everything more explicit - public static boolean CACHE = false; + public static final boolean CACHE = false; // just to make everything more explicit - public static boolean DONTCACHE = true; + public static final boolean DONTCACHE = true; + /** + * Comma separated list of allowed extensions. + */ protected String[] extensionsAllowed; + /** + * Comma separated list of denied extensions. + */ protected String[] extensionsDenied; + /** + * Comma separated list of allowed content types. + */ protected String[] contentTypesAllowed; + /** + * Comma separated list of denied content types. + */ protected String[] contentTypesDenied; + /** + * Cache requests with parameters? + */ protected boolean allowRequestWithParameters; + /** + * Cache content on admin instances? + */ protected boolean allowAdmin; + /** + * Cache content for authenticated users? + */ protected boolean allowAuthenticated; + /** + * Comma separated list of allowed start paths. + */ protected String[] pathsAllowed; + /** + * Comma separated list of denied start paths. + */ protected String[] pathsDenied; /** @@ -74,8 +101,8 @@ private Logger log = LoggerFactory.getLogger(AllInOneCacheVoter.class); /** - * Sets the extensionsAllowed. - * @param extensionsAllowed the extensionsAllowed to set + * Sets a comma separated list of allowed extensions. + * @param extensionsAllowed comma separated list of allowed extensions */ public void setExtensionsAllowed(String extensionsAllowed) { @@ -83,8 +110,8 @@ } /** - * Sets the extensionsDenied. - * @param extensionsDenied the extensionsDenied to set + * Sets a comma separated list of denied extensions. + * @param extensionsDenied comma separated list of denied extensions */ public void setExtensionsDenied(String extensionsDenied) { @@ -92,8 +119,8 @@ } /** - * Sets the contentTypesAllowed. - * @param contentTypesAllowed the contentTypesAllowed to set + * Sets a comma separated list of allowed content types. + * @param contentTypesAllowed comma separated list of allowed content types */ public void setContentTypesAllowed(String contentTypesAllowed) { @@ -101,8 +128,8 @@ } /** - * Sets the contentTypesDenied. - * @param contentTypesDenied the contentTypesDenied to set + * Sets a comma separated list of denied content types. + * @param contentTypesDenied comma separated list of denied content types. */ public void setContentTypesDenied(String contentTypesDenied) { @@ -110,7 +137,7 @@ } /** - * Sets the allowRequestWithParameters. + * Cache requests with parameters? * @param allowRequestWithParameters the allowRequestWithParameters to set */ public void setAllowRequestWithParameters(boolean allowRequestWithParameters) @@ -119,7 +146,7 @@ } /** - * Sets the allowAdmin. + * Cache content on admin instances? * @param allowAdmin the allowAdmin to set */ public void setAllowAdmin(boolean allowAdmin) @@ -128,7 +155,7 @@ } /** - * Sets the allowAuthenticated. + * Cache content for authenticated users? * @param allowAuthenticated the allowAuthenticated to set */ public void setAllowAuthenticated(boolean allowAuthenticated) @@ -137,8 +164,8 @@ } /** - * Sets the pathsAllowed. - * @param pathsAllowed the pathsAllowed to set + * Sets a comma separated list of allowed start paths. + * @param pathsAllowed comma separated list of allowed start paths. */ public void setPathsAllowed(String pathsAllowed) { @@ -146,8 +173,8 @@ } /** - * Sets the pathsDenied. - * @param pathsDenied the pathsDenied to set + * Sets a comma separated list of denied start paths. + * @param pathsDenied comma separated list of denied start paths. */ public void setPathsDenied(String pathsDenied) { @@ -198,12 +225,14 @@ { if (log.isDebugEnabled()) { - log.debug(logmessage, "extension [" - + extension - + "] doesn't match allowed:" - + ArrayUtils.toString(extensionsAllowed) - + " denied: " - + ArrayUtils.toString(extensionsDenied)); + log.debug( + logmessage, + "extension [" + + extension + + "] doesn't match allowed:" + + ArrayUtils.toString(extensionsAllowed) + + " denied: " + + ArrayUtils.toString(extensionsDenied)); } return DONTCACHE; } @@ -216,12 +245,14 @@ { if (log.isDebugEnabled()) { - log.debug(logmessage, "content type [" - + contentType - + "] doesn't match allowed:" - + ArrayUtils.toString(contentTypesAllowed) - + " denied: " - + ArrayUtils.toString(contentTypesDenied)); + log.debug( + logmessage, + "content type [" + + contentType + + "] doesn't match allowed:" + + ArrayUtils.toString(contentTypesAllowed) + + " denied: " + + ArrayUtils.toString(contentTypesDenied)); } return DONTCACHE; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <mol...@us...> - 2010-10-25 09:58:31
|
Revision: 3118 http://openutils.svn.sourceforge.net/openutils/?rev=3118&view=rev Author: molaschi Date: 2010-10-25 09:58:25 +0000 (Mon, 25 Oct 2010) Log Message: ----------- improve caching concurrent checks Modified Paths: -------------- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/LockableCacheContent.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/SynchCacheContentOperations.java Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java 2010-10-12 14:02:23 UTC (rev 3117) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java 2010-10-25 09:58:25 UTC (rev 3118) @@ -455,9 +455,7 @@ // wait that every rw operation finished for (String key : contents.keySet()) { - contents.get(key).lockToWrite(); - // when i have lock i can remove; - contents.get(key).releaseLockToWrite(); + contents.get(key).waitForWritingLock(); } contents.clear(); Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java 2010-10-12 14:02:23 UTC (rev 3117) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java 2010-10-25 09:58:25 UTC (rev 3118) @@ -173,11 +173,14 @@ */ public ResetableBufferedOutputStream beginWrite() throws IOException { - lockToWrite(); - isnew = false; - File f = new File(filename); - f.getParentFile().mkdirs(); - return new ResetableBufferedFileOutputStream(f, this); + if (lockToWrite()) + { + isnew = false; + File f = new File(filename); + f.getParentFile().mkdirs(); + return new ResetableBufferedFileOutputStream(f, this); + } + return null; } /** @@ -185,63 +188,67 @@ */ public boolean endWrite(OutputStream outputStream) throws IOException { - IOUtils.closeQuietly(outputStream); - - File f = ((ResetableBufferedFileOutputStream) outputStream).getFile(); - if (f.exists() && f.length() > 0) + try { - FileInputStream fis = new FileInputStream(f); - byte[] gzipControlChars = new byte[2]; - fis.read(gzipControlChars); - IOUtils.closeQuietly(fis); + IOUtils.closeQuietly(outputStream); - if (isGZipped(gzipControlChars)) + File f = ((ResetableBufferedFileOutputStream) outputStream).getFile(); + if (f != null && f.exists() && f.length() > 0) { - FileUtils.moveFile(new File(filename), new File(filenameGzip)); - File fgz = new File(filenameGzip); - f = new File(filename); - InputStream is = new GZIPInputStream(new FileInputStream(fgz)); - OutputStream os = new BufferedOutputStream(new FileOutputStream(f)); - IOUtils.copyLarge(is, os); - IOUtils.closeQuietly(is); - IOUtils.closeQuietly(os); - } - else if (gzipable) - { - File fgz = new File(filenameGzip); - OutputStream os = new GZIPOutputStream(new FileOutputStream(fgz)); - InputStream is = new FileInputStream(f); - IOUtils.copyLarge(is, os); - IOUtils.closeQuietly(is); - IOUtils.closeQuietly(os); - } + FileInputStream fis = new FileInputStream(f); + byte[] gzipControlChars = new byte[2]; + fis.read(gzipControlChars); + IOUtils.closeQuietly(fis); - size = new File(filename).length(); - sizeGzip = new File(filenameGzip).length(); + if (isGZipped(gzipControlChars)) + { + FileUtils.moveFile(new File(filename), new File(filenameGzip)); + File fgz = new File(filenameGzip); + f = new File(filename); + InputStream is = new GZIPInputStream(new FileInputStream(fgz)); + OutputStream os = new BufferedOutputStream(new FileOutputStream(f)); + IOUtils.copyLarge(is, os); + IOUtils.closeQuietly(is); + IOUtils.closeQuietly(os); + } + else if (gzipable) + { + File fgz = new File(filenameGzip); + OutputStream os = new GZIPOutputStream(new FileOutputStream(fgz)); + InputStream is = new FileInputStream(f); + IOUtils.copyLarge(is, os); + IOUtils.closeQuietly(is); + IOUtils.closeQuietly(os); + } - getCacheHeaders().getHeaders().put("x-server-cached", "true"); + size = new File(filename).length(); + sizeGzip = new File(filenameGzip).length(); - OutputStream os = new BufferedOutputStream(new FileOutputStream(new File(filenameHeaders))); - try - { - getCacheHeaders().serialize(os); + getCacheHeaders().getHeaders().put("x-server-cached", "true"); + + OutputStream os = new BufferedOutputStream(new FileOutputStream(new File(filenameHeaders))); + try + { + getCacheHeaders().serialize(os); + } + finally + { + IOUtils.closeQuietly(os); + } + + lastModified = System.currentTimeMillis(); + return true; } - finally - { - IOUtils.closeQuietly(os); - } + size = 0; + sizeGzip = 0; lastModified = System.currentTimeMillis(); + return false; + } + finally + { releaseLockToWrite(); - return true; } - - size = 0; - sizeGzip = 0; - lastModified = System.currentTimeMillis(); - - releaseLockToWrite(); - return false; } /** Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java 2010-10-12 14:02:23 UTC (rev 3117) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java 2010-10-25 09:58:25 UTC (rev 3118) @@ -142,9 +142,16 @@ // get cache from manager CachedItem cacheContent = cacheManager.get(request); - // if request is cached - if (!cacheContent.isNew()) + OutputStream os = null; + if (cacheContent.isNew()) { + // if someone is writing, returns null + os = cacheContent.beginWrite(); + } + + // if request is cached (not new or cacheContent.beginWrite returns null) + if (os == null) + { InputStream cacheContentInputStream = cacheContent.beginRead(acceptGzip); boolean reset = true; @@ -196,8 +203,6 @@ } } - OutputStream os = cacheContent.beginWrite(); - this.startProcessing(); // wrap response and start writing CacheResponseWrapper cacheContentResponse = new CacheResponseWrapper(response, cacheContent, os); Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/LockableCacheContent.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/LockableCacheContent.java 2010-10-12 14:02:23 UTC (rev 3117) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/LockableCacheContent.java 2010-10-25 09:58:25 UTC (rev 3118) @@ -19,13 +19,17 @@ package net.sourceforge.openutils.mgnlsimplecache.lock; +import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock; import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; import net.sourceforge.openutils.mgnlsimplecache.managers.CachedItem; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Manuel Molaschi * @author Fabrizio Giustina @@ -34,6 +38,13 @@ public abstract class LockableCacheContent implements CachedItem { + /** + * Logger. + */ + private Logger log = LoggerFactory.getLogger(LockableCacheContent.class); + + private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + private ReadLock readLock; private WriteLock writeLock; @@ -43,7 +54,6 @@ */ public LockableCacheContent() { - ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); readLock = lock.readLock(); writeLock = lock.writeLock(); } @@ -53,11 +63,41 @@ readLock.lock(); } - public void lockToWrite() + public boolean lockToWrite() { - writeLock.lock(); + if (lock.isWriteLockedByCurrentThread()) + { + RuntimeException ex = new RuntimeException("Double call to write lock while caching!!!"); + ex.fillInStackTrace(); + log.error("Exception getting lock for writing on cache: ", ex); + throw ex; + } + return writeLock.tryLock(); } + public void waitForWritingLock() + { + if (isWritingLocked()) + { + try + { + writeLock.tryLock(10, TimeUnit.SECONDS); + } + catch (InterruptedException e) + { + + } + finally + { + int holdCount = lock.getWriteHoldCount(); + for (int i = 0; i < holdCount; i++) + { + writeLock.unlock(); + } + } + } + } + public void releaseLockToWrite() { writeLock.unlock(); @@ -67,4 +107,9 @@ { readLock.unlock(); } + + public boolean isWritingLocked() + { + return lock.isWriteLocked(); + } } Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/SynchCacheContentOperations.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/SynchCacheContentOperations.java 2010-10-12 14:02:23 UTC (rev 3117) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/SynchCacheContentOperations.java 2010-10-25 09:58:25 UTC (rev 3118) @@ -54,7 +54,7 @@ public static <T, S> T doWrite(CachedItem cache, S arguments, SynchedOp<T, S> callback) throws Exception { boolean release = false; - if (cache instanceof LockableCacheContent) + if (cache instanceof LockableCacheContent && !((LockableCacheContent) cache).isWritingLocked()) { ((LockableCacheContent) cache).lockToWrite(); release = true; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2011-06-11 08:21:50
|
Revision: 3520 http://openutils.svn.sourceforge.net/openutils/?rev=3520&view=rev Author: fgiust Date: 2011-06-11 08:21:43 +0000 (Sat, 11 Jun 2011) Log Message: ----------- multiple changes to locking and flushing Modified Paths: -------------- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/LockableCacheContent.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheManager.java Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java 2011-06-11 07:57:38 UTC (rev 3519) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCacheManager.java 2011-06-11 08:21:43 UTC (rev 3520) @@ -269,7 +269,7 @@ * @param request * @return */ - protected String getKey(HttpServletRequest request) + public String getKey(HttpServletRequest request) { String key = (String) request.getAttribute(cacheKeyInRequest); if (key == null) @@ -350,10 +350,17 @@ // handle concurrent cache flushing if (content != null) { - content.lockToWrite(); + boolean locked = content.lockToWrite(); content.flush(); contents.remove(getKey(request)); - content.releaseLockToWrite(); + if (locked) + { + content.releaseLockToWrite(); + } + else + { + log.debug("Content not locked: {}", request.getRequestURI()); + } } } @@ -416,7 +423,7 @@ repository = StringUtils.substringBefore(repositoryUrl, ":"); path = StringUtils.substringAfter(repositoryUrl, ":"); } - ObservationUtil.registerChangeListener(repository, path, this); + ObservationUtil.registerDeferredChangeListener(repository, path, this, 5000, 30000); } } } @@ -474,12 +481,13 @@ public void run() { log.info("Deleting stale cache dir {}", cacheDir.getAbsolutePath()); - boolean deleted = renamedDir.delete(); - if (deleted) + + try { + FileUtils.deleteDirectory(renamedDir); log.info("Cache dir {} successfully deleted", cacheDir.getAbsolutePath()); } - else + catch (IOException e) { log.info("Unable to delete cache dir {}", cacheDir.getAbsolutePath()); } Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java 2011-06-11 07:57:38 UTC (rev 3519) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java 2011-06-11 08:21:43 UTC (rev 3520) @@ -188,6 +188,13 @@ */ public boolean endWrite(OutputStream outputStream) throws IOException { + + if (outputStream == null) + { + releaseLockToWrite(); + return false; + } + try { IOUtils.closeQuietly(outputStream); Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java 2011-06-11 07:57:38 UTC (rev 3519) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java 2011-06-11 08:21:43 UTC (rev 3520) @@ -39,6 +39,8 @@ import net.sourceforge.openutils.mgnlsimplecache.managers.CachedItem; import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** @@ -53,6 +55,11 @@ private CacheManager cacheManager; /** + * Logger. + */ + private Logger log = LoggerFactory.getLogger(CacheFilter.class); + + /** * Returns the cacheContentManager. * @return the cacheContentManager */ @@ -147,6 +154,7 @@ if (cacheContent == null) { chain.doFilter(request, response); + return; } OutputStream os = null; @@ -154,60 +162,23 @@ { // if someone is writing, returns null os = cacheContent.beginWrite(); + + if (os == null) + { + log.debug("Concurrent request for {} while caching", cacheManager.getKey(request)); + } } // if request is cached (not new or cacheContent.beginWrite returns null) if (os == null) { - InputStream cacheContentInputStream = cacheContent.beginRead(acceptGzip); + boolean gotContentFromCache = returnCachedContent(request, response, acceptGzip, cacheContent); - boolean reset = true; - try + if (!gotContentFromCache) { - if (cacheContentInputStream != null) - { - // user requires cache refresh - // don't confuse client cache with server cache, user requested a refresh of the browser cache, - // this doesn't mean the server can't send the cached page back! - if (!"no-cache".equals(request.getHeader("Cache-Control"))) - { - if (!this.ifModifiedSince(request, cacheContent.getCreationTime())) - { - reset = false; - response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); - return; - } - } - - CacheHeaders cacheHeaders = cacheContent.getCacheHeaders(); - cacheHeaders.apply(response); - - // stream from cache - response.setDateHeader("Last-Modified", cacheContent.getCreationTime()); - response.setContentLength((int) cacheContent.getBodyLength(acceptGzip)); - if (acceptGzip && cacheContent.hasGzip()) - { - response.setHeader("Content-Encoding", "gzip"); - response.setHeader("Vary", "Accept-Encoding"); - } - IOUtils.copy(cacheContentInputStream, response.getOutputStream()); - response.setStatus(HttpServletResponse.SC_OK); - reset = false; - return; - } + log.debug("No content got from cache for {}", cacheManager.getKey(request)); } - catch (Throwable e) - { - throw new ServletException(e); - } - finally - { - cacheContent.endRead(cacheContentInputStream); - if (reset) - { - cacheManager.reset(request); - } - } + return; } this.startProcessing(); @@ -246,6 +217,69 @@ } /** + * @param request + * @param response + * @param acceptGzip + * @param cacheContent + * @throws IOException + * @throws ServletException + */ + private boolean returnCachedContent(final HttpServletRequest request, final HttpServletResponse response, + final boolean acceptGzip, CachedItem cacheContent) throws IOException, ServletException + { + InputStream cacheContentInputStream = cacheContent.beginRead(acceptGzip); + + boolean reset = true; + try + { + if (cacheContentInputStream != null) + { + // user requires cache refresh + // don't confuse client cache with server cache, user requested a refresh of the browser cache, + // this doesn't mean the server can't send the cached page back! + if (!"no-cache".equals(request.getHeader("Cache-Control"))) + { + if (!this.ifModifiedSince(request, cacheContent.getCreationTime())) + { + reset = false; + response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); + return true; + } + } + + CacheHeaders cacheHeaders = cacheContent.getCacheHeaders(); + cacheHeaders.apply(response); + + // stream from cache + response.setDateHeader("Last-Modified", cacheContent.getCreationTime()); + response.setContentLength((int) cacheContent.getBodyLength(acceptGzip)); + if (acceptGzip && cacheContent.hasGzip()) + { + response.setHeader("Content-Encoding", "gzip"); + response.setHeader("Vary", "Accept-Encoding"); + } + IOUtils.copy(cacheContentInputStream, response.getOutputStream()); + response.setStatus(HttpServletResponse.SC_OK); + reset = false; + return true; + } + } + catch (Throwable e) + { + throw new ServletException(e); + } + finally + { + cacheContent.endRead(cacheContentInputStream); + if (reset) + { + cacheManager.reset(request); + } + } + return false; + } + + /** * Check if server cache is newer then the client cache * @param request The servlet request we are processing * @return boolean true if the server resource is newer Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/LockableCacheContent.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/LockableCacheContent.java 2011-06-11 07:57:38 UTC (rev 3519) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/LockableCacheContent.java 2011-06-11 08:21:43 UTC (rev 3520) @@ -50,7 +50,7 @@ private WriteLock writeLock; /** - * + * */ public LockableCacheContent() { @@ -100,7 +100,10 @@ public void releaseLockToWrite() { - writeLock.unlock(); + if (lock.isWriteLockedByCurrentThread()) + { + writeLock.unlock(); + } } public void releaseLockToRead() Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheManager.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheManager.java 2011-06-11 07:57:38 UTC (rev 3519) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheManager.java 2011-06-11 08:21:43 UTC (rev 3520) @@ -34,6 +34,8 @@ CachedItem get(HttpServletRequest request); + String getKey(HttpServletRequest request); + void reset(HttpServletRequest request) throws IOException; void flush(); @@ -41,6 +43,6 @@ void start(); void stop(); - + boolean isActive(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2011-06-13 13:51:04
|
Revision: 3527 http://openutils.svn.sourceforge.net/openutils/?rev=3527&view=rev Author: fgiust Date: 2011-06-13 13:50:57 +0000 (Mon, 13 Jun 2011) Log Message: ----------- CACHE-4 hasGzip() returns always true Modified Paths: -------------- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java 2011-06-13 08:28:31 UTC (rev 3526) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java 2011-06-13 13:50:57 UTC (rev 3527) @@ -137,7 +137,9 @@ */ public boolean hasGzip() { - return getBodyLength(true) > 0; + // the call to getBodyLength() is needed to force the check if the size is not cached + getBodyLength(true); + return sizeGzip > 0; } /** Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java 2011-06-13 08:28:31 UTC (rev 3526) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java 2011-06-13 13:50:57 UTC (rev 3527) @@ -207,6 +207,11 @@ if (cacheContentResponse.isError() || cacheContentResponse.isRedirect() || !hasContent) { + log.warn("Resetting {}: error={} redirect={}, empty={}", new Object[]{ + cacheManager.getKey(request), + cacheContentResponse.isError(), + cacheContentResponse.isRedirect(), + !hasContent }); cacheManager.reset(request); } } @@ -227,8 +232,10 @@ private boolean returnCachedContent(final HttpServletRequest request, final HttpServletResponse response, final boolean acceptGzip, CachedItem cacheContent) throws IOException, ServletException { - InputStream cacheContentInputStream = cacheContent.beginRead(acceptGzip); + boolean getGzip = acceptGzip && cacheContent.hasGzip(); + InputStream cacheContentInputStream = cacheContent.beginRead(getGzip); + boolean reset = true; try { @@ -252,8 +259,8 @@ // stream from cache response.setDateHeader("Last-Modified", cacheContent.getCreationTime()); - response.setContentLength((int) cacheContent.getBodyLength(acceptGzip)); - if (acceptGzip && cacheContent.hasGzip()) + response.setContentLength((int) cacheContent.getBodyLength(getGzip)); + if (getGzip) { response.setHeader("Content-Encoding", "gzip"); response.setHeader("Vary", "Accept-Encoding"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2012-03-10 08:33:20
|
Revision: 3747 http://openutils.svn.sourceforge.net/openutils/?rev=3747&view=rev Author: fgiust Date: 2012-03-10 08:33:13 +0000 (Sat, 10 Mar 2012) Log Message: ----------- CACHE-7 New option "waitwhenwriting" in cache filter Modified Paths: -------------- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/LockableCacheContent.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/SynchCacheContentOperations.java trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheResponseWrapper.java Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java 2012-03-10 08:29:07 UTC (rev 3746) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filesystem/FSCachedItem.java 2012-03-10 08:33:13 UTC (rev 3747) @@ -147,17 +147,17 @@ */ public InputStream beginRead(boolean acceptGzip) throws IOException { - lockToRead(); - File f = new File(acceptGzip ? filenameGzip : filename); - if (f.exists()) + if (lockToRead()) { - FileInputStream fis = new FileInputStream(f); - return new BufferedInputStream(fis); + File f = new File(acceptGzip ? filenameGzip : filename); + if (f.exists()) + { + FileInputStream fis = new FileInputStream(f); + return new BufferedInputStream(fis); + } } - else - { - return null; - } + + return null; } /** Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java 2012-03-10 08:29:07 UTC (rev 3746) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/filters/CacheFilter.java 2012-03-10 08:33:13 UTC (rev 3747) @@ -54,6 +54,8 @@ private CacheManager cacheManager; + private boolean waitwhenwriting = false; + /** * Logger. */ @@ -93,6 +95,15 @@ } /** + * Sets the waitwhenwriting. + * @param waitwhenwriting the waitwhenwriting to set + */ + public void setWaitwhenwriting(boolean waitwhenwriting) + { + this.waitwhenwriting = waitwhenwriting; + } + + /** * {@inheritDoc} */ @Override @@ -166,6 +177,11 @@ if (os == null) { log.debug("Concurrent request for {} while caching", cacheManager.getKey(request)); + if (!waitwhenwriting) + { + chain.doFilter(request, response); + return; + } } } @@ -236,7 +252,6 @@ boolean getGzip = acceptGzip && cacheContent.hasGzip(); InputStream cacheContentInputStream = cacheContent.beginRead(getGzip); - boolean reset = true; try { if (cacheContentInputStream != null) @@ -248,7 +263,6 @@ { if (!this.ifModifiedSince(request, cacheContent.getCreationTime())) { - reset = false; response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return true; } @@ -267,7 +281,7 @@ } IOUtils.copy(cacheContentInputStream, response.getOutputStream()); response.setStatus(HttpServletResponse.SC_OK); - reset = false; + return true; } } @@ -277,10 +291,9 @@ } finally { - cacheContent.endRead(cacheContentInputStream); - if (reset) + if (cacheContentInputStream != null) { - cacheManager.reset(request); + cacheContent.endRead(cacheContentInputStream); } } return false; Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/LockableCacheContent.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/LockableCacheContent.java 2012-03-10 08:29:07 UTC (rev 3746) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/LockableCacheContent.java 2012-03-10 08:33:13 UTC (rev 3747) @@ -58,9 +58,16 @@ writeLock = lock.writeLock(); } - public void lockToRead() + public boolean lockToRead() { - readLock.lock(); + try + { + return readLock.tryLock(60, TimeUnit.SECONDS); + } + catch (InterruptedException e) + { + return false; + } } public boolean lockToWrite() Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/SynchCacheContentOperations.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/SynchCacheContentOperations.java 2012-03-10 08:29:07 UTC (rev 3746) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/lock/SynchCacheContentOperations.java 2012-03-10 08:33:13 UTC (rev 3747) @@ -35,8 +35,7 @@ boolean release = false; if (cache instanceof LockableCacheContent) { - ((LockableCacheContent) cache).lockToRead(); - release = true; + release = ((LockableCacheContent) cache).lockToRead(); } try { Modified: trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheResponseWrapper.java =================================================================== --- trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheResponseWrapper.java 2012-03-10 08:29:07 UTC (rev 3746) +++ trunk/openutils-mgnlcache/src/main/java/net/sourceforge/openutils/mgnlsimplecache/managers/CacheResponseWrapper.java 2012-03-10 08:33:13 UTC (rev 3747) @@ -85,7 +85,15 @@ @Override public void flushBuffer() throws IOException { - super.flushBuffer(); + try + { + super.flushBuffer(); + } + catch (IOException e) + { + error = true; + throw e; + } if (cachingStream != null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |