Changeset 10808 for extensions/jiwigo
- Timestamp:
- May 7, 2011, 3:42:06 PM (14 years ago)
- Location:
- extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/transverse
- Files:
-
- 2 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/transverse/ImagesManagement.java
r10697 r10808 3 3 import java.awt.image.BufferedImage; 4 4 import java.net.URL; 5 import java.util.ArrayList; 6 import java.util.Collections; 7 import java.util.Date; 5 8 import java.util.HashMap; 9 import java.util.Iterator; 6 10 import java.util.List; 11 import java.util.Map; 7 12 8 13 import javax.imageio.ImageIO; … … 64 69 * cache allows to keep the images in the ram 65 70 */ 66 private HashMap<Integer, BufferedImage> imageCache = new HashMap<Integer, BufferedImage>();71 private HashMap<Integer, CacheObjectManagement> imageCache = new HashMap<Integer, CacheObjectManagement>(); 67 72 68 73 /** 69 74 * cache allows to keep the thumbnails in the ram 70 75 */ 71 private HashMap<Integer, BufferedImage> thumbnailsCache = new HashMap<Integer, BufferedImage>();76 private HashMap<Integer, CacheObjectManagement> thumbnailsCache = new HashMap<Integer, CacheObjectManagement>(); 72 77 73 78 /** … … 96 101 */ 97 102 private Integer currentCategory = -1; 103 104 private static Integer IMAGE_CACHE_SIZE = 20; 98 105 99 106 /** … … 170 177 171 178 /** 179 * Method that allows to keep a maximum number of images in the cache 180 * @param cache 181 * @param maxSize 182 */ 183 private void checkCache(HashMap<Integer, CacheObjectManagement> cache, Integer maxSize) { 184 if (cache.size() == maxSize) { 185 Integer objectsToRemove = (maxSize / 4); 186 if (LOG.isDebugEnabled()) { 187 LOG.debug("Cache has reached max size cleaning " + objectsToRemove + " objects"); 188 } 189 ArrayList as = new ArrayList(cache.entrySet()); 190 191 Collections.sort(as, new CacheObjectManagementComparator()); 192 Iterator i = as.iterator(); 193 for (int j = 0; j < objectsToRemove; j++) { 194 Integer key = (Integer) ((Map.Entry) i.next()).getKey(); 195 if (LOG.isDebugEnabled()) { 196 CacheObjectManagement cacheObject = cache.get(key); 197 String debugStr = ""; 198 for (Integer id : cache.keySet()) { 199 debugStr += "\t" + id + " : " + cache.get(id).getLastAccessDate() + "\n"; 200 } 201 LOG.debug("Available object : \n" + debugStr); 202 LOG.debug("Removed object : " + key + " : " + cacheObject.getLastAccessDate()); 203 } 204 205 CacheObjectManagement object = cache.remove(key); 206 object = null; 207 } 208 if (LOG.isDebugEnabled()) { 209 String debugStr = ""; 210 for (Integer id : cache.keySet()) { 211 debugStr += "\t" + id + " : " + cache.get(id).getLastAccessDate() + "\n"; 212 } 213 LOG.debug("Cache size is now " + cache.size() + ". Cache now contains : \n" + debugStr); 214 } 215 } 216 } 217 218 /** 172 219 * Function that allows to load images once 173 220 * to decrease response delays … … 177 224 if (imageCache.get(currentImage.getIdentifier()) == null) { 178 225 try { 226 checkCache(imageCache, IMAGE_CACHE_SIZE); 179 227 BufferedImage img = ImageIO.read(new URL(currentImage.getUrl())); 180 imageCache.put(currentImage.getIdentifier(), img); 228 CacheObjectManagement cacheObject = new CacheObjectManagement(img, new Date()); 229 imageCache.put(currentImage.getIdentifier(), cacheObject); 230 if (LOG.isDebugEnabled()) { 231 LOG.debug("Adding image to cache : " + currentImage.getIdentifier() + " : " 232 + cacheObject.getLastAccessDate()); 233 } 181 234 } catch (Exception e) { 182 235 LOG.error(Tools.getStackTrace(e)); 183 236 } 184 237 } 185 return imageCache.get(currentImage.getIdentifier()); 238 CacheObjectManagement cacheObject = imageCache.get(currentImage.getIdentifier()); 239 cacheObject.setLastAccessDate(new Date()); 240 return cacheObject.getImage(); 186 241 } 187 242 … … 195 250 try { 196 251 BufferedImage img = ImageIO.read(new URL(image.getThumbnailUrl())); 197 thumbnailsCache.put(image.getIdentifier(), img); 252 CacheObjectManagement cacheObject = new CacheObjectManagement(img, new Date()); 253 thumbnailsCache.put(image.getIdentifier(), cacheObject); 198 254 } catch (Exception e) { 199 255 LOG.error(Tools.getStackTrace(e)); 200 256 } 201 257 } 202 203 return thumbnailsCache.get(image.getIdentifier()); 258 CacheObjectManagement cacheObject = thumbnailsCache.get(image.getIdentifier()); 259 cacheObject.setLastAccessDate(new Date()); 260 return cacheObject.getImage(); 204 261 } 205 262
Note: See TracChangeset
for help on using the changeset viewer.