Java Code Examples for javax.cache.Cache#put()
The following examples show how to use
javax.cache.Cache#put() .
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: JCacheTest.java From redisson with Apache License 2.0 | 6 votes |
@Test public void testJson() throws InterruptedException, IllegalArgumentException, URISyntaxException, IOException { RedisProcess runner = new RedisRunner() .nosave() .randomDir() .port(6311) .run(); URL configUrl = getClass().getResource("redisson-jcache.json"); Config cfg = Config.fromJSON(configUrl); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new JavaTimeModule()); cfg.setCodec(new TypedJsonJacksonCodec(String.class, LocalDateTime.class, objectMapper)); Configuration<String, LocalDateTime> config = RedissonConfiguration.fromConfig(cfg); Cache<String, LocalDateTime> cache = Caching.getCachingProvider().getCacheManager() .createCache("test", config); LocalDateTime t = LocalDateTime.now(); cache.put("1", t); Assert.assertEquals(t, cache.get("1")); cache.close(); runner.stop(); }
Example 2
Source File: PersistenceBasicCompatibilityTest.java From ignite with Apache License 2.0 | 6 votes |
/** * @param cache to be filled by different keys and values. Results may be validated in {@link * #validateResultingCacheData(Cache)}. */ public static void saveCacheData(Cache<Object, Object> cache) { for (int i = 0; i < 10; i++) cache.put(i, "data" + i); cache.put("1", "2"); cache.put(12, 2); cache.put(13L, 2L); cache.put(TestEnum.A, "Enum_As_Key"); cache.put("Enum_As_Value", TestEnum.B); cache.put(TestEnum.C, TestEnum.C); cache.put("Serializable", new TestSerializable(42)); cache.put(new TestSerializable(42), "Serializable_As_Key"); cache.put("Externalizable", new TestExternalizable(42)); cache.put(new TestExternalizable(42), "Externalizable_As_Key"); cache.put("testStringContainer", new TestStringContainerToBePrinted("testStringContainer")); cache.put(UUID.fromString("DA9E0049-468C-4680-BF85-D5379164FDCC"), UUID.fromString("B851B870-3BA7-4E5F-BDB8-458B42300000")); }
Example 3
Source File: JSRExamplesTest.java From blazingcache with Apache License 2.0 | 6 votes |
@Test public void testJSRExample1() { CachingProvider cachingProvider = Caching.getCachingProvider(); Properties p = new Properties(); try (CacheManager cacheManager = cachingProvider.getCacheManager(cachingProvider.getDefaultURI(), cachingProvider.getDefaultClassLoader(), p)) { MutableConfiguration<String, Integer> config = new MutableConfiguration<String, Integer>() .setTypes(String.class, Integer.class) .setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(ONE_HOUR)) .setStatisticsEnabled(true); Cache<String, Integer> cache = cacheManager.createCache("simpleCache", config); String key = "key"; Integer value1 = 1; cache.put("key", value1); Integer value2 = cache.get(key); assertEquals(value1, value2); cache.remove(key); assertNull(cache.get(key)); } }
Example 4
Source File: BaseCache.java From carbon-identity-framework with Apache License 2.0 | 6 votes |
/** * Add a cache entry. * * @param key Key which cache entry is indexed. * @param entry Actual object where cache entry is placed. */ public void addToCache(K key, V entry) { if (!isEnabled()) { return; } try { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext .getThreadLocalCarbonContext(); carbonContext.setTenantId(MultitenantConstants.SUPER_TENANT_ID); carbonContext.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); // Element already in the cache. Remove it first Cache<K, V> cache = getBaseCache(); if (cache != null) { cache.put(key, entry); } } finally { PrivilegedCarbonContext.endTenantFlow(); } }
Example 5
Source File: CacheExpiryTest.java From cache2k with Apache License 2.0 | 6 votes |
@Test public void testCacheStatisticsRemoveAll() throws Exception { long _EXPIRY_MILLIS = 3; //cannot be zero or will not be added to the cache ExpiryPolicy policy = new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, _EXPIRY_MILLIS)); expiryPolicyServer.setExpiryPolicy(policy); MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>(); config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient)).setStatisticsEnabled(true); Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config); for (int i = 0; i < 100; i++) { cache.put(i, i+100); } //should work with all implementations Thread.sleep(_EXPIRY_MILLIS); cache.removeAll(); assertEquals(100L, lookupManagementAttribute(cache, CacheStatistics, "CachePuts")); //Removals does not count expired entries assertEquals(0L, lookupManagementAttribute(cache, CacheStatistics, "CacheRemovals")); }
Example 6
Source File: RealmCache.java From micro-integrator with Apache License 2.0 | 5 votes |
/** * Add a cache entry. * * @param key Key which cache entry is indexed. * @param entry Actual object where cache entry is placed. */ public void addToCache(RealmCacheKey key, RealmCacheEntry entry) { Cache<RealmCacheKey, RealmCacheEntry> cache = getRealmCache(); if (cache.containsKey(key)) { // Element already in the cache. Remove it first cache.remove(key); } cache.put(key, entry); }
Example 7
Source File: IteratorTest.java From blazingcache with Apache License 2.0 | 5 votes |
@Test public void testConditionalReplace() throws Exception { CachingProvider cachingProvider = Caching.getCachingProvider(); Properties p = new Properties(); try (CacheManager cacheManager = cachingProvider.getCacheManager(cachingProvider.getDefaultURI(), cachingProvider.getDefaultClassLoader(), p)) { Cache<Long, String> cache = cacheManager.createCache("test", new MutableConfiguration().setTypes(Long.class, String.class)); BlazingCacheCache<Long, String> blazingCache = cache.unwrap(BlazingCacheCache.class); long hitCount = 0; long missCount = 0; long putCount = 0; boolean result = cache.replace(1L, "MissingNoReplace", "NewValue"); missCount++; assertFalse(result); assertEquals((int) missCount, (int) blazingCache.getStatisticsMXBean().getCacheMisses()); assertEquals((int) hitCount, (int) blazingCache.getStatisticsMXBean().getCacheHits()); assertEquals((int) putCount, (int) blazingCache.getStatisticsMXBean().getCachePuts()); assertFalse(cache.containsKey(1L)); cache.put(1l, "Sooty"); putCount++; assertEquals((int) putCount, (int) blazingCache.getStatisticsMXBean().getCachePuts()); assertTrue(cache.containsKey(1L)); result = cache.replace(1L, "Sooty", "Replaced"); hitCount++; putCount++; assertTrue(result); assertEquals((int) missCount, (int) blazingCache.getStatisticsMXBean().getCacheMisses()); assertEquals((int) hitCount, (int) blazingCache.getStatisticsMXBean().getCacheHits()); assertEquals((int) putCount, (int) blazingCache.getStatisticsMXBean().getCachePuts()); result = cache.replace(1L, "Sooty", "InvalidReplace"); hitCount++; assertFalse(result); assertEquals((int) missCount, (int) blazingCache.getStatisticsMXBean().getCacheMisses()); assertEquals((int) hitCount, (int) blazingCache.getStatisticsMXBean().getCacheHits()); assertEquals((int) putCount, (int) blazingCache.getStatisticsMXBean().getCachePuts()); } }
Example 8
Source File: TypesTest.java From cache2k with Apache License 2.0 | 5 votes |
/** * What happens when you: * * 1) declare using generics and * 2) don't specify types during configuration. */ @Test public void simpleAPIWithGenericsAndNoTypeEnforcement() { MutableConfiguration config = new MutableConfiguration<String, Integer>(); Cache<Identifier, Dog> cache = cacheManager.createCache(cacheName, config); //Types are restricted //Cannot put in wrong types //cache.put(1, "something"); //can put in cache.put(pistachio.getName(), pistachio); cache.put(tonto.getName(), tonto); //cannot get out wrong key types //assertNotNull(cache.get(1)); assertNotNull(cache.get(pistachio.getName())); assertNotNull(cache.get(tonto.getName())); //cannot remove wrong key types //assertTrue(cache.remove(1)); assertTrue(cache.remove(pistachio.getName())); assertTrue(cache.remove(tonto.getName())); }
Example 9
Source File: TCKCacheManagerTest.java From blazingcache with Apache License 2.0 | 5 votes |
@Test public void containsKeyShouldNotCallExpiryPolicyMethods() { CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy(); MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>(); config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicy)); Cache<Integer, Integer> cache = getCacheManager().createCache("test", config); cache.containsKey(1); assertThat(expiryPolicy.getCreationCount(), is(0)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); cache.put(1, 1); assertTrue(expiryPolicy.getCreationCount() >= 1); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); expiryPolicy.resetCount(); cache.containsKey(1); assertThat(expiryPolicy.getCreationCount(), is(0)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); }
Example 10
Source File: ClaimInvalidationCache.java From micro-integrator with Apache License 2.0 | 5 votes |
/** * Add a cache entry. * * @param key Key which cache entry is indexed. * @param entry Actual object where cache entry is placed. */ private void addToCache(String key, Integer entry) { // Element already in the cache. Remove it first clearCacheEntry(key); Cache<String, Integer> cache = getClaimCache(); if (cache != null) { cache.put(key, entry); } }
Example 11
Source File: TypesTest.java From cache2k with Apache License 2.0 | 5 votes |
/** * What happens when you: * * 1) declare using generics with a super class * 2) declare using configuration with a sub class * * Set generics to Identifier and Dog * Bypass generics with a raw MutableConfiguration but set runtime to Identifier and Hound * * The configuration checking gets done on put. */ @Test public void genericsEnforcementAndStricterTypeEnforcement() { //configure the cache MutableConfiguration config = new MutableConfiguration<>(); config.setTypes(Identifier.class, Hound.class); Cache<Identifier, Dog> cache = cacheManager.createCache(cacheName, config); //Types are restricted and types are enforced //Cannot put in wrong types //cache.put(1, "something"); //can put in cache.put(pistachio.getName(), pistachio); //can put in with generics but possibly not with configuration as not a hound try { cache.put(tonto.getName(), tonto); } catch (ClassCastException e) { //expected but not mandatory. The RI throws these. } //cannot get out wrong key types //assertNotNull(cache.get(1)); assertNotNull(cache.get(pistachio.getName())); //not necessarily //assertNotNull(cache.get(tonto.getName())); //cannot remove wrong key types //assertTrue(cache.remove(1)); assertTrue(cache.remove(pistachio.getName())); //not necessarily //assertTrue(cache.remove(tonto.getName())); }
Example 12
Source File: JCacheTest.java From redisson with Apache License 2.0 | 5 votes |
@Test public void testRemoveAll() throws Exception { RedisProcess runner = new RedisRunner() .nosave() .randomDir() .port(6311) .run(); URL configUrl = getClass().getResource("redisson-jcache.json"); Config cfg = Config.fromJSON(configUrl); Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg); Cache<String, String> cache = Caching.getCachingProvider().getCacheManager() .createCache("test", config); cache.put("1", "2"); cache.put("3", "4"); cache.put("4", "4"); cache.put("5", "5"); Set<? extends String> keys = new HashSet<String>(Arrays.asList("1", "3", "4", "5")); cache.removeAll(keys); assertThat(cache.containsKey("1")).isFalse(); assertThat(cache.containsKey("3")).isFalse(); assertThat(cache.containsKey("4")).isFalse(); assertThat(cache.containsKey("5")).isFalse(); cache.close(); runner.stop(); }
Example 13
Source File: CacheExpiryTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void removeSpecifiedEntryShouldNotCallExpiryPolicyMethods() { CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy(); expiryPolicyServer.setExpiryPolicy(expiryPolicy); MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>(); config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient)); Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config); boolean result = cache.remove(1, 1); assertThat(expiryPolicy.getCreationCount(), is(0)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); cache.put(1, 1); assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); expiryPolicy.resetCount(); result = cache.remove(1, 2); assertFalse(result); assertThat(expiryPolicy.getCreationCount(), is(0)); assertThat(expiryPolicy.getAccessCount(), is(1)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); expiryPolicy.resetCount(); result = cache.remove(1, 1); assertTrue(result); assertThat(expiryPolicy.getCreationCount(), is(0)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); }
Example 14
Source File: Eh107XmlIntegrationTest.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void testCopierAtServiceLevel() throws Exception { CacheManager cacheManager = cachingProvider.getCacheManager( getClass().getResource("/ehcache-107-default-copiers.xml").toURI(), getClass().getClassLoader()); MutableConfiguration<Long, Client> config = new MutableConfiguration<>(); config.setTypes(Long.class, Client.class); Cache<Long, Client> bar = cacheManager.createCache("bar", config); Client client = new Client("tc", 1000000L); long key = 42L; bar.put(key, client); assertThat(bar.get(key), not(sameInstance(client))); }
Example 15
Source File: CacheExpiryTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void containsKeyShouldNotCallExpiryPolicyMethods() { CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy(); expiryPolicyServer.setExpiryPolicy(expiryPolicy); MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>(); config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient)); Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config); cache.containsKey(1); assertThat(expiryPolicy.getCreationCount(), is(0)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); cache.put(1, 1); assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); expiryPolicy.resetCount(); cache.containsKey(1); assertThat(expiryPolicy.getCreationCount(), is(0)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); closeTestCache(); }
Example 16
Source File: TypesTest.java From cache2k with Apache License 2.0 | 5 votes |
/** * Same as above but using the shorthand Caching to acquire. * Should work the same. */ @Test public void genericsEnforcementAndStricterTypeEnforcementFromCaching() { //configure the cache MutableConfiguration config = new MutableConfiguration<>(); config.setTypes(Identifier.class, Hound.class); Cache<Identifier, Dog> cache = cacheManager.createCache(cacheName, config); //Types are restricted and types are enforced //Cannot put in wrong types //cache.put(1, "something"); //can put in cache.put(pistachio.getName(), pistachio); //can put in with generics but possibly not with configuration as not a hound try { cache.put(tonto.getName(), tonto); } catch (ClassCastException e) { //expected but not mandatory. The RI throws these. } //cannot get out wrong key types //assertNotNull(cache.get(1)); assertNotNull(cache.get(pistachio.getName())); //not necessarily //assertNotNull(cache.get(tonto.getName())); //cannot remove wrong key types //assertTrue(cache.remove(1)); assertTrue(cache.remove(pistachio.getName())); //not necessarily //assertTrue(cache.remove(tonto.getName())); }
Example 17
Source File: CacheStarSchemaExample.java From ignite with Apache License 2.0 | 5 votes |
/** * Populate cache with {@code 'facts'}, which in our case are {@link FactPurchase} objects. * @param factCache Cache to populate. * * @throws IgniteException If failed. */ private static void populateFacts(Cache<Integer, FactPurchase> factCache) throws IgniteException { for (int i = 0; i < 100; i++) { int id = idGen++; DimStore store = rand(dataStore.values()); DimProduct prod = rand(dataProduct.values()); factCache.put(id, new FactPurchase(id, prod.getId(), store.getId(), (i + 1))); } }
Example 18
Source File: CacheExpiryTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void iteratorNextShouldCallGetExpiryForAccessedEntry() { CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy(); expiryPolicyServer.setExpiryPolicy(expiryPolicy); MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>(); config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient)); Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config); Set<Integer> keys = new HashSet<>(); keys.add(1); keys.add(2); cache.put(1, 1); cache.put(2, 2); assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(2)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); expiryPolicy.resetCount(); // when getting an existing entry, getExpiryForAccessedEntry is called. Iterator<Entry<Integer, Integer>> iter = cache.iterator(); int count = 0; while (iter.hasNext()) { Entry<Integer, Integer> entry = iter.next(); count++; assertThat(expiryPolicy.getCreationCount(), is(0)); assertThat(expiryPolicy.getAccessCount(), greaterThanOrEqualTo(1)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); expiryPolicy.resetCount(); } }
Example 19
Source File: ClaimInvalidationCache.java From micro-integrator with Apache License 2.0 | 5 votes |
/** * Update the cache entry without clearing. * * @param key Key which cache entry is indexed. * @param entry Actual object where cache entry is placed. */ private void updateCache(String key, Integer entry) { Cache<String, Integer> cache = getClaimCache(); if (cache != null) { cache.put(key, entry); } else { log.debug("Error while updating the claim cache. ClaimCache is null"); } }
Example 20
Source File: GridCacheDhtPreloadDisabledSelfTest.java From ignite with Apache License 2.0 | 4 votes |
/** * @param c Cache. * @param cnt Key count. */ private void putKeys(Cache<Integer, String> c, int cnt) { for (int i = 0; i < cnt; i++) c.put(i, Integer.toString(i)); }