@Override public void put(K key, V value, ExpiryPolicy expiryPolicy) { cache.put(key, value, expiryPolicy); }
@Override public V get(K k) { return cache.get(k); }
@Override ICompletableFuture storeAsync(K key, V value) { return (ICompletableFuture) cache.putAsync(key, value); } }
void assertSize() { int size = cache.size(); if (size > max) { max = size; } if (size > estimatedMaxSize) { fail(name + ": cache " + cache.getName() + " toleranceFactor=" + toleranceFactor + " configuredMaxSize=" + configuredMaxSize + " estimatedMaxSize=" + estimatedMaxSize + " size=" + cache.size() ); } } }
cache.put("key1", "value1"); System.out.println("Put value \"value1\" with key \"key1\""); System.out.println("Get value with key \"key1\": " + cache.get("key1")); + cache.getAndPut("key1", "value2")); System.out.println("Size of cache: " + cache.size()); System.out.println("Remove from cache with key \"key1\": " + cache.remove("key1")); cache.put("key1", "value3"); System.out.println("Put value \"value3\" with key \"key1\""); + cache.remove("key1", "xx")); + cache.remove("key1", "value3")); System.out.println("Get value with key \"key1\": " + cache.get("key1")); + cache.putIfAbsent("key1", "value1")); + cache.putIfAbsent("key1", "value1")); + cache.getAndRemove("key1")); System.out.println("Get value with key \"key1\": " + cache.get("key1")); cache.put("key1", "value1");
cache.put(key, getRandom().nextLong()); counter.put++; break; cache.put(key, getRandom().nextLong(), expiryPolicy); counter.putExpiry++; break; cache.putAsync(key, getRandom().nextLong(), expiryPolicy); counter.putAsyncExpiry++; break; cache.get(key, expiryPolicy); counter.getExpiry++; break; Future<Long> future = cache.getAsync(key, expiryPolicy); future.get(); counter.getAsyncExpiry++; if (cache.remove(key)) { counter.remove++; if (cache.replace(key, getRandom().nextLong())) { counter.replace++;
CacheConfig cacheConfig = cache.getConfiguration(CacheConfig.class); cache.put(i, i); cache.get(i); cache.get(i); cache.get(i); cache.put(i, i); System.out.println("cache size: " + cache.size()); if (!cache.containsKey(i)) { System.out.println("cache does not contains key: " + i);
public static void main(String[] args) { // Run as a Hazelcast Client System.setProperty("hazelcast.jcache.provider.type", "client"); // Create the JCache CacheManager CacheManager manager = Caching.getCachingProvider().getCacheManager(); MutableConfiguration<String, String> configuration = new MutableConfiguration<String, String>(); // Expire entries after 1 minute configuration.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(Duration.ONE_MINUTE)); // Get a Cache called "myCache" and configure with 1 minute expiry Cache<String, String> myCache = manager.createCache("myCache", configuration); // Put and Get a value from "myCache" myCache.put("key", "value"); String value = myCache.get("key"); System.out.println(value); //ICache is a Hazelcast interface that extends JCache, provides more functionality ICache<String, String> icache = myCache.unwrap(ICache.class); //Async Get and Put using ICache interface icache.getAsync("key"); icache.putAsync("key", "value"); //ICache allows custom expiry per cache entry final ExpiryPolicy customExpiryPolicy = AccessedExpiryPolicy.factoryOf(Duration.TEN_MINUTES).create(); icache.put("key", "newValue", customExpiryPolicy); //Size of the Cache should reflect the ICache and JCache operations icache.size(); //Shutdown this Hazelcast Client manager.getCachingProvider().close(); } }
cache.put("key", "value1", ttlToExpiryPolicy(1, TimeUnit.SECONDS)); System.out.println("Put value \"value1\" with key \"key\" and with TTL 1 seconds"); System.out.println("Get value with key \"key1\": " + cache.get("key")); System.out.println("Size of cache: " + cache.size()); System.out.println("Get value with key \"key1\": " + cache.get("key")); System.out.println("Size of cache: " + cache.size()); cache.put("key", "value1", new CreatedExpiryPolicy(Duration.ZERO)); System.out.println("Put value \"value1\" with key \"key\" and with zero expiry duration"); System.out.println("Get value with key \"key1\": " + cache.get("key")); System.out.println("Size of cache: " + cache.size()); cache.put("key", "value1", new CreatedExpiryPolicy(Duration.ETERNAL)); System.out.println("Put value \"value1\" with key \"key\" and with eternal expiry duration"); System.out.println("Get value with key \"key1\": " + cache.get("key")); System.out.println("Size of cache: " + cache.size()); System.out.println("Get value with key \"key1\": " + cache.get("key")); System.out.println("Size of cache: " + cache.size());
CacheConfig cacheConfig = cache.getConfiguration(CacheConfig.class); Iterable<CacheEntryListenerConfiguration> cacheEntryListenerConfigurations = cacheConfig.getCacheEntryListenerConfigurations(); cache.put("key", "value"); cache.put("key", "value1"); cache.remove("key"); cache.put("key", "value2", new HazelcastExpiryPolicy(1000, Long.MAX_VALUE, Long.MAX_VALUE)); sleepSeconds(2); System.out.println("value should be null: " + cache.get("key"));
public void run() { NearCacheConfig nearCacheConfig = createNearCacheConfig() .setInMemoryFormat(InMemoryFormat.OBJECT) .setCacheLocalEntries(true) .setInvalidateOnChange(false) .setEvictionConfig(createEvictionConfigWithEntryCountPolicy(RECORD_COUNT * 2)); ICache<Integer, Article> cache = createCacheWithNearCache(nearCacheConfig); Article article = new Article("foo"); cache.put(1, article); // the first get() will populate the Near Cache Article firstGet = cache.get(1); // the second and third get() will be served from the Near Cache Article secondGet = cache.get(1); Article thirdGet = cache.get(1); printNearCacheStats(cache); System.out.println("Since we use in-memory format BINARY, the article instances from the Near Cache will be different."); System.out.println("Compare first and second article instance: " + (firstGet == secondGet)); System.out.println("Compare second and third article instance: " + (secondGet == thirdGet)); HazelcastClient.shutdownAll(); Hazelcast.shutdownAll(); }
long start = System.nanoTime(); if (operation < getPercentage) { cache.get(key); stats.gets.incrementAndGet(); } else if (operation < getPercentage + putPercentage) { try { cache.put(key, values[random.nextInt(values.length)]); } catch (NativeOutOfMemoryError e) { System.err.println(e.getMessage()); cache.remove(key); stats.removes.incrementAndGet();
public static void main(String[] args) { init(); try { ICache<Integer, Integer> cache = createCache("MyCacheForIteratorUsage"); for (int i = 0; i < SIZE; i++) { cache.put(i, i * i); } Iterator<Cache.Entry<Integer, Integer>> iter = cache.iterator(); while (iter.hasNext()) { Cache.Entry<Integer, Integer> e = iter.next(); int key = e.getKey(); int value = e.getValue(); System.out.println("Key: " + key + ", Value: " + value); } cache.destroy(); } catch (Throwable t) { t.printStackTrace(); } finally { destroy(); } } }
@TimeStep public void timeStep(BaseThreadState state) { int key = state.randomInt(keyCount); if (!cache.containsKey(key)) { cache.put(key, 0, expiryPolicy); } }
@Verify public void globalVerify() { Counter totalCounter = new Counter(); for (Counter counter : results) { totalCounter.add(counter); } logger.info(name + " " + totalCounter + " from " + results.size() + " worker Threads"); for (int i = 0; i < keyCount; i++) { assertFalse(name + " ICache should not contain key " + i, cache.containsKey(i)); } assertFalse(name + " ICache iterator should not have elements", cache.iterator().hasNext()); assertEquals(name + " ICache size should be 0", 0, cache.size()); } }
@Override public boolean remove(K k) { return cache.remove(k); }
cache.put(i, "Value of " + i); Iterator<Cache.Entry<Integer, String>> iter1 = cache.iterator(); while (iter1.hasNext()) { Cache.Entry<Integer, String> entry = iter1.next(); Iterator<Cache.Entry<Integer, String>> iter2 = cache.iterator(25); while (iter2.hasNext()) { Cache.Entry<Integer, String> entry = iter2.next();
@Override public int size() { return cache.size(); }
private void populateLocalCacheEntrySet(ICache<K, T> cache) { cacheEntryList = new ConcurrentSkipListSet<>(); Iterator<Cache.Entry<K, T>> cacheEntryIterator = cache.iterator(); while (cacheEntryIterator.hasNext()) { cacheEntryList.add(cacheEntryIterator.next().getKey()); } cacheConfig = cache.getConfiguration(CacheConfig.class); LOG.info("Populated local cache entry set with respect to remote cache provider."); }
@Override public ICompletableFuture<V> getAsync(K key) { return cache.getAsync(key); }