@Test public void testMultiClusterMultipleClients() throws MalformedURLException, URISyntaxException { final String cacheName = "test"; final String key1 = "key1"; final String valuecm1 = "Value-is-cm1"; final String valuecm2 = "Value-is-cm2"; final HazelcastClientCachingProvider cachingProvider = new HazelcastClientCachingProvider(); final CacheManager cm1 = cachingProvider.getCacheManager(uri1, null); final CacheManager cm2 = cachingProvider.getCacheManager(uri2, null); final CacheConfig<String, String> cacheConfig = new CacheConfig<String, String>(); final Cache<String, String> cache1 = cm1.createCache(cacheName, cacheConfig); final Cache<String, String> cache2 = cm2.createCache(cacheName, cacheConfig); cache1.put(key1, valuecm1); cache2.put(key1, valuecm2); assertEquals(valuecm1, cache1.get(key1)); assertEquals(valuecm2, cache2.get(key1)); cachingProvider.close(uri1, null); cachingProvider.close(uri2, null); // cm1.close(); // cm2.close(); final CacheManager cm11 = cachingProvider.getCacheManager(uri1, null); final Cache<String, String> cache11 = cm11.getCache(cacheName); assertEquals(valuecm1, cache11.get(key1)); cm11.close(); }
@Test public void testTemplateOverridingStoreByValue() throws Exception { cacheManager = cachingProvider.getCacheManager( getClass() .getResource("/org/ehcache/docs/ehcache-jsr107-template-override.xml") .toURI(), getClass().getClassLoader()); MutableConfiguration<Long, String> mutableConfiguration = new MutableConfiguration<Long, String>(); mutableConfiguration.setTypes(Long.class, String.class); Cache<Long, String> myCache = null; myCache = cacheManager.createCache("anyCache", mutableConfiguration); myCache.put(1L, "foo"); assertNotSame("foo", myCache.get(1L)); assertTrue(myCache.getConfiguration(Configuration.class).isStoreByValue()); myCache = cacheManager.createCache("byRefCache", mutableConfiguration); myCache.put(1L, "foo"); assertSame("foo", myCache.get(1L)); assertFalse(myCache.getConfiguration(Configuration.class).isStoreByValue()); myCache = cacheManager.createCache("weirdCache1", mutableConfiguration); myCache.put(1L, "foo"); assertNotSame("foo", myCache.get(1L)); assertTrue(myCache.getConfiguration(Configuration.class).isStoreByValue()); myCache = cacheManager.createCache("weirdCache2", mutableConfiguration); myCache.put(1L, "foo"); assertSame("foo", myCache.get(1L)); assertFalse(myCache.getConfiguration(Configuration.class).isStoreByValue()); }
@Test public void testCacheLoader() { final CacheLoader<String, Integer> cacheLoader = new CacheLoader<String, Integer>() { @Override public Integer load(String key) throws CacheLoaderException { return Integer.valueOf(key); } @Override public Map<String, Integer> loadAll(Iterable<? extends String> keys) throws CacheLoaderException { Map<String, Integer> map = new HashMap<>(); for (String key : keys) { map.put(key, Integer.valueOf(key)); } return map; } }; try (CachingProvider cachingProvider = Caching.getCachingProvider(GuavaCachingProvider.class.getName())) { CacheManager cacheManager = cachingProvider.getCacheManager(); MutableConfiguration<String, Integer> custom = new MutableConfiguration<>(); custom.setStoreByValue(false); custom.setTypes(String.class, Integer.class); custom.setReadThrough(true); custom.setCacheLoaderFactory( new Factory<CacheLoader<String, Integer>>() { @Override public CacheLoader<String, Integer> create() { return cacheLoader; } }); Cache<String, Integer> loadingCache = cacheManager.createCache("loadingCache", custom); assertEquals(Integer.valueOf(1), loadingCache.get("1")); assertEquals(Integer.valueOf(2), loadingCache.get("2")); assertEquals(Integer.valueOf(3), loadingCache.get("3")); Set<String> keys = Sets.newHashSet("4", "5", "6"); Map<String, Integer> map = loadingCache.getAll(keys); assertEquals(3, map.size()); assertEquals(Integer.valueOf(4), map.get("4")); assertEquals(Integer.valueOf(5), map.get("5")); assertEquals(Integer.valueOf(6), map.get("6")); } }
@Test public void testHigherHitsCacheMergePolicy() { String cacheName = randomMapName(); Config config = newConfig(); HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config); HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config); TestMemberShipListener memberShipListener = new TestMemberShipListener(1); h2.getCluster().addMembershipListener(memberShipListener); TestLifeCycleListener lifeCycleListener = new TestLifeCycleListener(1); h2.getLifecycleService().addLifecycleListener(lifeCycleListener); closeConnectionBetween(h1, h2); assertOpenEventually(memberShipListener.latch); assertClusterSizeEventually(1, h1); assertClusterSizeEventually(1, h2); CachingProvider cachingProvider1 = HazelcastServerCachingProvider.createCachingProvider(h1); CachingProvider cachingProvider2 = HazelcastServerCachingProvider.createCachingProvider(h2); CacheManager cacheManager1 = cachingProvider1.getCacheManager(); CacheManager cacheManager2 = cachingProvider2.getCacheManager(); CacheConfig cacheConfig = newCacheConfig(cacheName, HigherHitsCacheMergePolicy.class.getName()); Cache cache1 = cacheManager1.createCache(cacheName, cacheConfig); Cache cache2 = cacheManager2.createCache(cacheName, cacheConfig); // TODO We assume that until here and also while doing get/put, cluster is still splitted. // This assumptions seems fragile due to time sensitivity. cache1.put("key1", "higherHitsValue"); cache1.put("key2", "value2"); // Increase hits number assertEquals("higherHitsValue", cache1.get("key1")); assertEquals("higherHitsValue", cache1.get("key1")); cache2.put("key1", "value1"); cache2.put("key2", "higherHitsValue2"); // Increase hits number assertEquals("higherHitsValue2", cache2.get("key2")); assertEquals("higherHitsValue2", cache2.get("key2")); assertOpenEventually(lifeCycleListener.latch); assertClusterSizeEventually(2, h1); assertClusterSizeEventually(2, h2); Cache cacheTest = cacheManager2.getCache(cacheName); assertEquals("higherHitsValue", cacheTest.get("key1")); assertEquals("higherHitsValue2", cacheTest.get("key2")); }
@Test public void testJCacheGettingStarted() { try (CachingProvider provider = Caching.getCachingProvider(); CacheManager manager = provider.getCacheManager(); Cache<String, String> cache = manager.createCache("testCache", new MutableConfiguration<>())) { cache.put("key", "value"); assertThat(cache.get("key")).isEqualTo("value"); cache.remove("key"); assertThat(cache.get("key")).isNull(); } }
/** * Loads the session from the backing store. * * @param session the session to load. * @param now current time in milliseconds. now == 0 implies that we're just checking for the * existence of such a session in the cache and do not intend actually to load it if it is * not. */ protected boolean load(Env env, SessionArrayValue session, long now) { try { if (session.inUse()) { return true; } else if (now <= 0) { return false; } if (_persistentStore != null) { String encoded = (String) _persistentStore.get(session.getId()); if (encoded != null) { session.decode(env, new StringBuilderValue(encoded)); } } if (session.load()) { session.setAccess(now); return true; } else { session.reset(now); } } catch (Exception e) { log.log(Level.FINE, e.toString(), e); session.reset(now); } return false; }
public static String getCachedValue(String jsonCacheName, String ckey) { Cache cache = CacheController.getCache(); Map<String, String> thisCache = (HashMap<String, String>) cache.get(jsonCacheName); if (thisCache == null) { return null; } return thisCache.get(ckey); }
@Test public void testJSRExample1() throws InterruptedException { final String cacheName = randomString(); CacheManager cacheManager = cachingProvider1.getCacheManager(); assertNotNull(cacheManager); assertNull(cacheManager.getCache(cacheName)); CacheConfig<Integer, String> config = new CacheConfig<Integer, String>(); Cache<Integer, String> cache = cacheManager.createCache(cacheName, config); assertNotNull(cache); assertTrueEventually( new AssertTask() { @Override public void run() throws Exception { CacheManager cm2 = cachingProvider2.getCacheManager(); assertNotNull(cm2.getCache(cacheName)); } }); Integer key = 1; String value1 = "value"; cache.put(key, value1); String value2 = cache.get(key); assertEquals(value1, value2); cache.remove(key); assertNull(cache.get(key)); Cache<Integer, String> cache2 = cacheManager.getCache(cacheName); assertNotNull(cache2); key = 1; value1 = "value"; cache.put(key, value1); value2 = cache.get(key); assertEquals(value1, value2); cache.remove(key); assertNull(cache.get(key)); cacheManager.destroyCache(cacheName); cacheManager.close(); }
@Test public void testCustomCacheMergePolicy() { String cacheName = randomMapName(); Config config = newConfig(); HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config); HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config); TestMemberShipListener memberShipListener = new TestMemberShipListener(1); h2.getCluster().addMembershipListener(memberShipListener); TestLifeCycleListener lifeCycleListener = new TestLifeCycleListener(1); h2.getLifecycleService().addLifecycleListener(lifeCycleListener); closeConnectionBetween(h1, h2); assertOpenEventually(memberShipListener.latch); assertClusterSizeEventually(1, h1); assertClusterSizeEventually(1, h2); CachingProvider cachingProvider1 = HazelcastServerCachingProvider.createCachingProvider(h1); CachingProvider cachingProvider2 = HazelcastServerCachingProvider.createCachingProvider(h2); CacheManager cacheManager1 = cachingProvider1.getCacheManager(); CacheManager cacheManager2 = cachingProvider2.getCacheManager(); CacheConfig cacheConfig = newCacheConfig(cacheName, CustomCacheMergePolicy.class.getName()); Cache cache1 = cacheManager1.createCache(cacheName, cacheConfig); Cache cache2 = cacheManager2.createCache(cacheName, cacheConfig); // TODO We assume that until here and also while doing get/put, cluster is still splitted. // This assumptions seems fragile due to time sensitivity. String key = generateKeyOwnedBy(h1); cache1.put(key, "value"); cache2.put(key, Integer.valueOf(1)); assertOpenEventually(lifeCycleListener.latch); assertClusterSizeEventually(2, h1); assertClusterSizeEventually(2, h2); Cache cacheTest = cacheManager2.getCache(cacheName); assertNotNull(cacheTest.get(key)); assertTrue(cacheTest.get(key) instanceof Integer); }
public static void updateCacheValue(String jsonCacheName, String ckey, String results) { Cache cache = CacheController.getCache(); Map<String, String> thisCache = (HashMap<String, String>) cache.get(jsonCacheName); if (thisCache == null) { thisCache = new HashMap<String, String>(); } thisCache.put(ckey, results); cache.put(jsonCacheName, thisCache); }
@Override public Object get(Object key) { Object value = map.get(key); if (value == null) { value = cache.get(key); map.put(key, value); } return value; }
public static void invalidateCacheValue(String jsonCacheName, String ckey) { Cache cache = CacheController.getCache(); Map<String, String> thisCache = (HashMap<String, String>) cache.get(jsonCacheName); if (thisCache != null) { thisCache.remove(ckey); } else { thisCache = new HashMap<String, String>(); } cache.put(jsonCacheName, thisCache); }
@SuppressWarnings("unchecked") @Override public Job find(Long scheduleId, Long id) { Object entityCacheKey = getEntityCacheKey(Job.class, getJobWideUniqueData(scheduleId, id)); Job result = (Job) cache.get(entityCacheKey); if (result != null) { return result; } result = super.find(scheduleId, id); if (result != null) { cache.put(entityCacheKey, result); } return result; }
@Test public void testCacheExpired() throws Exception { Configuration<String, String> configuration = new MutableConfiguration<String, String>() .setExpiryPolicyFactory( AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 3))); try (CachingProvider provider = Caching.getCachingProvider(); CacheManager manager = provider.getCacheManager(); Cache<String, String> cache = manager.createCache("testCache", configuration)) { cache.put("key1", "value1"); cache.put("key2", "value2"); TimeUnit.SECONDS.sleep(1); cache.get("key2"); TimeUnit.SECONDS.sleep(2); assertThat(cache.get("key1")).isNull(); assertThat(cache.get("key2")).isEqualTo("value2"); } }
@SuppressWarnings("unchecked") @Override public List<Key> getJobsByCronString(String cronString) { Object entityCacheKey = getEntityCacheKey(Job.class, cronString); List<Key> result = (List<Key>) cache.get(entityCacheKey); if (result != null) { return result; } result = super.getJobsByCronString(cronString); if (result != null) { ArrayList<Key> serializableList = new ArrayList<Key>(result.subList(0, result.size())); cache.put(entityCacheKey, serializableList); } return result; }
@Ignore @Test public void testWithHazelcastConfiguration() throws Exception { // wrong use!! ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); URI uri = classLoader.getResource("hazelcast-jcache.xml").toURI(); try (CachingProvider provider = Caching.getCachingProvider(); CacheManager manager = provider.getCacheManager(uri, classLoader); Cache<String, String> cache = manager.getCache("testCache")) { cache.put("key1", "value1"); cache.put("key2", "value2"); TimeUnit.SECONDS.sleep(1); cache.get("key2"); TimeUnit.SECONDS.sleep(2); assertThat(cache.get("key1")).isNull(); assertThat(cache.get("key2")).isEqualTo("value2"); } }
@SuppressWarnings("unchecked") private void updateJobInScheduleCache(Job job) { Long scheduleId = job.getKey().getParent().getId(); Object entityCacheKey = getEntityCacheKey(Schedule.class, scheduleId); Schedule schedule = (Schedule) cache.get(entityCacheKey); if (schedule == null) { return; // Nothing to update } schedule.updateJob(job); cache.put(entityCacheKey, schedule); }
protected List<ValueEval> fetchValuesWithOptimisations( DsLookupParameters parameters, OperationEvaluationContext ec) { DataSetOptimisationsCache caches = (DataSetOptimisationsCache) ec.getCustomEvaluationContext().get(DataSetOptimisationsCache.class); if (caches == null) { caches = this.external.getDataSetOptimisationsCache(); } Cache<DsLookupParameters, List> cache = caches.getDataSetToDsLookupParameters(); if (cache.containsKey(parameters)) { return cache.get(parameters); } return null; }
public Object get(Object arg0) { try { if (localCache.containsKey(arg0)) { localHits++; return localCache.get(arg0); } Object value = cache.get(arg0); if (value != null) { cacheHits++; localCache.put((String) arg0, value); } return value; } catch (Exception e) { LOGGER.error(e); clear(); } return null; }
@Test public void testPassThroughCacheMergePolicy() { String cacheName = randomMapName(); Config config = newConfig(); HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config); HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config); TestMemberShipListener memberShipListener = new TestMemberShipListener(1); h2.getCluster().addMembershipListener(memberShipListener); TestLifeCycleListener lifeCycleListener = new TestLifeCycleListener(1); h2.getLifecycleService().addLifecycleListener(lifeCycleListener); closeConnectionBetween(h1, h2); assertOpenEventually(memberShipListener.latch); assertClusterSizeEventually(1, h1); assertClusterSizeEventually(1, h2); CachingProvider cachingProvider1 = HazelcastServerCachingProvider.createCachingProvider(h1); CachingProvider cachingProvider2 = HazelcastServerCachingProvider.createCachingProvider(h2); CacheManager cacheManager1 = cachingProvider1.getCacheManager(); CacheManager cacheManager2 = cachingProvider2.getCacheManager(); CacheConfig cacheConfig = newCacheConfig(cacheName, PassThroughCacheMergePolicy.class.getName()); Cache cache1 = cacheManager1.createCache(cacheName, cacheConfig); Cache cache2 = cacheManager2.createCache(cacheName, cacheConfig); String key = generateKeyOwnedBy(h1); cache1.put(key, "value"); cache2.put(key, "passThroughValue"); assertOpenEventually(lifeCycleListener.latch); assertClusterSizeEventually(2, h1); assertClusterSizeEventually(2, h2); Cache cacheTest = cacheManager2.getCache(cacheName); assertEquals("passThroughValue", cacheTest.get(key)); }
@Verify public void verify() { long[] amount = new long[keyCount]; for (Map<Integer, Long> map : resultsPerWorker) { for (Map.Entry<Integer, Long> entry : map.entrySet()) { amount[entry.getKey()] += entry.getValue(); } } int failures = 0; for (int i = 0; i < keyCount; i++) { long expected = amount[i]; long found = cache.get(i); if (expected != found) { failures++; } } assertEquals("Failures have been found", 0, failures); }
/** * Get the API key validated against the specified API * * @param context API context * @param apiKey API key to be validated * @param apiVersion API version number * @return An APIKeyValidationInfoDTO object * @throws APISecurityException If an error occurs while accessing backend services */ public APIKeyValidationInfoDTO getKeyValidationInfo( String context, String apiKey, String apiVersion, String authenticationScheme, String clientDomain) throws APISecurityException { String cacheKey = apiKey + ":" + context + ":" + apiVersion + ":" + authenticationScheme; if (isGatewayAPIKeyValidationEnabled) { APIKeyValidationInfoDTO info = (APIKeyValidationInfoDTO) infoCache.get(cacheKey); if (info != null) { return info; } } // synchronized (apiKey.intern()) { // We synchronize on the API key here to allow concurrent processing // of different API keys - However when a burst of requests with the // same key is encountered, only one will be allowed to execute the logic, // and the rest will pick the value from the cache. // info = (APIKeyValidationInfoDTO) infoCache.get(cacheKey); // if (info != null) { // return info; // } APIKeyValidationInfoDTO info = doGetKeyValidationInfo(context, apiVersion, apiKey, authenticationScheme, clientDomain); if (info != null) { if (isGatewayAPIKeyValidationEnabled && clientDomain == null) { // save into cache only if, validation is correct and api is allowed for // all domains infoCache.put(cacheKey, info); } return info; } else { throw new APISecurityException( APISecurityConstants.API_AUTH_GENERAL_ERROR, "API key validator returned null"); } // } }
@SuppressWarnings("unchecked") @Override public void update(Job job, boolean commitAfter) { super.update(job, commitAfter); Object entityCacheKey = getEntityCacheKey(Job.class, getJobWideUniqueData(job.getKey())); Job cachedJob = (Job) cache.get(entityCacheKey); if (cachedJob != null) { if (!cachedJob.getCronString().equals(job.getCronString())) { abandonJobsByCronStringCache(cachedJob.getCronString()); abandonJobsByCronStringCache(job.getCronString()); } cache.put(entityCacheKey, job); } else { abandonJobsByCronStringCache(); } updateJobInScheduleCache(job); }
public String getResourceAuthenticationScheme( String context, String apiVersion, String requestPath, String httpMethod) { String cacheKey = context + ":" + apiVersion; APIInfoDTO apiInfoDTO = null; if (isGatewayAPIKeyValidationEnabled) { apiInfoDTO = (APIInfoDTO) resourceCache.get(cacheKey); } if (apiInfoDTO == null) { apiInfoDTO = doGetAPIInfo(context, apiVersion); resourceCache.put(cacheKey, apiInfoDTO); } // Match the case where the direct api context is matched if ("/".equals(requestPath)) { String requestCacheKey = context + "/" + apiVersion + requestPath + ":" + httpMethod; // Get decision from cache. VerbInfoDTO matchingVerb = null; if (isGatewayAPIKeyValidationEnabled) { matchingVerb = (VerbInfoDTO) resourceCache.get(requestCacheKey); } // On a cache hit if (matchingVerb != null) { return matchingVerb.getAuthType(); } else { for (ResourceInfoDTO resourceInfoDTO : apiInfoDTO.getResources()) { String urlPattern = resourceInfoDTO.getUrlPattern(); // If the request patch is '/', it can only be matched with a resource whose url-context // is '/*' if ("/*".equals(urlPattern)) { for (VerbInfoDTO verbDTO : resourceInfoDTO.getHttpVerbs()) { if (verbDTO.getHttpVerb().equals(httpMethod)) { // Store verb in cache resourceCache.put(requestCacheKey, verbDTO); return verbDTO.getAuthType(); } } } } } } // Remove the ending '/' from request requestPath = RESTUtils.trimTrailingSlashes(requestPath); while (requestPath.length() > 1) { String requestCacheKey = context + "/" + apiVersion + requestPath + ":" + httpMethod; // Get decision from cache. VerbInfoDTO matchingVerb = null; if (isGatewayAPIKeyValidationEnabled) { matchingVerb = (VerbInfoDTO) resourceCache.get(requestCacheKey); } // On a cache hit if (matchingVerb != null) { return matchingVerb.getAuthType(); } // On a cache miss else { for (ResourceInfoDTO resourceInfoDTO : apiInfoDTO.getResources()) { String urlPattern = resourceInfoDTO.getUrlPattern(); if (urlPattern.endsWith("/*")) { // Remove the ending '/*' urlPattern = urlPattern.substring(0, urlPattern.length() - 2); } // If the urlPattern ends with a '/', remove that as well. urlPattern = RESTUtils.trimTrailingSlashes(urlPattern); if (requestPath.endsWith(urlPattern)) { for (VerbInfoDTO verbDTO : resourceInfoDTO.getHttpVerbs()) { if (verbDTO.getHttpVerb().equals(httpMethod)) { // Store verb in cache resourceCache.put(requestCacheKey, verbDTO); return verbDTO.getAuthType(); } } } } } // Remove the section after the last occurrence of the '/' character int index = requestPath.lastIndexOf("/"); requestPath = requestPath.substring(0, index <= 0 ? 0 : index); } // nothing found. return the highest level of security return APIConstants.NO_MATCHING_AUTH_SCHEME; }