Java Code Examples for org.ehcache.Cache#replace()

The following examples show how to use org.ehcache.Cache#replace() . 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: OffHeapOsgiTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
public static void testOffHeapClientClass() {
  CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
    .withClassLoader(TestMethods.class.getClassLoader())
    .withCache("myCache", newCacheConfigurationBuilder(Long.class, Order.class, newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES).offheap(2, MemoryUnit.MB))
      .build())
    .build(true);

  Cache<Long, Order> cache = cacheManager.getCache("myCache", Long.class, Order.class);

  Order order = new Order(42L);
  cache.put(42L, order);

  assertTrue(cache.get(42L) instanceof Order);

  cache.replace(42L, order, new Order(-1L));

  assertEquals(-1L, cache.get(42L).id);
}
 
Example 2
Source File: OverSizeMappingTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testOverSizedObjectPutFailsWithOnHeapAsAuthority() {
  CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
      .withDefaultSizeOfMaxObjectSize(500, MemoryUnit.B).build(true);

  CacheConfiguration<String, String> cacheConfiguration = CacheConfigurationBuilder
      .newCacheConfigurationBuilder(String.class, String.class,
          newResourcePoolsBuilder()
              .heap(100, MemoryUnit.KB).build())
      .build();

  Cache<String, String> cache = cacheManager.createCache("cache",
      cacheConfiguration);

  cache.put("key1", getOverSizedObject());
  assertThat(cache.get("key1"), nullValue());

  cache.put("key1", "value1");
  cache.replace("key1", getOverSizedObject());
  assertThat(cache.get("key1"), nullValue());

}
 
Example 3
Source File: SyncService.java    From moon-api-gateway with MIT License 5 votes vote down vote up
/**
 * The changed application information is reflected in the cache.
 *
 * @param appInfo Changed application information.
 * @return Whether the operation was successful. Not yet applied.
 */
private boolean updateApp(AppInfo appInfo) {
    Cache<Integer, AppInfo> appInfoCaches = apiExposeSpec.getAppInfoCache();
    appInfoCaches.replace(appInfo.getAppId(), appInfo);

    return true;
}
 
Example 4
Source File: SyncService.java    From moon-api-gateway with MIT License 5 votes vote down vote up
/**
 * Change the allowed ip for a specific application and apply it to the cache.
 *
 * @param appId The application Id.
 * @param requestIps These are the allowed ips for the application. You only need to add ip.
 * @return Whether the operation was successful. Not yet applied.
 */
private boolean updateWhitelistIp(int appId, List<String> requestIps) {
    Cache<Integer, AppInfo> appInfoCaches = apiExposeSpec.getAppInfoCache();
    AppInfo appInfo = appInfoCaches.get(appId);
    appInfo.getAppIpAcl().addAll(requestIps);
    appInfoCaches.replace(appInfo.getAppId(), appInfo);

    return true;
}
 
Example 5
Source File: SyncService.java    From moon-api-gateway with MIT License 5 votes vote down vote up
/**
 * Delete the allowed ip for the application.
 *
 * @param appId The Application Id.
 * @param requestIps The ip list to delete.
 * @return Whether the operation was successful. Not yet applied.
 */
private boolean deleteWhitelistIp(int appId, List<String> requestIps) {
    Cache<Integer, AppInfo> appInfoCaches = apiExposeSpec.getAppInfoCache();
    AppInfo appInfo = appInfoCaches.get(appId);
    requestIps.forEach(ip -> appInfo.getAppIpAcl().remove(ip));
    appInfoCaches.replace(appInfo.getAppId(), appInfo);

    return true;
}
 
Example 6
Source File: BasicClusteredWriteBehindPassthroughTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
private void replace(Cache<Long, String> cache, String value, boolean addToCacheRecords) {
  cache.replace(KEY, value);
  if (addToCacheRecords) {
    cacheRecords.add(new Record(KEY, cache.get(KEY)));
  }
}
 
Example 7
Source File: BasicClusteredWriteBehindPassthroughTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
private void replace(Cache<Long, String> cache, String oldValue, String newValue, boolean addToCacheRecords) {
  cache.replace(KEY, oldValue, newValue);
  if (addToCacheRecords) {
    cacheRecords.add(new Record(KEY, cache.get(KEY)));
  }
}
 
Example 8
Source File: ClusteredEventsTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Test
public void testNonExpiringEventSequence() throws TimeoutException {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
    newCacheManagerBuilder()
      .with(cluster(CLUSTER_URI).autoCreate(s -> s.defaultServerResource("primary-server-resource")))
      .withCache(runningTest.getMethodName(), newCacheConfigurationBuilder(Long.class, String.class,
        newResourcePoolsBuilder().with(clusteredDedicated(16, MemoryUnit.MB))));

  try (PersistentCacheManager driver = clusteredCacheManagerBuilder.build(true)) {
    Cache<Long, String> driverCache = driver.getCache(runningTest.getMethodName(), Long.class, String.class);
    try (PersistentCacheManager observer = clusteredCacheManagerBuilder.build(true)) {
      Cache<Long, String> observerCache = observer.getCache(runningTest.getMethodName(), Long.class, String.class);

      List<CacheEvent<? extends Long, ? extends String>> driverEvents = new ArrayList<>();
      driverCache.getRuntimeConfiguration().registerCacheEventListener(driverEvents::add, EventOrdering.ORDERED, EventFiring.ASYNCHRONOUS, allOf(EventType.class));

      List<CacheEvent<? extends Long, ? extends String>> observerEvents = new ArrayList<>();
      observerCache.getRuntimeConfiguration().registerCacheEventListener(observerEvents::add, EventOrdering.ORDERED, EventFiring.ASYNCHRONOUS, allOf(EventType.class));


      driverCache.put(1L, "foo");
      driverCache.put(1L, "bar");
      driverCache.remove(1L);
      driverCache.putIfAbsent(1L, "baz");
      driverCache.replace(1L, "bat");
      driverCache.replace(1L, "bat", "bag");
      driverCache.remove(1L, "bag");

      @SuppressWarnings("unchecked")
      Matcher<Iterable<? extends CacheEvent<? extends Long, ? extends String>>> expectedSequence = contains(
        created(1L, "foo"),
        updated(1L, "foo", "bar"),
        removed(1L, "bar"),
        created(1L, "baz"),
        updated(1L, "baz", "bat"),
        updated(1L, "bat", "bag"),
        removed(1L, "bag"));

      within(Duration.ofSeconds(10)).runsCleanly(() -> {
        assertThat(driverEvents, expectedSequence);
        assertThat(observerEvents, expectedSequence);
      });
    }
  }
}