Examples of GlobalCacheService


Examples of org.apache.turbine.services.cache.GlobalCacheService

     * @throws Exception
     */   
   
    public void testObjectCount() throws Exception {

        GlobalCacheService globalCache = (GlobalCacheService)TurbineServices
        .getInstance()
        .getService( GlobalCacheService.SERVICE_NAME );
        assertNotNull("Could not retrive cache service.", globalCache);
       
        // Create and add Object that expires in 1.5 turbine Refresh
        long expireTime = TURBINE_CACHE_REFRESH + TURBINE_CACHE_REFRESH/2;
        CachedObject cacheObject = new CachedObject("This is a test", expireTime);
        assertNotNull( "Failed to create a cachable object", cacheObject);

        globalCache.addObject(cacheKey, cacheObject);
        assertEquals("After adding 1 Object", 1, globalCache.getNumberOfObjects());
       
        // Wait until we're passed 1 refresh, but not half way.
        Thread.sleep(TURBINE_CACHE_REFRESH + TURBINE_CACHE_REFRESH/3);
        assertEquals("After one refresh", 1, globalCache.getNumberOfObjects());

        // Wait until we're passed 2 more refreshes
        Thread.sleep((TURBINE_CACHE_REFRESH * 2) + TURBINE_CACHE_REFRESH/3);
        assertEquals("After three refreshes", 0, globalCache.getNumberOfObjects());
    }
View Full Code Here

Examples of org.apache.turbine.services.cache.GlobalCacheService

        RefreshableCachedObject cacheObject = null;
        long addTime = 0;
       
        boolean turbineCachePatchApplied = false; // FIXME:  Remove when patch applied
       
        GlobalCacheService globalCache = (GlobalCacheService)TurbineServices
        .getInstance()
        .getService( GlobalCacheService.SERVICE_NAME );
       
        // Create and add Object that expires in TEST_EXPIRETIME millis.
        cacheObject = new RefreshableCachedObject(new RefreshableObject(), TEST_EXPIRETIME);
        assertNotNull( "Failed to create a cachable object", cacheObject);
        globalCache.addObject(cacheKey, cacheObject);
        addTime = System.currentTimeMillis();
       
        // Try to get un-expired object
        try {
            retrievedObject = null;
            retrievedObject = globalCache.getObject(cacheKey);
            assertNotNull( "Did not retrieved a cached object", retrievedObject);
            assertEquals( "Did not retrieved correct cached object", cacheObject, retrievedObject);
        } catch (ObjectExpiredException e) {
            assertTrue( "Object expired early ( " + (System.currentTimeMillis() - addTime) + " millis)", false);
        } catch (Exception e) {
            throw e;
        }
       
        // Wait 1 Turbine cache refresh + 1 second.
        Thread.sleep(TEST_EXPIRETIME + 1000);
       
        // Try to get expired object
        try {
            retrievedObject = null;
            retrievedObject = globalCache.getObject(cacheKey);
            assertNotNull( "Did not retrieved a cached object, after sleep", retrievedObject);
            assertNotNull( "Cached object has no contents, after sleep.", ((RefreshableCachedObject)retrievedObject).getContents());
            assertTrue( "Object did not refresh.", ( ((RefreshableObject)((RefreshableCachedObject)retrievedObject).getContents()).getRefreshCount() > 0));
        } catch (ObjectExpiredException e) {
            assertTrue( "Received unexpected ObjectExpiredException exception "
            + "when retrieving refreshable object after ( "
            + (System.currentTimeMillis() - addTime) + " millis)", false);
        } catch (Exception e) {
            throw e;
        }
       
        // See if object will expires (testing every second for 100 seconds.  It sould not!
        for (int i=0; (i<100); i++) {
            Thread.sleep(1000); // Sleep 0.5 seconds
           
            // Try to get expired object
            try {
                retrievedObject = null;
                retrievedObject = globalCache.getObject(cacheKey);
                assertNotNull( "Did not retrieved a cached object, after sleep", retrievedObject);
                assertNotNull( "Cached object has no contents, after sleep.", ((RefreshableCachedObject)retrievedObject).getContents());
                assertTrue( "Object did not refresh.", ( ((RefreshableObject)((RefreshableCachedObject)retrievedObject).getContents()).getRefreshCount() > 0));
            } catch (ObjectExpiredException e) {
                assertTrue( "Received unexpected ObjectExpiredException exception "
                + "when retrieving refreshable object after ( "
                + (System.currentTimeMillis() - addTime) + " millis)", false);
            } catch (Exception e) {
                throw e;
            }
        }
       
        // Remove objects
        globalCache.removeObject(cacheKey);
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.