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

The following examples show how to use org.cache2k.Cache#peek() . 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: BasicCacheTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
public void testPeekAndPut() {
  Cache<String,String> c =
    Cache2kBuilder.of(String.class, String.class)
      .eternal(true)
      .build();
  String val = c.peek("something");
  c.put("something", "hello");
  val = c.get("something");
  c.close();
}
 
Example 2
Source File: BasicCacheTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
public void testPeekAndPut() {
  Cache<String,String> c =
    Cache2kBuilder.of(String.class, String.class)
      .eternal(true)
      .build();
  String val = c.peek("something");
  c.put("something", "hello");
  val = c.get("something");
  c.close();
}
 
Example 3
Source File: CacheTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void testPeekAndPut() {
  Cache<String,String> c =
    Cache2kBuilder.of(String.class, String.class)
      .eternal(true)
      .build();
  String val = c.peek("something");
  assertNull(val);
  c.put("something", "hello");
  val = c.get("something");
  assertNotNull(val);
  c.close();
}
 
Example 4
Source File: CacheTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetEntry() {
  Cache<String,String> c =
    Cache2kBuilder.of(String.class, String.class)
      .eternal(true)
      .build();
  String val = c.peek("something");
  assertNull(val);
  c.put("something", "hello");
  CacheEntry<String, String> e = c.getEntry("something");
  assertNotNull(e);
  assertEquals("hello", e.getValue());
  c.close();
}
 
Example 5
Source File: CacheTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void testContains() {
  Cache<String,String> c =
    Cache2kBuilder.of(String.class, String.class)
      .eternal(true)
      .build();
  String val = c.peek("something");
  assertNull(val);
  c.put("something", "hello");
  assertTrue(c.containsKey("something"));
  assertFalse(c.containsKey("dsaf"));
  c.close();
}
 
Example 6
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 7
Source File: ExpiryTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
/**
 * Don't suppress exceptions eternally if resilience policy is enabled by specifying
 * a retry interval.
 */
@Test
public void testEternalExceptionsExpireNoSuppress() {
  final IntCountingCacheSource g = new IntCountingCacheSource() {
    @Override
    public Integer load(final Integer o) {
      incrementLoadCalledCount();
      if (o == 99) {
        throw new RuntimeException("ouch");
      }
      return o;
    }
  };
  final Cache<Integer, Integer> c = cache = builder(Integer.class, Integer.class)
    .loader(g)
    .eternal(true)
    .retryInterval(TestingParameters.MINIMAL_TICK_MILLIS, TimeUnit.MILLISECONDS)
    .build();
  c.put(99, 1);
  int v = c.peek(99);
  assertEquals(1, v);
  TimeBox.millis(TestingParameters.MINIMAL_TICK_MILLIS)
    .work(new Runnable() {
      @Override
      public void run() {
        loadAndWait(new LoaderRunner() {
          @Override
          public void run(final CacheOperationCompletionListener l) {
            c.reloadAll(toIterable(99), l);
          }
        });
      }
    })
    .check(new Runnable() {
      @Override
      public void run() {
        try {
          c.get(99);
          fail("exception expected");
        } catch (Exception ex) {
          assertTrue(ex instanceof CacheLoaderException);
          assertTrue("expiry on exception", ex.toString().contains(EXPIRY_MARKER));
          assertTrue(ex.getCause() instanceof RuntimeException);
        }
      }
    });
  assertTrue(g.getLoaderCalledCount() > 0);
}