From bd9ac0902b4c3de455040c42e1b02915f28a2be9 Mon Sep 17 00:00:00 2001 From: mdb Date: Sun, 27 Apr 2003 07:37:32 +0000 Subject: [PATCH] Added the ability to temporarily disable flushing on LRU hash maps. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1104 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/LRUHashMap.java | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/LRUHashMap.java b/projects/samskivert/src/java/com/samskivert/util/LRUHashMap.java index ea09c3d2..9a3e987e 100644 --- a/projects/samskivert/src/java/com/samskivert/util/LRUHashMap.java +++ b/projects/samskivert/src/java/com/samskivert/util/LRUHashMap.java @@ -1,5 +1,5 @@ // -// $Id: LRUHashMap.java,v 1.4 2003/01/17 02:02:28 mdb Exp $ +// $Id: LRUHashMap.java,v 1.5 2003/04/27 07:37:32 mdb Exp $ package com.samskivert.util; @@ -63,11 +63,7 @@ public class LRUHashMap implements Map _maxSize = maxSize; // boot enough people to get below said size - while (_size > _maxSize && size() > 1) { - Object key = keySet().iterator().next(); - remove(key); -// System.out.println("Flushed " + key + ": " + _size); - } + flush(); } /** @@ -78,6 +74,20 @@ public class LRUHashMap implements Map return _maxSize; } + /** + * Used to temporarily disable flushing elements from the + * cache. Generally this is only used to avoid undesired garbage + * collection until such time as it is acceptable. Beware the risks of + * leaving flushing disabled for too long. + */ + public void setCanFlush (boolean canFlush) + { + if (_canFlush = canFlush) { + // if we just reenabled flushing, flush + flush(); + } + } + /** * Turn performance tracking on/off. */ @@ -167,16 +177,34 @@ public class LRUHashMap implements Map } // System.out.println("Added " + value + ": " + _size); + // flush if needed + flush(); + + return result; + } + + /** + * Flushes entries from the cache until we're back under our desired + * cache size. + */ + protected void flush () + { + if (!_canFlush) { + return; + } + // if we've exceeded our size, remove things until we're back // under the required size; but don't freak out if we have one // *really* big item +// int removed = 0; while (_size > _maxSize && size() > 1) { - key = keySet().iterator().next(); + Object key = keySet().iterator().next(); remove(key); -// System.out.println("Removed " + key + ": " + _size); +// removed += _sizer.computeSize(remove(key)); } - - return result; +// if (removed > 0) { +// System.out.println("Removed " + removed + ": " + _size); +// } } // documentation inherited from interface @@ -252,6 +280,9 @@ public class LRUHashMap implements Map /** The current size of this cache. */ protected int _size; + /** Used to temporarily disable flushing. */ + protected boolean _canFlush = true; + /** Used to compute the size of items in this cache. */ protected ItemSizer _sizer; /** Tracking info. */