From 10bfeb232415d4caf8726cb5dd746afbce27f7c7 Mon Sep 17 00:00:00 2001 From: john Date: Fri, 18 Apr 2003 15:37:29 +0000 Subject: [PATCH] fixed up CacheKey with appropriate equals and hashCode methods, and set the MRUCache and OpenIndy to use CacheKey's instead of hackish String keys also changed logger.warn to logger.info for the cache timing statements. --- source/mircoders/global/CacheKey.java | 11 +++++++++-- source/mircoders/global/MRUCache.java | 3 ++- source/mircoders/servlet/ServletModuleOpenIndy.java | 12 ++++++------ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/source/mircoders/global/CacheKey.java b/source/mircoders/global/CacheKey.java index 30b7e21b..0e10fc80 100755 --- a/source/mircoders/global/CacheKey.java +++ b/source/mircoders/global/CacheKey.java @@ -40,8 +40,15 @@ public class CacheKey { type=theType; selector=theSelector; } - public boolean equals(CacheKey aCacheKey){ - if (aCacheKey.type.equals(type) && aCacheKey.selector.equals(selector)) + + public int hashCode(){ + return type.hashCode()+selector.hashCode(); + } + + public boolean equals(Object aCacheKey){ + if (!(aCacheKey instanceof CacheKey)) + return false; + if (((CacheKey) aCacheKey).type.equals(type) && ((CacheKey) aCacheKey).selector.equals(selector)) return true; else return false; diff --git a/source/mircoders/global/MRUCache.java b/source/mircoders/global/MRUCache.java index 8e8a415a..f0c1a4e3 100755 --- a/source/mircoders/global/MRUCache.java +++ b/source/mircoders/global/MRUCache.java @@ -72,6 +72,7 @@ public class MRUCache { public boolean hasObject(CacheKey aCacheKey) { synchronized (cache) { + logger.info("MRUCache was this big : "+ mruList.size()); return cache.containsKey(aCacheKey); } } @@ -90,7 +91,7 @@ public class MRUCache { if (! hasObject(aCacheKey)){ // add to the cache cache.put(aCacheKey,data); - if (mruList.size() > cacheMaxItems){ + if (mruList.size() >= cacheMaxItems){ removeObject((CacheKey) mruList.getLast()); } } diff --git a/source/mircoders/servlet/ServletModuleOpenIndy.java b/source/mircoders/servlet/ServletModuleOpenIndy.java index 55675b1b..e0e70d93 100755 --- a/source/mircoders/servlet/ServletModuleOpenIndy.java +++ b/source/mircoders/servlet/ServletModuleOpenIndy.java @@ -94,7 +94,7 @@ import mir.util.HTTPRequestParser; import mir.util.StringRoutines; import mircoders.entity.EntityComment; import mircoders.entity.EntityContent; -//import mircoders.global.CacheKey; +import mircoders.global.CacheKey; import mircoders.global.MirGlobal; import mircoders.media.*; import mircoders.media.UnsupportedMediaFormatExc; @@ -127,7 +127,7 @@ import mircoders.storage.DatabaseTopics; * open-postings to the newswire * * @author mir-coders group - * @version $Id: ServletModuleOpenIndy.java,v 1.76 2003/04/18 14:54:54 john Exp $ + * @version $Id: ServletModuleOpenIndy.java,v 1.77 2003/04/18 15:37:29 john Exp $ * */ @@ -1105,16 +1105,16 @@ public class ServletModuleOpenIndy extends ServletModule String cacheType="pdf"; - String theCacheKey = cacheType+":"+cacheSelector; + CacheKey theCacheKey = new CacheKey(cacheType,cacheSelector); byte[] thePDF; if (MirGlobal.mruCache().hasObject(theCacheKey)){ - logger.warn("fetching pdf from cache"); + logger.info("fetching pdf from cache"); thePDF = (byte[]) MirGlobal.mruCache().getObject(theCacheKey); } else { - logger.warn("generating pdf and caching it"); + logger.info("generating pdf and caching it"); ByteArrayOutputStream out = new ByteArrayOutputStream(); PDFGenerator pdfMaker = new PDFGenerator(out); @@ -1148,7 +1148,7 @@ public class ServletModuleOpenIndy extends ServletModule res.getOutputStream().write(thePDF); res.getOutputStream().flush(); String elapsedtime=(new Long(System.currentTimeMillis()-starttime)).toString(); - logger.warn("pdf retireval took "+elapsedtime + " milliseconds" ); + logger.info("pdf retireval took "+elapsedtime + " milliseconds" ); } else { -- 2.11.0