1 18 package org.apache.activemq.memory; 19 20 import java.util.concurrent.atomic.AtomicLong ; 21 22 28 public class UsageManagerCacheFilter extends CacheFilter { 29 30 private final AtomicLong totalUsage = new AtomicLong (0); 31 private final UsageManager um; 32 33 public UsageManagerCacheFilter(Cache next, UsageManager um) { 34 super(next); 35 this.um = um; 36 } 37 38 public Object put(Object key, Object value) { 39 long usage = getUsageOfAddedObject(value); 40 Object rc = super.put(key, value); 41 if( rc !=null ) { 42 usage -= getUsageOfRemovedObject(rc); 43 } 44 totalUsage.addAndGet(usage); 45 um.increaseUsage(usage); 46 return rc; 47 } 48 49 public Object remove(Object key) { 50 Object rc = super.remove(key); 51 if( rc !=null ) { 52 long usage = getUsageOfRemovedObject(rc); 53 totalUsage.addAndGet(-usage); 54 um.decreaseUsage(usage); 55 } 56 return rc; 57 } 58 59 60 protected long getUsageOfAddedObject(Object value) { 61 return 1; 62 } 63 64 protected long getUsageOfRemovedObject(Object value) { 65 return 1; 66 } 67 68 public void close() { 69 um.decreaseUsage(totalUsage.get()); 70 } 71 } 72 | Popular Tags |