
Spring JCache
coding一月 20, 20211mins
Spring
There are two ways to customize the underlying javax.cache.cacheManager:
- Caches can be created on startup by setting the
spring.cache.cache-namesproperty. If a customjavax.cache.configuration.Configurationbean is defined, it is used to customize them. org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizerbeans are invoked with the reference of theCacheManagerfor full customization.
java
//JCacheCacheConfiguration@Bean@ConditionalOnMissingBeanCacheManager jCacheCacheManager(CacheProperties cacheProperties,ObjectProvider<javax.cache.configuration.Configuration<?, ?>> defaultCacheConfiguration,ObjectProvider<JCacheManagerCustomizer> cacheManagerCustomizers,ObjectProvider<JCachePropertiesCustomizer> cachePropertiesCustomizers) throws IOException {CacheManager jCacheCacheManager = createCacheManager(cacheProperties, cachePropertiesCustomizers);List<String> cacheNames = cacheProperties.getCacheNames();if (!CollectionUtils.isEmpty(cacheNames)) {for (String cacheName : cacheNames) {jCacheCacheManager.createCache(cacheName,defaultCacheConfiguration.getIfAvailable(MutableConfiguration::new));}}cacheManagerCustomizers.orderedStream().forEach((customizer) -> customizer.customize(jCacheCacheManager));return jCacheCacheManager;}// RedisCacheConfiguration
将Spring配置的CacheManager提供给Hihernate
java
@Beanpublic HibernatePropertiesCustomizer hibernatePropertiesCustomizer(javax.cache.CacheManager cm) {return hibernateProperties -> hibernateProperties.put(ConfigSettings.CACHE_MANAGER, cm);}
Hibernate创建的CacheManager的时候会去取
java
org.hibernate.cache.jcache.internal.JCacheRegionFactoryprotected CacheManager resolveCacheManager(SessionFactoryOptions settings, Map properties) {final Object explicitCacheManager = properties.get( ConfigSettings.CACHE_MANAGER );if ( explicitCacheManager != null ) {return useExplicitCacheManager( settings, explicitCacheManager );}......}
但是hibernate的cache必须提前让Spring这边创建好,否则由JcacheRegionFactory这边创建的话会报错
java
// JCacheRegionFactorycacheManager.createCache( regionName, new MutableConfiguration<>() );// org.redisson.jcache.JCacheManagercreateCache() {if (cacheRedisson == null && !(configuration instanceof RedissonConfiguration)) {throw new IllegalStateException("Default configuration hasn't been specified!");}}
评论
新的评论
上一篇
Javascript Object Signing and Encryption (JOSE)
JWT JWTs can be represented as either JSON Web Signature (JWS) 3 or a JSON Web Encryption (JWE) 4 objects. Claims within a JWS can be r…
下一篇
VIM 文件类型检测
:filetype on 启用文件类型识别后,VIM会尝试设置文件的 filetype 选项,这将会触发 FileType 事件,然后能够正确启用语法高亮等。识别文件文件是一般是通过文件扩展名(当前也根据文件内容) autocmd BufNewFile,BufRead *.…
