Package org.pentaho.platform.api.engine

Examples of org.pentaho.platform.api.engine.ICacheManager


  @Path( "/reportingDataCache" )
  @Produces( { MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON } )
  @Facet ( name = "Unsupported" )
  public Response purgeReportingDataCache() {
    if ( canAdminister() ) {
      ICacheManager cacheManager = PentahoSystem.get( ICacheManager.class );
      cacheManager.clearRegionCache( "report-dataset-cache" );
      cacheManager.clearRegionCache( "report-output-handlers" );
      return Response.ok().type( MediaType.TEXT_PLAIN ).build();
    } else {
      return Response.status( UNAUTHORIZED ).build();
    }
  }
View Full Code Here


   * session passed as a parameter.
   */
  @SuppressWarnings( "unchecked" )
  protected synchronized List<IOlapService.Catalog> getCache( IPentahoSession session ) {
    // Create the cache region if necessary.
    final ICacheManager cacheMgr = PentahoSystem.getCacheManager( session );
    final Object cacheKey = makeCacheSubRegionKey( getLocale() );


    final Lock writeLock = cacheLock.writeLock();
    try {

      writeLock.lock();

      if ( !cacheMgr.cacheEnabled( CATALOG_CACHE_REGION ) ) {
        // Create the region. This requires write access.
        cacheMgr.addCacheRegion( CATALOG_CACHE_REGION );
      }

      if ( cacheMgr.getFromRegionCache( CATALOG_CACHE_REGION, cacheKey ) == null ) {
        // Create the sub-region. This requires write access.
        cacheMgr.putInRegionCache(
          CATALOG_CACHE_REGION,
          cacheKey,
          new ArrayList<IOlapService.Catalog>() );
      }

      return (List<IOlapService.Catalog>)
        cacheMgr.getFromRegionCache( CATALOG_CACHE_REGION, cacheKey );

    } finally {
      writeLock.unlock();
    }
  }
View Full Code Here

   */
  protected void resetCache( IPentahoSession session ) {
    final Lock writeLock = cacheLock.writeLock();
    try {
      writeLock.lock();
      final ICacheManager cacheMgr = PentahoSystem.getCacheManager( session );
      cacheMgr.clearRegionCache( CATALOG_CACHE_REGION );
    } finally {
      writeLock.unlock();
    }
  }
View Full Code Here

  public void contextDestroyed( final ServletContextEvent event ) {
    // NOTE: if the cacheManager has been configured to have session creation scope
    // getCacheManager will return null, which is fine, since PentahoCacheSessionListener
    // should have cleaned up the session scoped caches. If the cacheManager
    // has been created with global scope, getCacheManager will return a non-null instance.
    ICacheManager cacheManager = PentahoSystem.getCacheManager( null );
    if ( cacheManager != null ) {
      cacheManager.cacheStop();
    }
  }
View Full Code Here

  // private final StringBuffer longString = new StringBuffer();

  public void testCacheRegion() {

    // Make sure we have a cache first...
    ICacheManager cacheManager = PentahoSystem.getCacheManager( null ); // TODO sbarkdull, need to get real session in
                                                                        // here
    Assert.assertNotNull( cacheManager );
    Assert.assertTrue( cacheManager.cacheEnabled() );

    // Test Session Based Caching
    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" ); //$NON-NLS-1$ //$NON-NLS-2$

    // ================================ Create Objects
    // User Objects

    // Cache any-old String...
    String user1StringObject = "User1's String Object"; //$NON-NLS-1$
    // Make sure we can cache these Document objects...
    Document user1Document = DocumentHelper.createDocument();
    Element user1RootNode = user1Document.addElement( "user1" ); //$NON-NLS-1$
    Element user1FileNode = user1RootNode.addElement( "file" ); //$NON-NLS-1$
    user1FileNode.addAttribute( "name", "test" ); //$NON-NLS-1$ //$NON-NLS-2$
    String user1CompareXMLOriginal = user1Document.asXML();

    // User2's Objects
    // Cache any-old String...
    String user2StringObject = "User2's String Object"; //$NON-NLS-1$
    Document user2Document = DocumentHelper.createDocument();
    Element user2RootNode = user2Document.addElement( "user2" ); //$NON-NLS-1$
    Element user2FileNode = user2RootNode.addElement( "folder" ); //$NON-NLS-1$
    user2FileNode.addAttribute( "name", "test2" ); //$NON-NLS-1$ //$NON-NLS-2$
    String user2CompareXMLOriginal = user2Document.asXML();

    // Global Objects
    Integer globalInt = new Integer( 372 );
    BigDecimal globalBigDecimal = new BigDecimal( "2342.123334444211" ); //$NON-NLS-1$
    StringBuffer globalStringBuffer = new StringBuffer();
    globalStringBuffer.append( "This is a really long string to stick in a string buffer" ); //$NON-NLS-1$

    cacheManager.putInRegionCache( userSession1.getId(), "StringObject", user1StringObject );
    cacheManager.putInRegionCache( userSession1.getId(), "repoDoc", user1Document ); //$NON-NLS-1$
    cacheManager.putInRegionCache( userSession2.getId(), "StringObject", user2StringObject ); //$NON-NLS-1$
    cacheManager.putInRegionCache( userSession2.getId(), "repoDoc", user2Document ); // $NON-N

    // Get them back out
    Object user1CachedStringObject = cacheManager.getFromRegionCache( userSession1.getId(), "StringObject" ); //$NON-NLS-1$
    Assert.assertNull( user1CachedStringObject );
    Object user1CachedDocument = cacheManager.getFromRegionCache( userSession1.getId(), "repoDoc" ); //$NON-NLS-1$
    Assert.assertNull( user1CachedDocument );
    Object user2CachedStringObject = cacheManager.getFromRegionCache( userSession2.getId(), "StringObject" ); //$NON-NLS-1$
    Assert.assertNull( user2CachedStringObject );
    Object user2CachedDocument = cacheManager.getFromRegionCache( userSession2.getId(), "repoDoc" ); //$NON-NLS-1$
    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 );
    cacheManager.putInRegionCache( userSession1.getId(), "repoDoc", user1Document ); //$NON-NLS-1$
    cacheManager.putInRegionCache( userSession2.getId(), "StringObject", user2StringObject ); //$NON-NLS-1$
    cacheManager.putInRegionCache( userSession2.getId(), "repoDoc", user2Document ); //$NON-NLS-1$

    // Get them back out
    Object user1CachedStringObject1 = cacheManager.getFromRegionCache( userSession1.getId(), "StringObject" ); //$NON-NLS-1$
    Assert.assertEquals( user1StringObject, (String) user1CachedStringObject1 );
    Object user1CachedDocument1 = cacheManager.getFromRegionCache( userSession1.getId(), "repoDoc" ); //$NON-NLS-1$
    String user1CompareXMLCached1 = ( (Document) user1CachedDocument1 ).asXML();
    Assert.assertEquals( user1CompareXMLOriginal, user1CompareXMLCached1 );

    Object user2CachedStringObject1 = cacheManager.getFromRegionCache( userSession2.getId(), "StringObject" ); //$NON-NLS-1$
    Assert.assertEquals( user2StringObject, (String) user2CachedStringObject1 );
    Object user2CachedDocument1 = cacheManager.getFromRegionCache( userSession2.getId(), "repoDoc" ); //$NON-NLS-1$
    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.
    cacheManager.removeFromRegionCache( userSession1.getId(), "StringObject" ); //$NON-NLS-1$
    // Try to get it back anyway.
    Object notThere = cacheManager.getFromRegionCache( userSession1.getId(), "StringObject" ); //$NON-NLS-1$
    Assert.assertNull( notThere );

    // Make sure that User2 is unaffected
    Object shouldBeThere = cacheManager.getFromRegionCache( userSession2.getId(), "StringObject" ); //$NON-NLS-1$
    Assert.assertNotNull( shouldBeThere );

    // Kill user1's session
    cacheManager.removeRegionCache( userSession1.getId() );
    notThere = cacheManager.getFromRegionCache( userSession1.getId(), "repoDoc" ); //$NON-NLS-1$
    Assert.assertNull( notThere );

    // Make sure that User2 is still unaffected
    shouldBeThere = cacheManager.getFromRegionCache( userSession2.getId(), "StringObject" ); //$NON-NLS-1$
    Assert.assertNotNull( shouldBeThere );

    // Test Global Caching
    cacheManager.addCacheRegion( "Global" );
    // Put stuff in
    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" ); //$NON-NLS-1$
    Assert.assertEquals( globalInt, cachedGlobalInt );
    Object cachedGlobalBigDecimal = cacheManager.getFromRegionCache( "Global", "globalBigDecimalKey" ); //$NON-NLS-1$
    Assert.assertEquals( globalBigDecimal, cachedGlobalBigDecimal );
    Object cachedGlobalStringBuffer = cacheManager.getFromRegionCache( "Global", "globalStringBufferKey" ); //$NON-NLS-1$
    Assert.assertEquals( globalStringBuffer, cachedGlobalStringBuffer );

    // Test clear all session-based keys. This should leave the global stuff
    // alone.
    cacheManager.removeRegionCache( userSession2.getId() );
    notThere = cacheManager.getFromRegionCache( userSession2.getId(), "StringObject" ); //$NON-NLS-1$
    Assert.assertNull( notThere );
    notThere = cacheManager.getFromRegionCache( userSession2.getId(), "repoDoc" ); //$NON-NLS-1$
    Assert.assertNull( notThere );
    shouldBeThere = cacheManager.getFromRegionCache( "Global", "globalIntegerKey" ); //$NON-NLS-1$
    Assert.assertNotNull( shouldBeThere );

    // Totally clear out the cache.
    cacheManager.clearCache();
    notThere = cacheManager.getFromRegionCache( "Global", "globalIntegerKey" ); //$NON-NLS-1$
    Assert.assertNull( notThere );
    notThere = cacheManager.getFromRegionCache( "Global", "globalBigDecimalKey" ); //$NON-NLS-1$
    Assert.assertNull( notThere );
    notThere = cacheManager.getFromRegionCache( "Global", "globalStringBufferKey" ); //$NON-NLS-1$
    Assert.assertNull( notThere );
    cacheManager.addCacheRegion( "Global" );
    // Force cache overload - make sure it spools objects to disk...
    // Assumes cache size is set to 2000 objects maximum.
    for ( int i = 0; i < 10000; i++ ) {
      String someCachedString = "This is the string to cache " + i; //$NON-NLS-1$
      String someCachedKey = "SomeCachedKey" + i; //$NON-NLS-1$
      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...
    shouldBeThere = cacheManager.getFromRegionCache( "Global", "SomeCachedKey1" ); //$NON-NLS-1$
    Assert.assertEquals( shouldBeThere, "This is the string to cache 1" ); //$NON-NLS-1$
    // Get middle item from the cache...
    shouldBeThere = cacheManager.getFromRegionCache( "Global", "SomeCachedKey5000" ); //$NON-NLS-1$
    Assert.assertEquals( shouldBeThere, "This is the string to cache 5000" ); //$NON-NLS-1$
    // Get last item from the cache...
    shouldBeThere = cacheManager.getFromRegionCache( "Global", "SomeCachedKey999" ); //$NON-NLS-1$
    Assert.assertEquals( shouldBeThere, "This is the string to cache 999" ); //$NON-NLS-1$

    // Clear cache again...
    cacheManager.clearCache();

    // Make sure...
    notThere = cacheManager.getFromRegionCache( "Global", "SomeCachedKey2" ); //$NON-NLS-1$
    Assert.assertNull( notThere );

    notThere = cacheManager.getFromRegionCache( "Global", "SomeCachedKey5002" ); //$NON-NLS-1$
    Assert.assertNull( notThere );

    notThere = cacheManager.getFromRegionCache( "Global", "SomeCachedKey998" ); //$NON-NLS-1$
    Assert.assertNull( notThere );

    // Done with tests.

  }
View Full Code Here

    return d;
  }

  public void tearDown() {
    // Clean the cache
    ICacheManager cacheManager = PentahoSystem.getCacheManager( null );
    cacheManager.clearRegionCache( CACHE_NAME );

    super.tearDown();
  }
View Full Code Here

  // private final StringBuffer longString = new StringBuffer();

  public void testCache() {

    // Make sure we have a cache first...
    ICacheManager cacheManager = PentahoSystem.getCacheManager( null ); // TODO sbarkdull, need to get real session in
                                                                        // here
    Assert.assertNotNull( cacheManager );
    Assert.assertTrue( cacheManager.cacheEnabled() );

    // Test Session Based Caching
    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" ); //$NON-NLS-1$ //$NON-NLS-2$

    // ================================ Create Objects
    // User Objects

    // Cache any-old String...
    String user1StringObject = "User1's String Object"; //$NON-NLS-1$
    // Make sure we can cache these Document objects...
    Document user1Document = DocumentHelper.createDocument();
    Element user1RootNode = user1Document.addElement( "user1" ); //$NON-NLS-1$
    Element user1FileNode = user1RootNode.addElement( "file" ); //$NON-NLS-1$
    user1FileNode.addAttribute( "name", "test" ); //$NON-NLS-1$ //$NON-NLS-2$
    String user1CompareXMLOriginal = user1Document.asXML();

    // User2's Objects
    // Cache any-old String...
    String user2StringObject = "User2's String Object"; //$NON-NLS-1$
    Document user2Document = DocumentHelper.createDocument();
    Element user2RootNode = user2Document.addElement( "user2" ); //$NON-NLS-1$
    Element user2FileNode = user2RootNode.addElement( "folder" ); //$NON-NLS-1$
    user2FileNode.addAttribute( "name", "test2" ); //$NON-NLS-1$ //$NON-NLS-2$
    String user2CompareXMLOriginal = user2Document.asXML();

    // Global Objects
    Integer globalInt = new Integer( 372 );
    BigDecimal globalBigDecimal = new BigDecimal( "2342.123334444211" ); //$NON-NLS-1$
    StringBuffer globalStringBuffer = new StringBuffer();
    globalStringBuffer.append( "This is a really long string to stick in a string buffer" ); //$NON-NLS-1$

    // Ok - we now have some stuff to jam into the cache.
    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 ); //$NON-NLS-1$

    // Get them back out
    Object user1CachedStringObject = cacheManager.getFromSessionCache( userSession1, "StringObject" ); //$NON-NLS-1$
    Assert.assertEquals( user1StringObject, (String) user1CachedStringObject );
    Object user1CachedDocument = cacheManager.getFromSessionCache( userSession1, "repoDoc" ); //$NON-NLS-1$
    String user1CompareXMLCached = ( (Document) user1CachedDocument ).asXML();
    Assert.assertEquals( user1CompareXMLOriginal, user1CompareXMLCached );

    Object user2CachedStringObject = cacheManager.getFromSessionCache( userSession2, "StringObject" ); //$NON-NLS-1$
    Assert.assertEquals( user2StringObject, (String) user2CachedStringObject );
    Object user2CachedDocument = cacheManager.getFromSessionCache( userSession2, "repoDoc" ); //$NON-NLS-1$
    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.
    cacheManager.removeFromSessionCache( userSession1, "StringObject" ); //$NON-NLS-1$
    // Try to get it back anyway.
    Object notThere = cacheManager.getFromSessionCache( userSession1, "StringObject" ); //$NON-NLS-1$
    Assert.assertNull( notThere );

    // Make sure that User2 is unaffected
    Object shouldBeThere = cacheManager.getFromSessionCache( userSession2, "StringObject" ); //$NON-NLS-1$
    Assert.assertNotNull( shouldBeThere );

    // Kill user1's session
    cacheManager.killSessionCache( userSession1 );
    notThere = cacheManager.getFromSessionCache( userSession1, "repoDoc" ); //$NON-NLS-1$
    Assert.assertNull( notThere );

    // Make sure that User2 is still unaffected
    shouldBeThere = cacheManager.getFromSessionCache( userSession2, "StringObject" ); //$NON-NLS-1$
    Assert.assertNotNull( shouldBeThere );

    // Test Global Caching

    // Put stuff in
    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" ); //$NON-NLS-1$
    Assert.assertEquals( globalInt, cachedGlobalInt );
    Object cachedGlobalBigDecimal = cacheManager.getFromGlobalCache( "globalBigDecimalKey" ); //$NON-NLS-1$
    Assert.assertEquals( globalBigDecimal, cachedGlobalBigDecimal );
    Object cachedGlobalStringBuffer = cacheManager.getFromGlobalCache( "globalStringBufferKey" ); //$NON-NLS-1$
    Assert.assertEquals( globalStringBuffer, cachedGlobalStringBuffer );

    // Test clear all session-based keys. This should leave the global stuff
    // alone.
    cacheManager.killSessionCaches();
    notThere = cacheManager.getFromSessionCache( userSession2, "StringObject" ); //$NON-NLS-1$
    Assert.assertNull( notThere );
    notThere = cacheManager.getFromSessionCache( userSession2, "repoDoc" ); //$NON-NLS-1$
    Assert.assertNull( notThere );
    shouldBeThere = cacheManager.getFromGlobalCache( "globalIntegerKey" ); //$NON-NLS-1$
    Assert.assertNotNull( shouldBeThere );

    // Totally clear out the cache.
    cacheManager.clearCache();
    notThere = cacheManager.getFromGlobalCache( "globalIntegerKey" ); //$NON-NLS-1$
    Assert.assertNull( notThere );
    notThere = cacheManager.getFromGlobalCache( "globalBigDecimalKey" ); //$NON-NLS-1$
    Assert.assertNull( notThere );
    notThere = cacheManager.getFromGlobalCache( "globalStringBufferKey" ); //$NON-NLS-1$
    Assert.assertNull( notThere );
    cacheManager.addCacheRegion( ICacheManager.GLOBAL );
    // Force cache overload - make sure it spools objects to disk...
    // Assumes cache size is set to 2000 objects maximum.
    for ( int i = 0; i < 10000; i++ ) {
      String someCachedString = "This is the string to cache " + i; //$NON-NLS-1$
      String someCachedKey = "SomeCachedKey" + i; //$NON-NLS-1$
      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...
    shouldBeThere = cacheManager.getFromGlobalCache( "SomeCachedKey1" ); //$NON-NLS-1$
    Assert.assertEquals( shouldBeThere, "This is the string to cache 1" ); //$NON-NLS-1$
    // Get middle item from the cache...
    shouldBeThere = cacheManager.getFromGlobalCache( "SomeCachedKey5000" ); //$NON-NLS-1$
    Assert.assertEquals( shouldBeThere, "This is the string to cache 5000" ); //$NON-NLS-1$
    // Get last item from the cache...
    shouldBeThere = cacheManager.getFromGlobalCache( "SomeCachedKey999" ); //$NON-NLS-1$
    Assert.assertEquals( shouldBeThere, "This is the string to cache 999" ); //$NON-NLS-1$

    // Clear cache again...
    cacheManager.clearCache();

    // Make sure...
    notThere = cacheManager.getFromGlobalCache( "SomeCachedKey2" ); //$NON-NLS-1$
    Assert.assertNull( notThere );

    notThere = cacheManager.getFromGlobalCache( "SomeCachedKey5002" ); //$NON-NLS-1$
    Assert.assertNull( notThere );

    notThere = cacheManager.getFromGlobalCache( "SomeCachedKey998" ); //$NON-NLS-1$
    Assert.assertNull( notThere );

    // Done with tests.

  }
View Full Code Here

  // TODO: clean this up so we don't have a fallback impl
  public static ICacheManager getCacheManager( IPentahoSession session ) {
    try {
      // TODO get the SimpleMapCacheManager into the object map somehow
      // we will try to use a simple map cache manager if one has not been configured
      ICacheManager cacheManager = aggObjectFactory.get( ICacheManager.class, session );
      return cacheManager;
    } catch ( ObjectFactoryException e ) {
      ICacheManager cacheManager = SimpleMapCacheManager.getInstance();
      Logger.warn( PentahoSystem.class.getName(), "Using default cache manager" ); //$NON-NLS-1$
      return cacheManager;
    }
  }
View Full Code Here

TOP

Related Classes of org.pentaho.platform.api.engine.ICacheManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.