1 23 24 package org.infoglue.deliver.util; 25 26 import com.opensymphony.oscache.base.NeedsRefreshException; 27 import com.opensymphony.oscache.base.events.CacheEntryEvent; 28 import com.opensymphony.oscache.base.events.CachewideEvent; 29 import com.opensymphony.oscache.extra.CacheEntryEventListenerImpl; 30 31 public class ExtendedCacheEntryEventListenerImpl extends CacheEntryEventListenerImpl 32 { 33 private int totalSize = 0; 34 35 private int getOldSize(CacheEntryEvent event) 36 { 37 try 38 { 39 return event.getMap().getFromCache(event.getKey()).toString().length(); 40 } 41 catch (NeedsRefreshException e) 42 { 43 event.getMap().cancelUpdate(event.getKey()); 44 return 0; 45 } 46 } 47 48 53 public void cacheEntryAdded(CacheEntryEvent event) { 54 super.cacheEntryAdded(event); 55 if(event.getEntry().getContent() instanceof byte[]) 56 totalSize = totalSize + ((byte[])event.getEntry().getContent()).length * 8; 57 else 58 totalSize = totalSize + event.getEntry().getContent().toString().length() * 8; 59 } 60 61 66 public void cacheEntryFlushed(CacheEntryEvent event) { 67 super.cacheEntryFlushed(event); 68 totalSize = totalSize - getOldSize(event); 69 } 70 71 76 public void cacheEntryRemoved(CacheEntryEvent event) { 77 super.cacheEntryRemoved(event); 78 totalSize = totalSize - getOldSize(event); 79 } 80 81 86 public void cacheEntryUpdated(CacheEntryEvent event) { 87 super.cacheEntryRemoved(event); 88 } 90 91 96 public void cacheFlushed(CachewideEvent event) { 97 super.cacheFlushed(event); 98 totalSize = 0; 99 } 100 101 104 public String toString() { 105 return ("Added " + getEntryAddedCount() + ", Approximate size " + this.totalSize + ", Cache Flushed " + getCacheFlushedCount()); 106 } 107 } | Popular Tags |