Java Code Examples for org.cache2k.Cache#loadAll()

The following examples show how to use org.cache2k.Cache#loadAll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CacheLoaderTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadAll() throws Exception {
  final AtomicInteger countLoad =  new AtomicInteger();
  Cache<Integer,Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
    @Override
    public void extend(final Cache2kBuilder<Integer, Integer> b) {
      b .loader(new CacheLoader<Integer, Integer>() {
        @Override
        public Integer load(final Integer key) throws Exception {
          return countLoad.incrementAndGet();
        }
      });
    }
  });
  c.get(5);
  CompletionWaiter w = new CompletionWaiter();
  c.loadAll(toIterable(5, 6), w);
  w.awaitCompletion();
  assertEquals(2, countLoad.get());
  assertEquals((Integer) 2, c.get(6));
  c.loadAll(toIterable(5, 6), null);
  c.loadAll(Collections.EMPTY_SET, null);
}
 
Example 2
Source File: CacheLoaderTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * We should always have two loader threads.
 */
@Test
public void testTwoLoaderThreadsAndPoolInfo() throws Exception {
  final CountDownLatch inLoader = new CountDownLatch(2);
  final CountDownLatch releaseLoader = new CountDownLatch(1);
  Cache<Integer,Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
    @Override
    public void extend(final Cache2kBuilder<Integer, Integer> b) {
      b .loader(new CacheLoader<Integer, Integer>() {
        @Override
        public Integer load(final Integer key) throws Exception {
          inLoader.countDown();
          releaseLoader.await();
          return key * 2;
        }
      });
    }
  });
  c.loadAll(toIterable(1), null);
  c.loadAll(toIterable(2), null);
  inLoader.await();
  assertEquals(2, latestInfo(c).getAsyncLoadsStarted());
  assertEquals(2, latestInfo(c).getAsyncLoadsInFlight());
  assertEquals(2, latestInfo(c).getLoaderThreadsMaxActive());
  releaseLoader.countDown();
}
 
Example 3
Source File: CacheLoaderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void testSeparateLoaderExecutor() {
  final AtomicInteger executionCount = new AtomicInteger(0);
  Cache<Integer,Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
    @Override
    public void extend(final Cache2kBuilder<Integer, Integer> b) {
      b.loader(new CacheLoader<Integer, Integer>() {
        @Override
        public Integer load(final Integer key) throws Exception {
          return key * 2;
        }
      });
      b.loaderExecutor(new Executor() {
        @Override
        public void execute(final Runnable command) {
          executionCount.incrementAndGet();
          getLoaderExecutor().execute(command);
        }
      });
    }
  });
  assertEquals((Integer) 10, c.get(5));
  assertEquals((Integer) 20, c.get(10));
  assertEquals(0, executionCount.get());
  CompletionWaiter waiter = new CompletionWaiter();
  c.loadAll(toIterable(1, 2, 3), waiter);
  waiter.awaitCompletion();
  assertEquals("executor is used", 3, executionCount.get());
  waiter = new CompletionWaiter();
  c.prefetchAll(toIterable(6, 7, 8), waiter);
  waiter.awaitCompletion();
  assertEquals("prefetch uses executor, too", 6, executionCount.get());
}
 
Example 4
Source File: CacheLoaderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * Start two overlapping loads, expect that one is done in the caller thread,
 * since only one thread is available.
 */
@Test
public void testOneLoaderThreadsAndPoolInfo() throws Exception {
  final Thread callingThread = Thread.currentThread();
  final CountDownLatch inLoader = new CountDownLatch(1);
  final CountDownLatch releaseLoader = new CountDownLatch(1);
  final AtomicInteger asyncCount = new AtomicInteger();
  Cache<Integer,Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
    @Override
    public void extend(final Cache2kBuilder<Integer, Integer> b) {
      b .loaderThreadCount(1)
        .loader(new CacheLoader<Integer, Integer>() {
        @Override
        public Integer load(final Integer key) throws Exception {
          if (callingThread != Thread.currentThread()) {
            asyncCount.incrementAndGet();
            inLoader.countDown();
            releaseLoader.await();
          }
          return key * 2;
        }
      });
    }
  });
  c.loadAll(toIterable(1), null);
  c.loadAll(toIterable(2), null);
  inLoader.await();
  assertEquals("only one load is separate thread", 1, latestInfo(c).getAsyncLoadsStarted());
  assertEquals("only one load is separate thread", 1, asyncCount.get());
  assertEquals(1, latestInfo(c).getAsyncLoadsInFlight());
  assertEquals(1, latestInfo(c).getLoaderThreadsMaxActive());
  releaseLoader.countDown();
}
 
Example 5
Source File: CacheLoaderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsyncLoaderWithExecutorWithAsync() {
  final AtomicInteger loaderCalled = new AtomicInteger();
  final AtomicInteger loaderExecuted = new AtomicInteger();
  Cache<Integer,Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
    @Override
    public void extend(final Cache2kBuilder<Integer, Integer> b) {
      b.loader(new AsyncCacheLoader<Integer, Integer>() {
        @Override
        public void load(final Integer key, final Context<Integer, Integer> ctx, final Callback<Integer> callback) {
          loaderCalled.incrementAndGet();
           ctx.getLoaderExecutor().execute(new Runnable() {
             @Override
             public void run() {
               loaderExecuted.incrementAndGet();
               callback.onLoadSuccess(key);
             }
           });
        }
      });
    }
  });
  CompletionWaiter w = new CompletionWaiter();
  c.loadAll(TestingBase.keys(1, 2, 1802), w);
  w.awaitCompletion();
  assertEquals(1, (int) c.peek(1));
  Object o1 = c.peek(1802);
  assertTrue(c.peek(1802) == o1);
  w = new CompletionWaiter();
  c.reloadAll(TestingBase.keys(1802, 4, 5), w);
  w.awaitCompletion();
  assertNotNull(c.peek(1802));
  assertTrue(c.peek(1802) != o1);
}
 
Example 6
Source File: StaticUtil.java    From cache2k with Apache License 2.0 4 votes vote down vote up
public static <K,V> CacheLoaderTest.CompletionWaiter load(Cache<K,V> c, K ...keys) {
  CacheLoaderTest.CompletionWaiter w = new CacheLoaderTest.CompletionWaiter();
  c.loadAll(toIterable(keys), w);
  w.awaitCompletion();
  return w;
}
 
Example 7
Source File: CacheLoaderTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
/**
 * Execute loader in another thread.
 */
@Test
public void blockAndComplete() throws Exception {
  final int count = 1000;
  final AtomicInteger loaderCalled = new AtomicInteger();
  final CountDownLatch complete = new CountDownLatch(count);
  final AtomicInteger loaderExecuted = new AtomicInteger();
  final CountDownLatch releaseLoader = new CountDownLatch(1);
  Cache<Integer,Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
    @Override
    public void extend(final Cache2kBuilder<Integer, Integer> b) {
      b.loader(new AsyncCacheLoader<Integer, Integer>() {
        @Override
        public void load(final Integer key, final Context<Integer, Integer> ctx, final Callback<Integer> callback) {
          loaderCalled.incrementAndGet();
          ctx.getLoaderExecutor().execute(new Runnable() {
            @Override
            public void run() {
              try {
                releaseLoader.await();
              } catch (InterruptedException ex) {
                ex.printStackTrace();
              }
              loaderExecuted.incrementAndGet();
              callback.onLoadSuccess(key);
            }
          });
        }
      });
    }
  });
  final CacheOperationCompletionListener l = new CacheOperationCompletionListener() {
    @Override
    public void onCompleted() {
      complete.countDown();
    }

    @Override
    public void onException(final Throwable exception) {

    }
  };
  for (int i = 0; i < count; i++) {
    c.loadAll(toIterable(1,2,3), l);
  }
  releaseLoader.countDown();
  complete.await(TestingParameters.MAX_FINISH_WAIT_MILLIS, TimeUnit.MILLISECONDS);
}