use of org.pentaho.platform.api.engine.ICacheManager in project pentaho-platform by pentaho.
the class PentahoSystem method refreshSettings.
public static void refreshSettings() {
PentahoSystem.systemSettingsService.resetSettingsCache();
ICacheManager cacheManager = getCacheManager(null);
if (cacheManager != null) {
cacheManager.removeFromGlobalCache(WAIT_SECONDS);
}
}
use of org.pentaho.platform.api.engine.ICacheManager in project pentaho-platform by pentaho.
the class SessionCachingMetadataDomainRepositoryTest method shouldUseDomainIdsCacheIfEnabled.
@Test
public void shouldUseDomainIdsCacheIfEnabled() throws Exception {
MockSessionAwareMetadataDomainRepository mock = spy(new MockSessionAwareMetadataDomainRepository());
PentahoSessionHolder.setSession(new StandaloneSession("session", "1"));
SessionCachingMetadataDomainRepository repo = new SessionCachingMetadataDomainRepository(mock);
Domain domain = new Domain();
domain.setId("id");
mock.setPersistedDomains(domain);
ICacheManager manager = mock(ICacheManager.class);
Set<String> ids = new HashSet<>(Arrays.asList("domainId1", "domainId2"));
when(manager.getFromRegionCache("metadata-domain-repository", "domain-id-cache-for-session:1")).thenReturn(ids);
repo.cacheManager = manager;
repo.domainIdsCacheEnabled = true;
Set<String> domainIds = repo.getDomainIds();
assertEquals(ids, domainIds);
verify(mock, times(0)).reloadDomains();
verify(manager, times(1)).getFromRegionCache("metadata-domain-repository", "domain-id-cache-for-session:1");
}
use of org.pentaho.platform.api.engine.ICacheManager in project pentaho-platform by pentaho.
the class SessionCachingMetadataDomainRepositoryTest method shouldNotUseDomainIdsCacheIfDisabled.
@Test
public void shouldNotUseDomainIdsCacheIfDisabled() throws Exception {
MockSessionAwareMetadataDomainRepository mock = spy(new MockSessionAwareMetadataDomainRepository());
PentahoSessionHolder.setSession(new StandaloneSession("session", "1"));
SessionCachingMetadataDomainRepository repo = new SessionCachingMetadataDomainRepository(mock);
Domain domain = new Domain();
domain.setId("id");
mock.setPersistedDomains(domain);
ICacheManager manager = mock(ICacheManager.class);
repo.cacheManager = manager;
repo.domainIdsCacheEnabled = false;
Set<String> domainIds = repo.getDomainIds();
assertEquals(1, domainIds.size());
assertTrue(domainIds.contains("id"));
verify(mock, times(1)).getDomainIds();
verify(mock, times(1)).reloadDomains();
verify(manager, times(0)).getFromRegionCache("metadata-domain-repository", "domain-id-cache-for-session:1");
verify(manager, times(0)).addCacheRegion("domain-id-cache-for-session:1");
}
use of org.pentaho.platform.api.engine.ICacheManager in project pentaho-platform by pentaho.
the class CacheManagerIT method testCache.
// private final StringBuffer longString = new StringBuffer();
public void testCache() {
// Make sure we have a cache first...
// TODO sbarkdull, need to get real session in
ICacheManager cacheManager = PentahoSystem.getCacheManager(null);
// here
Assert.assertNotNull(cacheManager);
Assert.assertTrue(cacheManager.cacheEnabled());
// Test Session Based Caching
// $NON-NLS-1$ //$NON-NLS-2$
StandaloneSession userSession1 = new StandaloneSession("Standalone Session", "1234-5678-90");
// $NON-NLS-1$ //$NON-NLS-2$
StandaloneSession userSession2 = new StandaloneSession("Standalone Session", "abc-def-ghi-jkl");
// ================================ Create Objects
// User Objects
// Cache any-old String...
// $NON-NLS-1$
String user1StringObject = "User1's String Object";
// Make sure we can cache these Document objects...
Document user1Document = DocumentHelper.createDocument();
// $NON-NLS-1$
Element user1RootNode = user1Document.addElement("user1");
// $NON-NLS-1$
Element user1FileNode = user1RootNode.addElement("file");
// $NON-NLS-1$ //$NON-NLS-2$
user1FileNode.addAttribute("name", "test");
String user1CompareXMLOriginal = user1Document.asXML();
// User2's Objects
// Cache any-old String...
// $NON-NLS-1$
String user2StringObject = "User2's String Object";
Document user2Document = DocumentHelper.createDocument();
// $NON-NLS-1$
Element user2RootNode = user2Document.addElement("user2");
// $NON-NLS-1$
Element user2FileNode = user2RootNode.addElement("folder");
// $NON-NLS-1$ //$NON-NLS-2$
user2FileNode.addAttribute("name", "test2");
String user2CompareXMLOriginal = user2Document.asXML();
// Global Objects
Integer globalInt = new Integer(372);
// $NON-NLS-1$
BigDecimal globalBigDecimal = new BigDecimal("2342.123334444211");
StringBuffer globalStringBuffer = new StringBuffer();
// $NON-NLS-1$
globalStringBuffer.append("This is a really long string to stick in a string buffer");
// Ok - we now have some stuff to jam into the cache.
// $NON-NLS-1$
cacheManager.putInSessionCache(userSession1, "StringObject", user1StringObject);
// $NON-NLS-1$
cacheManager.putInSessionCache(userSession1, "repoDoc", user1Document);
// $NON-NLS-1$
cacheManager.putInSessionCache(userSession2, "StringObject", user2StringObject);
// $NON-NLS-1$
cacheManager.putInSessionCache(userSession2, "repoDoc", user2Document);
// Get them back out
// $NON-NLS-1$
Object user1CachedStringObject = cacheManager.getFromSessionCache(userSession1, "StringObject");
Assert.assertEquals(user1StringObject, (String) user1CachedStringObject);
// $NON-NLS-1$
Object user1CachedDocument = cacheManager.getFromSessionCache(userSession1, "repoDoc");
String user1CompareXMLCached = ((Document) user1CachedDocument).asXML();
Assert.assertEquals(user1CompareXMLOriginal, user1CompareXMLCached);
// $NON-NLS-1$
Object user2CachedStringObject = cacheManager.getFromSessionCache(userSession2, "StringObject");
Assert.assertEquals(user2StringObject, (String) user2CachedStringObject);
// $NON-NLS-1$
Object user2CachedDocument = cacheManager.getFromSessionCache(userSession2, "repoDoc");
String user2CompareXMLCached = ((Document) user2CachedDocument).asXML();
Assert.assertEquals(user2CompareXMLOriginal, user2CompareXMLCached);
// OK - We've verified that their objects are unique to each individual
// user.
// Test Removals from session only
// Remove a single user-session based object.
// $NON-NLS-1$
cacheManager.removeFromSessionCache(userSession1, "StringObject");
// Try to get it back anyway.
// $NON-NLS-1$
Object notThere = cacheManager.getFromSessionCache(userSession1, "StringObject");
Assert.assertNull(notThere);
// Make sure that User2 is unaffected
// $NON-NLS-1$
Object shouldBeThere = cacheManager.getFromSessionCache(userSession2, "StringObject");
Assert.assertNotNull(shouldBeThere);
// Kill user1's session
cacheManager.killSessionCache(userSession1);
// $NON-NLS-1$
notThere = cacheManager.getFromSessionCache(userSession1, "repoDoc");
Assert.assertNull(notThere);
// Make sure that User2 is still unaffected
// $NON-NLS-1$
shouldBeThere = cacheManager.getFromSessionCache(userSession2, "StringObject");
Assert.assertNotNull(shouldBeThere);
// Test Global Caching
// Put stuff in
// $NON-NLS-1$
cacheManager.putInGlobalCache("globalIntegerKey", globalInt);
// $NON-NLS-1$
cacheManager.putInGlobalCache("globalBigDecimalKey", globalBigDecimal);
// $NON-NLS-1$
cacheManager.putInGlobalCache("globalStringBufferKey", globalStringBuffer);
// $NON-NLS-1$
Object cachedGlobalInt = cacheManager.getFromGlobalCache("globalIntegerKey");
Assert.assertEquals(globalInt, cachedGlobalInt);
// $NON-NLS-1$
Object cachedGlobalBigDecimal = cacheManager.getFromGlobalCache("globalBigDecimalKey");
Assert.assertEquals(globalBigDecimal, cachedGlobalBigDecimal);
// $NON-NLS-1$
Object cachedGlobalStringBuffer = cacheManager.getFromGlobalCache("globalStringBufferKey");
Assert.assertEquals(globalStringBuffer, cachedGlobalStringBuffer);
// Test clear all session-based keys. This should leave the global stuff
// alone.
cacheManager.killSessionCaches();
// $NON-NLS-1$
notThere = cacheManager.getFromSessionCache(userSession2, "StringObject");
Assert.assertNull(notThere);
// $NON-NLS-1$
notThere = cacheManager.getFromSessionCache(userSession2, "repoDoc");
Assert.assertNull(notThere);
// $NON-NLS-1$
shouldBeThere = cacheManager.getFromGlobalCache("globalIntegerKey");
Assert.assertNotNull(shouldBeThere);
// Totally clear out the cache.
cacheManager.clearCache();
// $NON-NLS-1$
notThere = cacheManager.getFromGlobalCache("globalIntegerKey");
Assert.assertNull(notThere);
// $NON-NLS-1$
notThere = cacheManager.getFromGlobalCache("globalBigDecimalKey");
Assert.assertNull(notThere);
// $NON-NLS-1$
notThere = cacheManager.getFromGlobalCache("globalStringBufferKey");
Assert.assertNull(notThere);
cacheManager.addCacheRegion(ICacheManager.GLOBAL);
// Assumes cache size is set to 2000 objects maximum.
for (int i = 0; i < 10000; i++) {
// $NON-NLS-1$
String someCachedString = "This is the string to cache " + i;
// $NON-NLS-1$
String someCachedKey = "SomeCachedKey" + i;
if ((i % 1000) == 0) {
sleep(5);
}
cacheManager.putInGlobalCache(someCachedKey, someCachedString);
}
// Let cache stabalize, and decide what hasn't been used for a while.
// 15 seconds should do it.
sleep(15);
// Get first item from the cache...
// $NON-NLS-1$
shouldBeThere = cacheManager.getFromGlobalCache("SomeCachedKey1");
// $NON-NLS-1$
Assert.assertEquals(shouldBeThere, "This is the string to cache 1");
// Get middle item from the cache...
// $NON-NLS-1$
shouldBeThere = cacheManager.getFromGlobalCache("SomeCachedKey5000");
// $NON-NLS-1$
Assert.assertEquals(shouldBeThere, "This is the string to cache 5000");
// Get last item from the cache...
// $NON-NLS-1$
shouldBeThere = cacheManager.getFromGlobalCache("SomeCachedKey999");
// $NON-NLS-1$
Assert.assertEquals(shouldBeThere, "This is the string to cache 999");
// Clear cache again...
cacheManager.clearCache();
// Make sure...
// $NON-NLS-1$
notThere = cacheManager.getFromGlobalCache("SomeCachedKey2");
Assert.assertNull(notThere);
// $NON-NLS-1$
notThere = cacheManager.getFromGlobalCache("SomeCachedKey5002");
Assert.assertNull(notThere);
// $NON-NLS-1$
notThere = cacheManager.getFromGlobalCache("SomeCachedKey998");
Assert.assertNull(notThere);
// Done with tests.
}
use of org.pentaho.platform.api.engine.ICacheManager in project pentaho-platform by pentaho.
the class CacheManagerWithRegionIT method testCacheRegion.
// private final StringBuffer longString = new StringBuffer();
public void testCacheRegion() {
// Make sure we have a cache first...
// TODO sbarkdull, need to get real session in
ICacheManager cacheManager = PentahoSystem.getCacheManager(null);
// here
Assert.assertNotNull(cacheManager);
Assert.assertTrue(cacheManager.cacheEnabled());
// Test Session Based Caching
// $NON-NLS-1$ //$NON-NLS-2$
StandaloneSession userSession1 = new StandaloneSession("Standalone Session", "1234-5678-90");
// $NON-NLS-1$ //$NON-NLS-2$
StandaloneSession userSession2 = new StandaloneSession("Standalone Session", "abc-def-ghi-jkl");
// ================================ Create Objects
// User Objects
// Cache any-old String...
// $NON-NLS-1$
String user1StringObject = "User1's String Object";
// Make sure we can cache these Document objects...
Document user1Document = DocumentHelper.createDocument();
// $NON-NLS-1$
Element user1RootNode = user1Document.addElement("user1");
// $NON-NLS-1$
Element user1FileNode = user1RootNode.addElement("file");
// $NON-NLS-1$ //$NON-NLS-2$
user1FileNode.addAttribute("name", "test");
String user1CompareXMLOriginal = user1Document.asXML();
// User2's Objects
// Cache any-old String...
// $NON-NLS-1$
String user2StringObject = "User2's String Object";
Document user2Document = DocumentHelper.createDocument();
// $NON-NLS-1$
Element user2RootNode = user2Document.addElement("user2");
// $NON-NLS-1$
Element user2FileNode = user2RootNode.addElement("folder");
// $NON-NLS-1$ //$NON-NLS-2$
user2FileNode.addAttribute("name", "test2");
String user2CompareXMLOriginal = user2Document.asXML();
// Global Objects
Integer globalInt = new Integer(372);
// $NON-NLS-1$
BigDecimal globalBigDecimal = new BigDecimal("2342.123334444211");
StringBuffer globalStringBuffer = new StringBuffer();
// $NON-NLS-1$
globalStringBuffer.append("This is a really long string to stick in a string buffer");
cacheManager.putInRegionCache(userSession1.getId(), "StringObject", user1StringObject);
// $NON-NLS-1$
cacheManager.putInRegionCache(userSession1.getId(), "repoDoc", user1Document);
// $NON-NLS-1$
cacheManager.putInRegionCache(userSession2.getId(), "StringObject", user2StringObject);
// $NON-N
cacheManager.putInRegionCache(userSession2.getId(), "repoDoc", user2Document);
// Get them back out
// $NON-NLS-1$
Object user1CachedStringObject = cacheManager.getFromRegionCache(userSession1.getId(), "StringObject");
Assert.assertNull(user1CachedStringObject);
// $NON-NLS-1$
Object user1CachedDocument = cacheManager.getFromRegionCache(userSession1.getId(), "repoDoc");
Assert.assertNull(user1CachedDocument);
// $NON-NLS-1$
Object user2CachedStringObject = cacheManager.getFromRegionCache(userSession2.getId(), "StringObject");
Assert.assertNull(user2CachedStringObject);
// $NON-NLS-1$
Object user2CachedDocument = cacheManager.getFromRegionCache(userSession2.getId(), "repoDoc");
Assert.assertNull(user2CachedDocument);
cacheManager.addCacheRegion(userSession1.getId());
cacheManager.addCacheRegion(userSession2.getId());
// Ok - we now have some stuff to jam into the cache.
cacheManager.putInRegionCache(userSession1.getId(), "StringObject", user1StringObject);
// $NON-NLS-1$
cacheManager.putInRegionCache(userSession1.getId(), "repoDoc", user1Document);
// $NON-NLS-1$
cacheManager.putInRegionCache(userSession2.getId(), "StringObject", user2StringObject);
// $NON-NLS-1$
cacheManager.putInRegionCache(userSession2.getId(), "repoDoc", user2Document);
// Get them back out
// $NON-NLS-1$
Object user1CachedStringObject1 = cacheManager.getFromRegionCache(userSession1.getId(), "StringObject");
Assert.assertEquals(user1StringObject, (String) user1CachedStringObject1);
// $NON-NLS-1$
Object user1CachedDocument1 = cacheManager.getFromRegionCache(userSession1.getId(), "repoDoc");
String user1CompareXMLCached1 = ((Document) user1CachedDocument1).asXML();
Assert.assertEquals(user1CompareXMLOriginal, user1CompareXMLCached1);
// $NON-NLS-1$
Object user2CachedStringObject1 = cacheManager.getFromRegionCache(userSession2.getId(), "StringObject");
Assert.assertEquals(user2StringObject, (String) user2CachedStringObject1);
// $NON-NLS-1$
Object user2CachedDocument1 = cacheManager.getFromRegionCache(userSession2.getId(), "repoDoc");
String user2CompareXMLCached1 = ((Document) user2CachedDocument1).asXML();
Assert.assertEquals(user2CompareXMLOriginal, user2CompareXMLCached1);
// OK - We've verified that their objects are unique to each individual
// user.
// Test Removals from session only
// Remove a single user-session based object.
// $NON-NLS-1$
cacheManager.removeFromRegionCache(userSession1.getId(), "StringObject");
// Try to get it back anyway.
// $NON-NLS-1$
Object notThere = cacheManager.getFromRegionCache(userSession1.getId(), "StringObject");
Assert.assertNull(notThere);
// Make sure that User2 is unaffected
// $NON-NLS-1$
Object shouldBeThere = cacheManager.getFromRegionCache(userSession2.getId(), "StringObject");
Assert.assertNotNull(shouldBeThere);
// Kill user1's session
cacheManager.removeRegionCache(userSession1.getId());
// $NON-NLS-1$
notThere = cacheManager.getFromRegionCache(userSession1.getId(), "repoDoc");
Assert.assertNull(notThere);
// Make sure that User2 is still unaffected
// $NON-NLS-1$
shouldBeThere = cacheManager.getFromRegionCache(userSession2.getId(), "StringObject");
Assert.assertNotNull(shouldBeThere);
// Test Global Caching
cacheManager.addCacheRegion("Global");
// Put stuff in
// $NON-NLS-1$
cacheManager.putInRegionCache("Global", "globalIntegerKey", globalInt);
// $NON-NLS-1$
cacheManager.putInRegionCache("Global", "globalBigDecimalKey", globalBigDecimal);
// $NON-NLS-1$
cacheManager.putInRegionCache("Global", "globalStringBufferKey", globalStringBuffer);
// $NON-NLS-1$
Object cachedGlobalInt = cacheManager.getFromRegionCache("Global", "globalIntegerKey");
Assert.assertEquals(globalInt, cachedGlobalInt);
// $NON-NLS-1$
Object cachedGlobalBigDecimal = cacheManager.getFromRegionCache("Global", "globalBigDecimalKey");
Assert.assertEquals(globalBigDecimal, cachedGlobalBigDecimal);
// $NON-NLS-1$
Object cachedGlobalStringBuffer = cacheManager.getFromRegionCache("Global", "globalStringBufferKey");
Assert.assertEquals(globalStringBuffer, cachedGlobalStringBuffer);
// Test clear all session-based keys. This should leave the global stuff
// alone.
cacheManager.removeRegionCache(userSession2.getId());
// $NON-NLS-1$
notThere = cacheManager.getFromRegionCache(userSession2.getId(), "StringObject");
Assert.assertNull(notThere);
// $NON-NLS-1$
notThere = cacheManager.getFromRegionCache(userSession2.getId(), "repoDoc");
Assert.assertNull(notThere);
// $NON-NLS-1$
shouldBeThere = cacheManager.getFromRegionCache("Global", "globalIntegerKey");
Assert.assertNotNull(shouldBeThere);
// Totally clear out the cache.
cacheManager.clearCache();
// $NON-NLS-1$
notThere = cacheManager.getFromRegionCache("Global", "globalIntegerKey");
Assert.assertNull(notThere);
// $NON-NLS-1$
notThere = cacheManager.getFromRegionCache("Global", "globalBigDecimalKey");
Assert.assertNull(notThere);
// $NON-NLS-1$
notThere = cacheManager.getFromRegionCache("Global", "globalStringBufferKey");
Assert.assertNull(notThere);
cacheManager.addCacheRegion("Global");
// Assumes cache size is set to 2000 objects maximum.
for (int i = 0; i < 10000; i++) {
// $NON-NLS-1$
String someCachedString = "This is the string to cache " + i;
// $NON-NLS-1$
String someCachedKey = "SomeCachedKey" + i;
if ((i % 1000) == 0) {
sleep(5);
}
cacheManager.putInRegionCache("Global", someCachedKey, someCachedString);
}
// Let cache stabalize, and decide what hasn't been used for a while.
// 15 seconds should do it.
sleep(15);
// Get first item from the cache...
// $NON-NLS-1$
shouldBeThere = cacheManager.getFromRegionCache("Global", "SomeCachedKey1");
// $NON-NLS-1$
Assert.assertEquals(shouldBeThere, "This is the string to cache 1");
// Get middle item from the cache...
// $NON-NLS-1$
shouldBeThere = cacheManager.getFromRegionCache("Global", "SomeCachedKey5000");
// $NON-NLS-1$
Assert.assertEquals(shouldBeThere, "This is the string to cache 5000");
// Get last item from the cache...
// $NON-NLS-1$
shouldBeThere = cacheManager.getFromRegionCache("Global", "SomeCachedKey999");
// $NON-NLS-1$
Assert.assertEquals(shouldBeThere, "This is the string to cache 999");
// Clear cache again...
cacheManager.clearCache();
// Make sure...
// $NON-NLS-1$
notThere = cacheManager.getFromRegionCache("Global", "SomeCachedKey2");
Assert.assertNull(notThere);
// $NON-NLS-1$
notThere = cacheManager.getFromRegionCache("Global", "SomeCachedKey5002");
Assert.assertNull(notThere);
// $NON-NLS-1$
notThere = cacheManager.getFromRegionCache("Global", "SomeCachedKey998");
Assert.assertNull(notThere);
// Done with tests.
}
Aggregations