/** * Gets the cached result for this region/key OR does the work and caches * the result, returning the result. If the result has not been cached yet, * this calls doWork() on the JCSWorkerHelper to do the work and cache the * result. This is also an opportunity to do any post processing of the * result in your CachedWorker implementation. * @param aKey * The key to get/put with on the Cache. * @param aWorker * The JCSWorkerHelper implementing Object doWork(). This gets * called if the cache get misses, and the result is put into * cache. * @return The result of doing the work, or the cached result. * @throws Exception * Throws an exception if anything goes wrong while doing the * work. */ public V getResult( K aKey, JCSWorkerHelper aWorker ) throws Exception { return run( aKey, null, aWorker ); }
/** * Test basic worker functionality. This is a serial not a concurrent test. * <p> * Just verify that the worker will go to the cache before asking the helper. * * @throws Exception * */ public void testSimpleGet() throws Exception { JCSWorker<String, Long> cachingWorker = new JCSWorker<String, Long>( "example region" ); // This is the helper. JCSWorkerHelper helper = new AbstractJCSWorkerHelper() { int timesCalled = 0; @Override public Object doWork() { Object results = Long.valueOf( ++timesCalled ); return results; } }; String key = "abc"; Long result = cachingWorker.getResult( key, helper ); assertEquals( "Called the wrong number of times", Long.valueOf( 1 ), result ); // should get it from the cache. Long result2 = cachingWorker.getResult( key, helper ); assertEquals( "Called the wrong number of times", Long.valueOf( 1 ), result2 ); }
helper = map.get( getRegion() + aKey ); if ( helper == null ) map.put( getRegion() + aKey, aHelper ); logger.debug( "Found a worker already doing this work (" + getRegion() + ":" + aKey + ")." ); + getRegion() + ":" + aKey + ")." ); logger.debug( getRegion() + " is doing the work." ); logger.debug( getRegion() + ":" + aKey + " entered finally." ); map.remove( getRegion() + aKey );
/** * Test basic worker functionality. This is a serial not a concurrent test. * <p> * Just verify that the worker will go to the cache before asking the helper. * * @throws Exception * */ public void testSimpleGet() throws Exception { JCSWorker<String, Long> cachingWorker = new JCSWorker<String, Long>( "example region" ); // This is the helper. JCSWorkerHelper<Long> helper = new AbstractJCSWorkerHelper<Long>() { int timesCalled = 0; @Override public Long doWork() { return Long.valueOf( ++timesCalled ); } }; String key = "abc"; Long result = cachingWorker.getResult( key, helper ); assertEquals( "Called the wrong number of times", Long.valueOf( 1 ), result ); // should get it from the cache. Long result2 = cachingWorker.getResult( key, helper ); assertEquals( "Called the wrong number of times", Long.valueOf( 1 ), result2 ); }
JCSWorkerHelper<V> helper = map.putIfAbsent(getRegion() + aKey, aHelper); logger.debug( "Found a worker already doing this work (" + getRegion() + ":" + aKey + ")." ); + getRegion() + ":" + aKey + ")." ); logger.debug( getRegion() + " is doing the work." ); logger.debug( getRegion() + ":" + aKey + " entered finally." ); map.remove( getRegion() + aKey );
/** * Gets the cached result for this region/key OR does the work and caches * the result, returning the result. If the result has not been cached yet, * this calls doWork() on the JCSWorkerHelper to do the work and cache the * result. This is also an opportunity to do any post processing of the * result in your CachedWorker implementation. * @param aKey * The key to get/put with on the Cache. * @param aWorker * The JCSWorkerHelper implementing Object doWork(). This gets * called if the cache get misses, and the result is put into * cache. * @return The result of doing the work, or the cached result. * @throws Exception * Throws an exception if anything goes wrong while doing the * work. */ public V getResult( K aKey, JCSWorkerHelper<V> aWorker ) throws Exception { return run( aKey, null, aWorker ); }
/** * Gets the cached result for this region/key OR does the work and caches * the result, returning the result. If the result has not been cached yet, * this calls doWork() on the JCSWorkerHelper to do the work and cache the * result. This is also an opportunity to do any post processing of the * result in your CachedWorker implementation. * @param aKey * The key to get/put with on the Cache. * @param aGroup * The cache group to put the result in. * @param aWorker * The JCSWorkerHelper implementing Object doWork(). This gets * called if the cache get misses, and the result is put into * cache. * @return The result of doing the work, or the cached result. * @throws Exception * Throws an exception if anything goes wrong while doing the * work. */ public V getResult( K aKey, String aGroup, JCSWorkerHelper<V> aWorker ) throws Exception { return run( aKey, aGroup, aWorker ); }
/** * Gets the cached result for this region/key OR does the work and caches * the result, returning the result. If the result has not been cached yet, * this calls doWork() on the JCSWorkerHelper to do the work and cache the * result. This is also an opertunity to do any post processing of the * result in your CachedWorker implementation. * @param aKey * The key to get/put with on the Cache. * @param aGroup * The cache group to put the result in. * @param aWorker * The JCSWorkerHelper implementing Object doWork(). This gets * called if the cache get misses, and the result is put into * cache. * @return The result of doing the work, or the cached result. * @throws Exception * Throws an exception if anything goes wrong while doing the * work. */ public V getResult( K aKey, String aGroup, JCSWorkerHelper aWorker ) throws Exception { return run( aKey, aGroup, aWorker ); }