spring boot 2.1.4 hibernate二級緩存 Hazelcast實現(一)

Hazelcast優點網上均可以查到,默認的分佈式緩存,使用Hazelcast替換ehcache優點比較明顯,也方便項目從單機到發展分佈式,而不用再引入其餘組件也達到了性能要求,按照springboot推薦,這裏使用JCache規範操做緩存,引入包java

<!-- 集成jcache規範 -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-jcache</artifactId>
		</dependency>
		 <!-- hazelcast jar  -->
		<dependency>
			<groupId>com.hazelcast</groupId>
			<artifactId>hazelcast</artifactId>
		</dependency>
		<dependency>
			<groupId> com.hazelcast </groupId>
			<artifactId>hazelcast-spring</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-hazelcast</artifactId>
		</dependency>

spring-session-core和spring-session-hazelcast這兩個包是爲了將session也保存到hazelcast中,這時候系統就是分佈式的了spring

配置spring.jp .properties.hibernate.cache.region.factory_class=org.hibernate.cache.jcache.internal.JCacheRegionFactory或是緩存

@Bean
    public HibernatePropertiesCustomizer hibernateSecondLevelCacheCustomizer(JCacheCacheManager jCacheCacheManager) {
        return (properties) -> properties.put(ConfigSettings.CACHE_MANAGER, jCacheCacheManager.getCacheManager());
    }

上面兩種方式配置一種就能夠了,hibernate二級緩存就算配置好了springboot

 

啓動時系統出現了警告:session

org.hibernate.orm.cache : HHH90001006: Missing cache[com.bc.core.system.entity.SysUser] was created on-the-fly. The created cache will use a provider-specific default configuration: make sure you defined one. You can disable this warning by setting 'hibernate.javax.cache.missing_cache_strategy' to 'create'.app

 意思是咱們須要配置hibernate.javax.cache.missing_cache_strategy=create,表示若是沒有在配置文件中配置緩存,將會自動建立,這裏的配置文件是hazelcast.xml,可是這裏咱們並無這個文件,hazelcast會自動讀取默認的一份配置文件,在hazelcast的jar包中有hazelcast-default.xml文件,記錄了默認配置,要自定義配置時,複製一份,重命名爲hazelcast.xml,放到resources目錄下,會自動讀到async

路徑配置spring.hazelcast.config=classpath:hazelcast.xml    能夠不用配置分佈式

這裏給出一份hazelcast.xml文件中cache配置示例,默認裏面沒有,找官方文檔才發現,網上都是配置map的ide

<cache name="default">
		<key-type class-name="java.lang.Object" />
		<value-type class-name="java.lang.Object" />
		<statistics-enabled>true</statistics-enabled>
		<management-enabled>true</management-enabled>
		<read-through>true</read-through>
		<write-through>true</write-through>
		<backup-count>1</backup-count>
		<async-backup-count>1</async-backup-count>
		<in-memory-format>BINARY</in-memory-format>
		<eviction size="10000" max-size-policy="ENTRY_COUNT"
			eviction-policy="LRU" />
		<quorum-ref>quorum-name</quorum-ref>
		<disable-per-entry-invalidation-events>true
		</disable-per-entry-invalidation-events>
		<!-- 
		<cache-loader-factory class-name="com.example.cache.MyCacheLoaderFactory" />
		<cache-writer-factory class-name="com.example.cache.MyCacheWriterFactory" />
		<expiry-policy-factory class-name="com.example.cache.MyExpiryPolicyFactory" />
		<cache-entry-listeners>
			<cache-entry-listener old-value-required="false"
				synchronous="false">
				<cache-entry-listener-factory
					class-name="com.example.cache.MyEntryListenerFactory" />
				<cache-entry-event-filter-factory
					class-name="com.example.cache.MyEntryEventFilterFactory" />
			</cache-entry-listener>
		</cache-entry-listeners> 
		-->
	</cache>

 

hibernate.javax.cache.missing_cache_strategy 這是hibernate配置項,對應到spring中性能

在application.properties中的配置項:spring.jpa.properties.hibernate.javax.cache.missing_cache_strategy=create

相關文章
相關標籤/搜索