@Bean public JCacheManagerCustomizer petclinicCacheConfigurationCustomizer() { return cm -> { cm.createCache("vets", cacheConfiguration()); }; }
@Bean public CacheManager jCacheManager() { CacheManager cacheManager = this.cachingProvider.getCacheManager(); MutableConfiguration<Object, Object> mutableConfiguration = new MutableConfiguration<>(); mutableConfiguration.setStoreByValue(false); // otherwise value has to be Serializable cacheManager.createCache("testCache", mutableConfiguration); cacheManager.createCache("primary", mutableConfiguration); cacheManager.createCache("secondary", mutableConfiguration); return cacheManager; }
@Bean public JCacheManagerCustomizer initializrCacheManagerCustomizer() { return (cacheManager) -> { cacheManager.createCache("initializr.metadata", config().setExpiryPolicyFactory( CreatedExpiryPolicy.factoryOf(Duration.TEN_MINUTES))); cacheManager.createCache("initializr.dependency-metadata", config()); cacheManager.createCache("initializr.project-resources", config()); }; }
protected <K, T> Cache<K, SerializedEntity<T>> createCache(String cacheName, Type<T> type) { Class keyClass = getKeyClass(type); if (keyClass == null) { throw new IllegalStateException(); } MutableConfiguration<K, SerializedEntity<T>> configuration = new MutableConfiguration<>(); configuration.setTypes(keyClass, (Class) SerializedEntity.class); configure(configuration); return cacheManager.createCache(cacheName, configuration); }
@Test public void jmxBeanIsRegistered_createCache() throws Exception { checkConfigurationJmx(() -> cacheManager.createCache("cache-not-in-config-file", TypesafeConfigurator.from(ConfigFactory.load(), "test-cache").get())); }
@Test public void definedCache() { try { cacheManager.createCache("test-cache-2", cacheConfig); Assert.fail(); } catch (CacheException ignored) {} checkConfiguration(() -> cacheManager.getCache("test-cache-2", String.class, Integer.class), 1000L); }
@BeforeMethod(alwaysRun = true) public void before() { ticker = new FakeTicker().advance(START_TIME_MS, TimeUnit.MILLISECONDS); jcache = (CacheProxy<Integer, Integer>) cacheManager.createCache("jcache", getConfiguration()); jcacheLoading = (LoadingCacheProxy<Integer, Integer>) cacheManager.createCache( "jcacheLoading", getLoadingConfiguration()); }
@Test public void anonymousCache() { checkConfiguration(() -> cacheManager.createCache("cache-not-in-config-file", cacheConfig), 500L); checkConfiguration(() -> cacheManager.getCache("cache-not-in-config-file", String.class, String.class), 500L); }
JCacheProfiler() { random = new Random(); count = new LongAdder(); CachingProvider provider = Caching.getCachingProvider(CaffeineCachingProvider.class.getName()); CacheManager cacheManager = provider.getCacheManager( provider.getDefaultURI(), provider.getDefaultClassLoader()); cache = cacheManager.createCache("profiler", new MutableConfiguration<>()); executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder() .setPriority(Thread.MIN_PRIORITY).setDaemon(true).build()); }
@Override public void buildEntityManagerFactory() { JCacheHelper.locateStandardCacheManager().createCache( "default-update-timestamps-region", new MutableConfiguration<>() ); JCacheHelper.locateStandardCacheManager().createCache( "default-query-results-region", new MutableConfiguration<>() ); JCacheHelper.locateStandardCacheManager().createCache( "org.hibernate.userguide.mapping.identifier.CacheableNaturalIdTest$Book##NaturalId", new MutableConfiguration<>() ); // JCacheHelper.locateStandardCacheManager().createCache( "", new MutableConfiguration<>() ); super.buildEntityManagerFactory(); }
@Test public void test107ExpiryOverriddenByEhcacheTemplateExpiry() { final AtomicBoolean expiryFactoryInvoked = new AtomicBoolean(false); MutableConfiguration<Long, Product> configuration = new MutableConfiguration<>(); configuration.setTypes(Long.class, Product.class); configuration.setExpiryPolicyFactory(() -> { expiryFactoryInvoked.set(true); return new CreatedExpiryPolicy(Duration.FIVE_MINUTES); }); cacheManager.createCache("productCache3", configuration); assertThat(expiryFactoryInvoked.get(), is(false)); }
@Before public void setup() { this.cacheManager = getCachingProvider().getCacheManager(); this.cacheManager.createCache(CACHE_NAME, new MutableConfiguration<>()); this.cacheManager.createCache(CACHE_NAME_NO_NULL, new MutableConfiguration<>()); this.nativeCache = this.cacheManager.getCache(CACHE_NAME); this.cache = new JCacheCache(this.nativeCache); Cache<Object, Object> nativeCacheNoNull = this.cacheManager.getCache(CACHE_NAME_NO_NULL); this.cacheNoNull = new JCacheCache(nativeCacheNoNull, false); }
@Override public void buildEntityManagerFactory() { JCacheHelper.locateStandardCacheManager().createCache( "hibernate.test.org.hibernate.userguide.caching.NonStrictReadWriteCacheTest$Person", new MutableConfiguration<>() ); JCacheHelper.locateStandardCacheManager().createCache( "hibernate.test.org.hibernate.userguide.caching.NonStrictReadWriteCacheTest$Phone", new MutableConfiguration<>() ); JCacheHelper.locateStandardCacheManager().createCache( "hibernate.test.org.hibernate.userguide.caching.NonStrictReadWriteCacheTest$Person.phones", new MutableConfiguration<>() ); super.buildEntityManagerFactory(); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); CachingProvider provider = Caching.getCachingProvider(); cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-loader-writer-107-load-atomics.xml").toURI(), getClass().getClassLoader()); testCache = cacheManager.createCache("testCache", new MutableConfiguration<Number, CharSequence>() .setReadThrough(true) .setWriteThrough(true) .setCacheLoaderFactory(() -> cacheLoader) .setCacheWriterFactory(() -> cacheWriter) .setTypes(Number.class, CharSequence.class)); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); CachingProvider provider = Caching.getCachingProvider(); cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-loader-writer-107.xml").toURI(), getClass().getClassLoader()); testCache = cacheManager.createCache("testCache", new MutableConfiguration<Number, CharSequence>() .setReadThrough(true) .setWriteThrough(true) .setCacheLoaderFactory(() -> cacheLoader) .setCacheWriterFactory(() -> cacheWriter) .setTypes(Number.class, CharSequence.class)); }
public static void createCache(CacheManager cacheManager, String name, boolean usePrefix) { if ( usePrefix ) { name = prefix( name ); } if ( cacheManager.getCache( name ) != null ) { cacheManager.destroyCache( name ); } cacheManager.createCache( name, new MutableConfiguration<>() ); }
@Test public void enablingReadThroughDoesNotForceWriteThrough() throws Exception { MutableConfiguration<Long, String> config = getConfiguration(true, cacheLoader, false, cacheWriter); Cache<Long, String> cache = cachingProvider.getCacheManager().createCache("writingCache", config); cache.put(42L, "Tadam!!!"); cache.get(100L); verifyZeroInteractions(cacheWriter); verify(cacheLoader).load(100L); }
@Test public void testBasicCacheOperation() throws IOException, URISyntaxException { Configuration config = new DefaultConfiguration(ResourceCombinationsTest.class.getClassLoader(), new DefaultPersistenceConfiguration(diskPath.newFolder())); try (CacheManager cacheManager = new EhcacheCachingProvider().getCacheManager(URI.create("dummy"), config)) { Cache<String, String> cache = cacheManager.createCache("test", fromEhcacheCacheConfiguration( newCacheConfigurationBuilder(String.class, String.class, resources))); cache.put("foo", "bar"); assertThat(cache.get("foo"), is("bar")); } } }
public JCache(URL url) { String method = url.getParameter(Constants.METHOD_KEY, ""); String key = url.getAddress() + "." + url.getServiceKey() + "." + method; // jcache parameter is the full-qualified class name of SPI implementation String type = url.getParameter("jcache"); CachingProvider provider = StringUtils.isEmpty(type) ? Caching.getCachingProvider() : Caching.getCachingProvider(type); CacheManager cacheManager = provider.getCacheManager(); Cache<Object, Object> cache = cacheManager.getCache(key); if (cache == null) { try { //configure the cache MutableConfiguration config = new MutableConfiguration<Object, Object>() .setTypes(Object.class, Object.class) .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MILLISECONDS, url.getMethodParameter(method, "cache.write.expire", 60 * 1000)))) .setStoreByValue(false) .setManagementEnabled(true) .setStatisticsEnabled(true); cache = cacheManager.createCache(key, config); } catch (CacheException e) { // concurrent cache initialization cache = cacheManager.getCache(key); } } this.store = cache; }
public JCache(URL url) { String method = url.getParameter(Constants.METHOD_KEY, ""); String key = url.getAddress() + "." + url.getServiceKey() + "." + method; // jcache parameter is the full-qualified class name of SPI implementation String type = url.getParameter("jcache"); CachingProvider provider = StringUtils.isEmpty(type) ? Caching.getCachingProvider() : Caching.getCachingProvider(type); CacheManager cacheManager = provider.getCacheManager(); Cache<Object, Object> cache = cacheManager.getCache(key); if (cache == null) { try { //configure the cache MutableConfiguration config = new MutableConfiguration<Object, Object>() .setTypes(Object.class, Object.class) .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MILLISECONDS, url.getMethodParameter(method, "cache.write.expire", 60 * 1000)))) .setStoreByValue(false) .setManagementEnabled(true) .setStatisticsEnabled(true); cache = cacheManager.createCache(key, config); } catch (CacheException e) { // concurrent cache initialization cache = cacheManager.getCache(key); } } this.store = cache; }