class VersionedOffHeapCacheExample {

  public static void example() {
    OffHeapByteBufferStore bufferStore = new OffHeapByteBufferStore(10000, 10);
    final Cache<String, VersionedItem<String>> cache = CacheBuilder.versionedOffHeapCache()
        .storage(bufferStore)
        .build();
    ExecutorService service = Executors.newFixedThreadPool(3);
    for (int i = 0; i < 100000; i++) {
      service.execute(() -> {
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        while (true) {
          try {
            VersionedItem<String> value = cache.get("apple");
            String newValue = getRandomString();
            if (value == null) {
              cache.put("apple", new SimpleItem<>(newValue));
            } else {
              value = value.update(newValue);
              cache.put("apple", value);
            }
            System.out.println(cache.get("apple").getValue());
            break;
          } catch (StaleItemException ex) {
            ex.printStackTrace();
          }
        }
        VersionedItem<String> item = cache.get("apple");
        System.out.println(item.getVersion());
      });
    }
  }

}