@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; }
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(); }
private static <K, V> boolean isStoreByValue(Configuration<K, V> config, CacheConfiguration<K, V> ehcacheConfig) { if(ehcacheConfig != null) { Collection<ServiceConfiguration<?>> serviceConfigurations = ehcacheConfig.getServiceConfigurations(); for (ServiceConfiguration<?> serviceConfiguration : serviceConfigurations) { if (serviceConfiguration instanceof DefaultCopierConfiguration) { DefaultCopierConfiguration<?> copierConfig = (DefaultCopierConfiguration)serviceConfiguration; if(copierConfig.getType().equals(DefaultCopierConfiguration.Type.VALUE)) { if(copierConfig.getClazz().isAssignableFrom(IdentityCopier.class)) { return false; } else { return true; } } } } } return config.isStoreByValue(); }
@SuppressWarnings("unchecked") public static void testJsr107EhcacheOsgi() throws Exception { CachingProvider cachingProvider = Caching.getCachingProvider("org.ehcache.jsr107.EhcacheCachingProvider", TestMethods.class.getClassLoader()); CacheManager cacheManager = cachingProvider.getCacheManager(TestMethods.class.getResource("/org/ehcache/osgi/ehcache-107-osgi.xml").toURI(), TestMethods.class.getClassLoader()); Cache<Long, Person> personCache = cacheManager.getCache("personCache", Long.class, Person.class); assertEquals(Person.class, personCache.getConfiguration(javax.cache.configuration.Configuration.class).getValueType()); }
@Override public Class<K> getKeyType() { return c.getKeyType(); }
@Test @SuppressWarnings("unchecked") public void testTemplateOverridingStoreByValue() throws Exception { cacheManager = cachingProvider.getCacheManager( getClass().getResource("/org/ehcache/docs/ehcache-jsr107-template-override.xml").toURI(), getClass().getClassLoader()); MutableConfiguration<Long, Client> mutableConfiguration = new MutableConfiguration<>(); mutableConfiguration.setTypes(Long.class, Client.class); Client client1 = new Client("client1", 1); Cache<Long, Client> myCache = null; myCache = cacheManager.createCache("anyCache", mutableConfiguration); myCache.put(1L, client1); assertNotSame(client1, myCache.get(1L)); assertTrue(myCache.getConfiguration(Configuration.class).isStoreByValue()); myCache = cacheManager.createCache("byRefCache", mutableConfiguration); myCache.put(1L, client1); assertSame(client1, myCache.get(1L)); assertFalse(myCache.getConfiguration(Configuration.class).isStoreByValue()); myCache = cacheManager.createCache("weirdCache1", mutableConfiguration); myCache.put(1L, client1); assertNotSame(client1, myCache.get(1L)); assertTrue(myCache.getConfiguration(Configuration.class).isStoreByValue()); myCache = cacheManager.createCache("weirdCache2", mutableConfiguration); myCache.put(1L, client1); assertSame(client1, myCache.get(1L)); assertFalse(myCache.getConfiguration(Configuration.class).isStoreByValue()); }
@Override public Class<T> getValueType() { return c.getValueType(); }
@Override public String getKeyType() { return config().getKeyType().getName(); }
/** {@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(); } }
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(); }
assertFalse(anotherCache.getConfiguration(Configuration.class).isStoreByValue()); // <5>
@Override public String getValueType() { return configuration().getValueType().getName(); }
@Override public String getKeyType() { return jCache.getConfiguration(CompleteConfiguration.class).getKeyType().getName(); }
@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; }
/** 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; }
@Override public boolean isStoreByValue() { return config().isStoreByValue(); }
@Override public String getValueType() { return config().getValueType().getName(); }
@Override public String getKeyType() { return config().getKeyType().getName(); }
@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))); }
@SuppressWarnings("unchecked") protected <K, V, C extends Configuration<K, V>> CacheConfig<K, V> createCacheConfig(String cacheName, C configuration) { CacheConfig<K, V> cacheConfig; if (configuration instanceof CompleteConfiguration) { cacheConfig = new CacheConfig<K, V>((CompleteConfiguration<K, V>) configuration); } else { cacheConfig = new CacheConfig<K, V>(); cacheConfig.setStoreByValue(configuration.isStoreByValue()); Class<K> keyType = configuration.getKeyType(); Class<V> valueType = configuration.getValueType(); cacheConfig.setTypes(keyType, valueType); } cacheConfig.setName(cacheName); cacheConfig.setManagerPrefix(this.cacheNamePrefix); cacheConfig.setUriString(getURI().toString()); return cacheConfig; }