@Override public @Nullable <K, V> Cache<K, V> getCache( String cacheName, Class<K> keyType, Class<V> valueType) { CacheProxy<K, V> cache = getCache(cacheName); if (cache == null) { return null; } requireNonNull(keyType); requireNonNull(valueType); Configuration<?, ?> config = cache.getConfiguration(); if (keyType != config.getKeyType()) { throw new ClassCastException("Incompatible cache key types specified, expected " + config.getKeyType() + " but " + keyType + " was specified"); } else if (valueType != config.getValueType()) { throw new ClassCastException("Incompatible cache value types specified, expected " + config.getValueType() + " but " + valueType + " was specified"); } return cache; }
/** {@inheritDoc} */ @Override public <K, V> Cache<K, V> getCache(String cacheName, Class<K> keyType, Class<V> valType) { kernalGateway.readLock(); try { Cache<K, V> cache = getCache0(cacheName); if (cache != null) { if(!keyType.isAssignableFrom(cache.getConfiguration(Configuration.class).getKeyType())) throw new ClassCastException(); if(!valType.isAssignableFrom(cache.getConfiguration(Configuration.class).getValueType())) throw new ClassCastException(); } return cache; } finally { kernalGateway.readUnlock(); } }
@SuppressWarnings("unchecked") @Override public <K, V> Cache<K, V> getCache(String cacheName, Class<K> keyType, Class<V> valueType) { checkClosed(); if (cacheName == null || keyType == null || valueType == null) { throw new NullPointerException(); } Eh107Cache<K, V> cache = safeCacheRetrieval(cacheName); if (cache == null) { return null; } Class<?> actualKeyType = cache.getConfiguration(Configuration.class).getKeyType(); Class<?> actualValueType = cache.getConfiguration(Configuration.class).getValueType(); if (keyType != actualKeyType) { throw new ClassCastException("Cache has key type " + actualKeyType.getName() + ", but getCache() called with key type " + keyType.getName()); } if (valueType != actualValueType) { throw new ClassCastException("Cache has value type " + actualValueType.getName() + ", but getCache() called with value type " + valueType.getName()); } return cache; }
@Test public void testTypeOverriding() throws Exception { CachingProvider provider = Caching.getCachingProvider(); javax.cache.CacheManager cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-107-types.xml").toURI(), getClass().getClassLoader()); MutableConfiguration<Long, String> cache1Conf = new MutableConfiguration<>(); cache1Conf.setTypes(Long.class, String.class); javax.cache.Cache<Long, String> cache = cacheManager.createCache("defaultCache", cache1Conf); @SuppressWarnings("unchecked") Configuration<Long, String> cache1CompleteConf = cache.getConfiguration(Configuration.class); assertThat(cache1CompleteConf.getKeyType(), is(equalTo(Long.class))); assertThat(cache1CompleteConf.getValueType(), is(equalTo(String.class))); }
@Test public void testRunTimeTypeLaxity() throws Exception { CachingProvider provider = Caching.getCachingProvider(); javax.cache.CacheManager cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-107-types.xml").toURI(), getClass().getClassLoader()); MutableConfiguration<Long, String> cache1Conf = new MutableConfiguration<>(); cache1Conf.setTypes(Long.class, String.class); javax.cache.Cache<Long, String> cache = cacheManager.createCache("cache1", cache1Conf); @SuppressWarnings("unchecked") Configuration<Long, String> cache1CompleteConf = cache.getConfiguration(Configuration.class); assertThat(cache1CompleteConf.getKeyType(), is(equalTo(Long.class))); assertThat(cache1CompleteConf.getValueType(), is(equalTo(String.class))); try { cacheManager.getCache("cache1"); } finally { cacheManager.destroyCache("cache1"); cacheManager.close(); } }
@Test @SuppressWarnings("unchecked") public void testCompileTimeTypeSafety() throws Exception { CachingProvider provider = Caching.getCachingProvider(); javax.cache.CacheManager cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-107-types.xml").toURI(), getClass().getClassLoader()); MutableConfiguration<Long, String> cache1Conf = new MutableConfiguration<>(); javax.cache.Cache<Long, String> cache = cacheManager.createCache("cache1", cache1Conf); cache.put(1l, "one"); cache.put(2l, "two"); Configuration<Object, Object> cache1CompleteConf = cache.getConfiguration(Configuration.class); //This ensures that we have compile time type safety, i.e when configuration does not have types defined but // what you get cache as should work. assertThat(cache1CompleteConf.getKeyType(), is(equalTo(Object.class))); assertThat(cache1CompleteConf.getValueType(), is(equalTo(Object.class))); assertThat(cache.get(1l), is(equalTo("one"))); assertThat(cache.get(2l), is(equalTo("two"))); javax.cache.Cache<String, String> second = cacheManager.getCache("cache1"); second.put("3","three"); assertThat(second.get("3"), is(equalTo("three"))); cacheManager.destroyCache("cache1"); cacheManager.close(); }
@SuppressWarnings("unchecked") @Test public void testXmlExampleIn107() throws Exception { javax.cache.Cache<Long, Product> productCache = cacheManager.getCache("productCache", Long.class, Product.class); assertThat(productCache, is(notNullValue())); Configuration<Long, Product> configuration = productCache.getConfiguration(Configuration.class); assertThat(configuration.getKeyType(), is(equalTo(Long.class))); assertThat(configuration.getValueType(), is(equalTo(Product.class))); Eh107ReverseConfiguration<Long, Product> eh107ReverseConfiguration = productCache.getConfiguration(Eh107ReverseConfiguration.class); assertThat(eh107ReverseConfiguration.isReadThrough(), is(true)); assertThat(eh107ReverseConfiguration.isWriteThrough(), is(true)); assertThat(eh107ReverseConfiguration.isStoreByValue(), is(true)); Product product = new Product(1L); productCache.put(1L, product); assertThat(productCache.get(1L).getId(), equalTo(product.getId())); product.setMutable("foo"); assertThat(productCache.get(1L).getMutable(), nullValue()); assertThat(productCache.get(1L), not(sameInstance(product))); javax.cache.Cache<Long, Customer> customerCache = cacheManager.getCache("customerCache", Long.class, Customer.class); assertThat(customerCache, is(notNullValue())); Configuration<Long, Customer> customerConfiguration = customerCache.getConfiguration(Configuration.class); assertThat(customerConfiguration.getKeyType(), is(equalTo(Long.class))); assertThat(customerConfiguration.getValueType(), is(equalTo(Customer.class))); Customer customer = new Customer(1L); customerCache.put(1L, customer); assertThat(customerCache.get(1L).getId(), equalTo(customer.getId())); }
public JCacheConfiguration(Configuration<K, V> configuration) { if (configuration != null) { if (configuration instanceof RedissonConfiguration) { configuration = ((RedissonConfiguration<K, V>)configuration).getJcacheConfig(); } if (configuration instanceof CompleteConfiguration) { delegate = new MutableConfiguration<K, V>((CompleteConfiguration<K, V>) configuration); } else { delegate = new MutableConfiguration<K, V>(); delegate.setStoreByValue(configuration.isStoreByValue()); delegate.setTypes(configuration.getKeyType(), configuration.getValueType()); } } else { delegate = new MutableConfiguration<K, V>(); } this.expiryPolicy = delegate.getExpiryPolicyFactory().create(); }
public JCacheConfiguration(Configuration<K, V> configuration) { if (configuration != null) { if (configuration instanceof RedissonConfiguration) { configuration = ((RedissonConfiguration<K, V>)configuration).getJcacheConfig(); } if (configuration instanceof CompleteConfiguration) { delegate = new MutableConfiguration<K, V>((CompleteConfiguration<K, V>) configuration); } else { delegate = new MutableConfiguration<K, V>(); delegate.setStoreByValue(configuration.isStoreByValue()); delegate.setTypes(configuration.getKeyType(), configuration.getValueType()); } } else { delegate = new MutableConfiguration<K, V>(); } this.expiryPolicy = delegate.getExpiryPolicyFactory().create(); }
igniteCacheCfg = new CacheConfiguration<>(); igniteCacheCfg.setTypes(cacheCfg.getKeyType(), cacheCfg.getValueType());
/** Copies the configuration and overlays it on top of the default settings. */ @SuppressWarnings("PMD.AccessorMethodGeneration") private <K, V> CaffeineConfiguration<K, V> resolveConfigurationFor( Configuration<K, V> configuration) { if (configuration instanceof CaffeineConfiguration<?, ?>) { return new CaffeineConfiguration<>((CaffeineConfiguration<K, V>) configuration); } CaffeineConfiguration<K, V> template = TypesafeConfigurator.defaults(rootConfig); if (configuration instanceof CompleteConfiguration<?, ?>) { CompleteConfiguration<K, V> complete = (CompleteConfiguration<K, V>) configuration; template.setReadThrough(complete.isReadThrough()); template.setWriteThrough(complete.isWriteThrough()); template.setManagementEnabled(complete.isManagementEnabled()); template.setStatisticsEnabled(complete.isStatisticsEnabled()); template.getCacheEntryListenerConfigurations() .forEach(template::removeCacheEntryListenerConfiguration); complete.getCacheEntryListenerConfigurations() .forEach(template::addCacheEntryListenerConfiguration); template.setCacheLoaderFactory(complete.getCacheLoaderFactory()); template.setCacheWriterFactory(complete.getCacheWriterFactory()); template.setExpiryPolicyFactory(complete.getExpiryPolicyFactory()); } template.setTypes(configuration.getKeyType(), configuration.getValueType()); template.setStoreByValue(configuration.isStoreByValue()); return template; }
public Eh107CompleteConfiguration(Configuration<K, V> config, final CacheConfiguration<K, V> ehcacheConfig, boolean useEhcacheExpiry, boolean useEhcacheLoaderWriter) { this.ehcacheConfig = ehcacheConfig; this.keyType = config.getKeyType(); this.valueType = config.getValueType(); this.isStoreByValue = isStoreByValue(config, ehcacheConfig);
Jsr107CacheLoaderWriter<? super K, V> loaderWriter = null; try { CacheConfigurationBuilder<K, V> builder = newCacheConfigurationBuilder(configuration.getKeyType(), configuration.getValueType(), heap(Long.MAX_VALUE));
@Override public Class<K> getKeyType() { return c.getKeyType(); }
@Override public String getKeyType() { return config().getKeyType().getName(); }
@Override public String getKeyType() { return config().getKeyType().getName(); }
@Override public String getKeyType() { Configuration<K,V> config = tcache.configuration(); return config.getKeyType().getCanonicalName(); }
@Override public String getKeyType() { return configuration().getKeyType().getName(); }
@Test public void getUntypedCache() { CacheManager cacheManager = getCacheManager(); //configure an un-typed Cache MutableConfiguration config = new MutableConfiguration(); cacheManager.createCache("untyped-cache", config); Cache cache = cacheManager.getCache("untyped-cache"); assertNotNull(cache); assertEquals(Object.class, cache.getConfiguration(CompleteConfiguration.class).getKeyType()); assertEquals(Object.class, cache.getConfiguration(CompleteConfiguration.class).getValueType()); }
/** * Ensure a cache's config isn't changed by its configuration object after construction. */ @Test public void testModifyingConfigurationAfterCreateCacheDoesNotModifyCacheConfiguration() { MutableBasicConfiguration mutableConfiguration = new MutableBasicConfiguration().setTypes(Integer.class, Integer.class); Cache<Object, Object> cache = getCacheManager().createCache(getTestCacheName() + "_", mutableConfiguration); mutableConfiguration.setTypes(String.class, String.class); assertEquals(Integer.class, cache.getConfiguration(Configuration.class).getKeyType()); assertEquals(Integer.class, cache.getConfiguration(Configuration.class).getValueType()); }