1 16 package org.outerj.daisy.cache.impl; 17 18 import org.outerj.daisy.cache.DocumentCache; 19 import org.outerj.daisy.repository.commonimpl.DocumentImpl; 20 import org.outerj.daisy.repository.AvailableVariants; 21 import org.apache.commons.collections.map.LRUMap; 22 import org.apache.commons.collections.map.MultiKeyMap; 23 import org.apache.avalon.framework.configuration.Configurable; 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.ConfigurationException; 26 import org.apache.avalon.framework.activity.Initializable; 27 import org.apache.avalon.framework.service.Serviceable; 28 import org.apache.avalon.framework.service.ServiceManager; 29 import org.apache.avalon.framework.service.ServiceException; 30 31 import javax.management.MBeanServer ; 32 import javax.management.ObjectName ; 33 34 41 public class DocumentCacheImpl implements DocumentCache, Configurable, Initializable, Serviceable, DocumentCacheImplMBean { 42 private MultiKeyMap documentCache; 43 private LRUMap documentCacheBacker; 44 private LRUMap availableVariantsCache; 45 private int documentCacheMaxSize; 46 private int availableVariantsCacheMaxSize; 47 private static final int DEFAULT_MAX_SIZE = 10000; 48 private ServiceManager serviceManager; 49 50 public void configure(Configuration configuration) throws ConfigurationException { 51 documentCacheMaxSize = configuration.getChild("documentCacheMaxSize").getValueAsInteger(DEFAULT_MAX_SIZE); 52 availableVariantsCacheMaxSize = configuration.getChild("availableVariantsCacheMaxSize").getValueAsInteger(DEFAULT_MAX_SIZE); 53 } 54 55 58 public void service(ServiceManager serviceManager) throws ServiceException { 59 this.serviceManager = serviceManager; 60 } 61 62 public void initialize() throws Exception { 63 documentCacheBacker = new LRUMap(documentCacheMaxSize); 64 documentCache = MultiKeyMap.decorate(documentCacheBacker); 65 availableVariantsCache = new LRUMap(availableVariantsCacheMaxSize); 66 67 MBeanServer mbeanServer = (MBeanServer )serviceManager.lookup("mbeanserver"); 68 try { 69 mbeanServer.registerMBean(this, new ObjectName ("Daisy:name=DocumentCache")); 70 } finally { 71 serviceManager.release(mbeanServer); 72 } 73 } 74 75 public void clear() { 76 documentCache.clear(); 77 availableVariantsCache.clear(); 78 } 79 80 public synchronized void put(long documentId, long branchId, long languageId, DocumentImpl document) { 81 documentCache.put(new Long (documentId), new Long (branchId), new Long (languageId), document); 82 } 83 84 public synchronized DocumentImpl get(long documentId, long branchId, long languageId) { 85 return (DocumentImpl)documentCache.get(new Long (documentId), new Long (branchId), new Long (languageId)); 86 } 87 88 public synchronized void remove(long documentId) { 89 documentCache.removeAll(new Long (documentId)); 90 } 91 92 public synchronized void remove(long documentId, long branchId, long languageId) { 93 documentCache.remove(new Long (documentId), new Long (branchId), new Long (languageId)); 94 } 95 96 public synchronized void put(long documentId, AvailableVariants availableVariants) { 97 availableVariantsCache.put(new Long (documentId), availableVariants); 98 } 99 100 public synchronized AvailableVariants getAvailableVariants(long documentId) { 101 return (AvailableVariants)availableVariantsCache.get(new Long (documentId)); 102 } 103 104 public synchronized void removeAvailableVariants(long documentId) { 105 availableVariantsCache.remove(new Long (documentId)); 106 } 107 108 public synchronized int getDocumentCacheMaxSize() { 109 return documentCacheBacker.maxSize(); 110 } 111 112 public synchronized int getDocumentCacheCurrentSize() { 113 return documentCache.size(); 114 } 115 116 public int getAvailableVariantCacheMaxSize() { 117 return availableVariantsCache.maxSize(); 118 } 119 120 public int getAvailableVariantCacheCurrentSize() { 121 return availableVariantsCache.size(); 122 } 123 124 public synchronized void clearCache() { 125 documentCache.clear(); 126 } 127 } 128 | Popular Tags |