Updated cache behavior
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/admin/accounting-manager@134089 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
a61b16d7fc
commit
1519b868ee
|
@ -1,15 +1,19 @@
|
|||
package org.gcube.portlets.admin.accountingmanager.server.amservice.cache;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.cache.Cache;
|
||||
import javax.cache.CacheManager;
|
||||
import javax.cache.Caching;
|
||||
import javax.cache.configuration.MutableConfiguration;
|
||||
import javax.cache.expiry.AccessedExpiryPolicy;
|
||||
import javax.cache.expiry.Duration;
|
||||
import javax.cache.spi.CachingProvider;
|
||||
|
||||
import org.ehcache.config.CacheConfiguration;
|
||||
import org.ehcache.config.builders.CacheConfigurationBuilder;
|
||||
import org.ehcache.config.builders.ResourcePoolsBuilder;
|
||||
import org.ehcache.config.units.MemoryUnit;
|
||||
import org.ehcache.expiry.Expirations;
|
||||
import org.ehcache.jsr107.Eh107Configuration;
|
||||
import org.gcube.portlets.admin.accountingmanager.shared.data.response.SeriesResponse;
|
||||
import org.gcube.portlets.admin.accountingmanager.shared.exception.ServiceException;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -29,6 +33,9 @@ public class AccountingCache implements Serializable {
|
|||
|
||||
private static final String ACCOUNTING_CACHE = "AccountingCache";
|
||||
private CacheManager cacheManager;
|
||||
private CacheConfiguration<String, SeriesResponse> cacheConfiguration;
|
||||
|
||||
//private MBeanServer mBeanServer;
|
||||
|
||||
public AccountingCache() throws ServiceException {
|
||||
super();
|
||||
|
@ -37,6 +44,35 @@ public class AccountingCache implements Serializable {
|
|||
// Resolve a cache manager
|
||||
CachingProvider cachingProvider = Caching.getCachingProvider();
|
||||
cacheManager = cachingProvider.getCacheManager();
|
||||
logger.info("AccountingCache URI: " + cacheManager.getURI());
|
||||
|
||||
ResourcePoolsBuilder builder = ResourcePoolsBuilder
|
||||
.newResourcePoolsBuilder().heap(2L, MemoryUnit.KB);
|
||||
//.offheap(1L, MemoryUnit.MB);
|
||||
|
||||
//.disk(500L, MemoryUnit.KB)
|
||||
|
||||
// .withSizeOfMaxObjectSize(1L, MemoryUnit.KB)
|
||||
cacheConfiguration = CacheConfigurationBuilder
|
||||
.newCacheConfigurationBuilder(String.class,
|
||||
SeriesResponse.class, builder)
|
||||
.withExpiry(
|
||||
Expirations
|
||||
.timeToLiveExpiration(org.ehcache.expiry.Duration
|
||||
.of(30L, TimeUnit.MINUTES)))
|
||||
.build();
|
||||
|
||||
|
||||
|
||||
//mBeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||
|
||||
|
||||
/*
|
||||
* PersistentCacheManager myCacheManager =
|
||||
* CacheManagerBuilder.newCacheManagerBuilder()
|
||||
* .with(CacheManagerBuilder.persistence("AccountingCache"))
|
||||
* .build(true);
|
||||
*/
|
||||
|
||||
} catch (Throwable e) {
|
||||
logger.error(e.getLocalizedMessage(), e);
|
||||
|
@ -50,40 +86,38 @@ public class AccountingCache implements Serializable {
|
|||
try {
|
||||
String cacheName = ACCOUNTING_CACHE + scope;
|
||||
|
||||
|
||||
// create the cache
|
||||
Cache<String, SeriesResponse> cache = cacheManager
|
||||
.getCache(cacheName, String.class, SeriesResponse.class);
|
||||
Cache<String, SeriesResponse> cache = cacheManager.getCache(
|
||||
cacheName, String.class, SeriesResponse.class);
|
||||
|
||||
if (cache == null) {
|
||||
// configure the cache MutableConfiguration<String, Integer>
|
||||
MutableConfiguration<String, SeriesResponse> config = new MutableConfiguration<String, SeriesResponse>()
|
||||
.setTypes(String.class, SeriesResponse.class)
|
||||
.setExpiryPolicyFactory(
|
||||
AccessedExpiryPolicy
|
||||
.factoryOf(Duration.THIRTY_MINUTES))
|
||||
.setStatisticsEnabled(true);
|
||||
/*
|
||||
* MutableConfiguration<String, SeriesResponse> config = new
|
||||
* MutableConfiguration<String, SeriesResponse>()
|
||||
* .setTypes(String.class, SeriesResponse.class)
|
||||
* .setExpiryPolicyFactory( CreatedExpiryPolicy
|
||||
* .factoryOf(Duration.THIRTY_MINUTES))
|
||||
* .setStatisticsEnabled(true);
|
||||
*
|
||||
* cache = cacheManager.createCache(cacheName, config);
|
||||
*/
|
||||
|
||||
cache = cacheManager.createCache(cacheName, Eh107Configuration
|
||||
.fromEhcacheCacheConfiguration(cacheConfiguration));
|
||||
cacheManager.enableManagement(cacheName, true);
|
||||
cacheManager.enableStatistics(cacheName, true);
|
||||
|
||||
cache = cacheManager.createCache(cacheName, config);
|
||||
|
||||
|
||||
/*ObjectName objectName = new ObjectName("javax.cache:type=CacheStatistics"
|
||||
+ " , CacheManager=" + (cache.getCacheManager().getURI().toString())
|
||||
+ " , Cache=" + cache.getName());
|
||||
|
||||
mBeanServer.registerMBean(cache, objectName);
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* ResourcePoolsBuilder builder = ResourcePoolsBuilder
|
||||
* .newResourcePoolsBuilder().heap(5L, MemoryUnit.MB);
|
||||
*
|
||||
* CacheConfiguration<String, Integer> cacheConfiguration =
|
||||
* CacheConfigurationBuilder
|
||||
* .newCacheConfigurationBuilder(String.class, Integer.class,
|
||||
* builder) .withSizeOfMaxObjectSize(2L, MemoryUnit.MB) .withExpiry(
|
||||
* Expirations .timeToLiveExpiration(org.ehcache.expiry.Duration
|
||||
* .of(1L, TimeUnit.HOURS))).build();
|
||||
*
|
||||
* cache = cacheManager.createCache(ACCOUNTING_CACHE,
|
||||
* Eh107Configuration
|
||||
* .fromEhcacheCacheConfiguration(cacheConfiguration));
|
||||
*/
|
||||
return cache;
|
||||
|
||||
} catch (Exception e) {
|
||||
|
@ -98,20 +132,21 @@ public class AccountingCache implements Serializable {
|
|||
throws ServiceException {
|
||||
Cache<String, SeriesResponse> cache = initCache(scope);
|
||||
cache.put(key, value);
|
||||
logger.debug("Cached: ["+scope+", " + key + ", " + value + "]");
|
||||
logger.debug("Cached: [" + scope + ", " + key + ", " + value + "]");
|
||||
|
||||
}
|
||||
|
||||
public SeriesResponse get(String scope, String key) throws ServiceException {
|
||||
Cache<String, SeriesResponse> cache = initCache(scope);
|
||||
SeriesResponse value = cache.get(key);
|
||||
logger.debug("Cached value: ["+scope+", " + value+"]");
|
||||
logger.debug("Cached value: [" + scope + ", " + value + "]");
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void finalize() throws Throwable {
|
||||
super.finalize();
|
||||
logger.debug("Release the cache resources");
|
||||
if (cacheManager != null) {
|
||||
try {
|
||||
|
@ -132,6 +167,7 @@ public class AccountingCache implements Serializable {
|
|||
+ e.getLocalizedMessage(), e);
|
||||
}
|
||||
}
|
||||
super.finalize();
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -56,8 +56,7 @@ public class StorageUtil {
|
|||
"Attention no public link for this item!");
|
||||
}
|
||||
|
||||
} catch (WorkspaceFolderNotFoundException | InternalErrorException
|
||||
| HomeNotFoundException | ItemNotFoundException e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new ServiceException(e.getLocalizedMessage());
|
||||
|
||||
|
|
|
@ -22,23 +22,47 @@ public class TestAccountingManangerCache extends TestCase {
|
|||
private static Logger logger = LoggerFactory
|
||||
.getLogger(TestAccountingManangerCache.class);
|
||||
|
||||
|
||||
public void testAccountingManagerResource() {
|
||||
public void testAccountingManagerCache() {
|
||||
if (Constants.TEST_ENABLE) {
|
||||
logger.debug("Test Enabled");
|
||||
|
||||
try {
|
||||
AccountingCache accountingCache=new AccountingCache();
|
||||
SeriesResponse s=new SeriesResponse();
|
||||
accountingCache.put("scope","key", s);
|
||||
AccountingCache accountingCache = new AccountingCache();
|
||||
SeriesResponse s = new SeriesResponse();
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
accountingCache.put("scope", "key" + i, s);
|
||||
}
|
||||
Thread.sleep(180000);
|
||||
|
||||
accountingCache.finalize();
|
||||
assertTrue(true);
|
||||
|
||||
|
||||
} catch (Throwable e) {
|
||||
logger.error(e.getLocalizedMessage(),e);
|
||||
logger.error(e.getLocalizedMessage(), e);
|
||||
assertTrue("Error in cache!", false);
|
||||
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
logger.debug("Test Disabled");
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void testAccountingManagerCacheRemoveAll() {
|
||||
if (Constants.TEST_ENABLE) {
|
||||
logger.debug("Test Enabled");
|
||||
|
||||
try {
|
||||
AccountingCache accountingCache = new AccountingCache();
|
||||
accountingCache.finalize();
|
||||
assertTrue(true);
|
||||
|
||||
} catch (Throwable e) {
|
||||
logger.error(e.getLocalizedMessage(), e);
|
||||
assertTrue("Error in cache!", false);
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
@ -47,6 +71,4 @@ public class TestAccountingManangerCache extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue