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;
|
package org.gcube.portlets.admin.accountingmanager.server.amservice.cache;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import javax.cache.Cache;
|
import javax.cache.Cache;
|
||||||
import javax.cache.CacheManager;
|
import javax.cache.CacheManager;
|
||||||
import javax.cache.Caching;
|
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 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.data.response.SeriesResponse;
|
||||||
import org.gcube.portlets.admin.accountingmanager.shared.exception.ServiceException;
|
import org.gcube.portlets.admin.accountingmanager.shared.exception.ServiceException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -29,6 +33,9 @@ public class AccountingCache implements Serializable {
|
||||||
|
|
||||||
private static final String ACCOUNTING_CACHE = "AccountingCache";
|
private static final String ACCOUNTING_CACHE = "AccountingCache";
|
||||||
private CacheManager cacheManager;
|
private CacheManager cacheManager;
|
||||||
|
private CacheConfiguration<String, SeriesResponse> cacheConfiguration;
|
||||||
|
|
||||||
|
//private MBeanServer mBeanServer;
|
||||||
|
|
||||||
public AccountingCache() throws ServiceException {
|
public AccountingCache() throws ServiceException {
|
||||||
super();
|
super();
|
||||||
|
@ -37,6 +44,35 @@ public class AccountingCache implements Serializable {
|
||||||
// Resolve a cache manager
|
// Resolve a cache manager
|
||||||
CachingProvider cachingProvider = Caching.getCachingProvider();
|
CachingProvider cachingProvider = Caching.getCachingProvider();
|
||||||
cacheManager = cachingProvider.getCacheManager();
|
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) {
|
} catch (Throwable e) {
|
||||||
logger.error(e.getLocalizedMessage(), e);
|
logger.error(e.getLocalizedMessage(), e);
|
||||||
|
@ -50,40 +86,38 @@ public class AccountingCache implements Serializable {
|
||||||
try {
|
try {
|
||||||
String cacheName = ACCOUNTING_CACHE + scope;
|
String cacheName = ACCOUNTING_CACHE + scope;
|
||||||
|
|
||||||
|
|
||||||
// create the cache
|
// create the cache
|
||||||
Cache<String, SeriesResponse> cache = cacheManager
|
Cache<String, SeriesResponse> cache = cacheManager.getCache(
|
||||||
.getCache(cacheName, String.class, SeriesResponse.class);
|
cacheName, String.class, SeriesResponse.class);
|
||||||
|
|
||||||
if (cache == null) {
|
if (cache == null) {
|
||||||
// configure the cache MutableConfiguration<String, Integer>
|
/*
|
||||||
MutableConfiguration<String, SeriesResponse> config = new MutableConfiguration<String, SeriesResponse>()
|
* MutableConfiguration<String, SeriesResponse> config = new
|
||||||
.setTypes(String.class, SeriesResponse.class)
|
* MutableConfiguration<String, SeriesResponse>()
|
||||||
.setExpiryPolicyFactory(
|
* .setTypes(String.class, SeriesResponse.class)
|
||||||
AccessedExpiryPolicy
|
* .setExpiryPolicyFactory( CreatedExpiryPolicy
|
||||||
.factoryOf(Duration.THIRTY_MINUTES))
|
* .factoryOf(Duration.THIRTY_MINUTES))
|
||||||
.setStatisticsEnabled(true);
|
* .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;
|
return cache;
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -98,20 +132,21 @@ public class AccountingCache implements Serializable {
|
||||||
throws ServiceException {
|
throws ServiceException {
|
||||||
Cache<String, SeriesResponse> cache = initCache(scope);
|
Cache<String, SeriesResponse> cache = initCache(scope);
|
||||||
cache.put(key, value);
|
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 {
|
public SeriesResponse get(String scope, String key) throws ServiceException {
|
||||||
Cache<String, SeriesResponse> cache = initCache(scope);
|
Cache<String, SeriesResponse> cache = initCache(scope);
|
||||||
SeriesResponse value = cache.get(key);
|
SeriesResponse value = cache.get(key);
|
||||||
logger.debug("Cached value: ["+scope+", " + value+"]");
|
logger.debug("Cached value: [" + scope + ", " + value + "]");
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void finalize() throws Throwable {
|
public void finalize() throws Throwable {
|
||||||
super.finalize();
|
|
||||||
logger.debug("Release the cache resources");
|
logger.debug("Release the cache resources");
|
||||||
if (cacheManager != null) {
|
if (cacheManager != null) {
|
||||||
try {
|
try {
|
||||||
|
@ -132,6 +167,7 @@ public class AccountingCache implements Serializable {
|
||||||
+ e.getLocalizedMessage(), e);
|
+ e.getLocalizedMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
super.finalize();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,8 +56,7 @@ public class StorageUtil {
|
||||||
"Attention no public link for this item!");
|
"Attention no public link for this item!");
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (WorkspaceFolderNotFoundException | InternalErrorException
|
} catch (Exception e) {
|
||||||
| HomeNotFoundException | ItemNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
throw new ServiceException(e.getLocalizedMessage());
|
throw new ServiceException(e.getLocalizedMessage());
|
||||||
|
|
||||||
|
|
|
@ -22,21 +22,23 @@ public class TestAccountingManangerCache extends TestCase {
|
||||||
private static Logger logger = LoggerFactory
|
private static Logger logger = LoggerFactory
|
||||||
.getLogger(TestAccountingManangerCache.class);
|
.getLogger(TestAccountingManangerCache.class);
|
||||||
|
|
||||||
|
public void testAccountingManagerCache() {
|
||||||
public void testAccountingManagerResource() {
|
|
||||||
if (Constants.TEST_ENABLE) {
|
if (Constants.TEST_ENABLE) {
|
||||||
logger.debug("Test Enabled");
|
logger.debug("Test Enabled");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
AccountingCache accountingCache=new AccountingCache();
|
AccountingCache accountingCache = new AccountingCache();
|
||||||
SeriesResponse s=new SeriesResponse();
|
SeriesResponse s = new SeriesResponse();
|
||||||
accountingCache.put("scope","key", s);
|
for (int i = 0; i < 1000000; i++) {
|
||||||
|
accountingCache.put("scope", "key" + i, s);
|
||||||
|
}
|
||||||
|
Thread.sleep(180000);
|
||||||
|
|
||||||
accountingCache.finalize();
|
accountingCache.finalize();
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
|
|
||||||
|
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
logger.error(e.getLocalizedMessage(),e);
|
logger.error(e.getLocalizedMessage(), e);
|
||||||
assertTrue("Error in cache!", false);
|
assertTrue("Error in cache!", false);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -48,5 +50,25 @@ public class TestAccountingManangerCache extends TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 {
|
||||||
|
logger.debug("Test Disabled");
|
||||||
|
assertTrue(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
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