EhCache 是一個純Java的進程內緩存框架,具有快速、精干等特點,是Hibernate中默認的CacheProvider。
ehcache提供了多種緩存策略,主要分為內存和磁盤兩級,所以無需擔心容量問題。
spring-boot是一個快速的集成框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。
由于spring-boot無需任何樣板化的配置文件,所以spring-boot集成一些其他框架時會有略微的不同。
1.spring-boot是一個通過maven管理的jar包的框架,集成ehcache需要的依賴如下
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId></dependency><dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.8.3</version></dependency>
具體pom.xml文件如下
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lclc.boot</groupId> <artifactId>boot-cache</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.1.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>17.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.8.3</version> </dependency> </dependencies> <dependencyManagement> <dependencies> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories></project>
2.使用ehcache,我們需要一個ehcache.xml來定義一些cache的屬性。
<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="java.io.tmpdir/Tmp_EhCache" /> <defaultCache eternal="false" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" /> <cache name="demo" eternal="false" maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" /></ehcache>
解釋下這個xml文件中的標簽。
(1).diskStore: 為緩存路徑,ehcache分為內存和磁盤兩級,此屬性定義磁盤的緩存位置。參數解釋如下:
(2).defaultCache:默認緩存策略,當ehcache找不到定義的緩存時,則使用這個緩存策略。只能定義一個。
(3).cache:自定緩存策略,為自定義的緩存策略。參數解釋如下:
SpringBoot支持很多種緩存方式:redis、guava、ehcahe、jcache等等。
說明下redis和ehcache的區別:
Redis:屬于獨立的運行程序,需要單獨安裝后,使用Java中的Jedis來操縱。因為它是獨立,所以如果你寫個單元測試程序,放一些數據在Redis中,然后又寫一個程序去拿數據,那么是可以拿到這個數據的。,
ehcache:與Redis明顯不同,它與java程序是綁在一起的,java程序活著,它就活著。譬如,寫一個獨立程序放數據,再寫一個獨立程序拿數據,那么是拿不到數據的。只能在獨立程序中才能拿到數據。
3.將ehcache的管理器暴露給spring的上下文容器,
@Configuration// 標注啟動了緩存@EnableCachingpublic class CacheConfiguration { /* * ehcache 主要的管理器 */ @Bean(name = "appEhCacheCacheManager") public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){ return new EhCacheCacheManager (bean.getObject ()); } /* * 據shared與否的設置,Spring分別通過CacheManager.create()或new CacheManager()方式來創建一個ehcache基地. */ @Bean public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){ EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean (); cacheManagerFactoryBean.setConfigLocation (new ClassPathResource ("conf/ehcache-app.xml")); cacheManagerFactoryBean.setShared (true); return cacheManagerFactoryBean; }}
@Configuration:為spring-boot注解,主要標注此為配置類,優先掃描。
@Bean:向spring容器中加入bean。
至此所有的配置都做好了,通過spring-boot進行集成框架就是這么簡單。
4.使用ehcache
使用ehcache主要通過spring的緩存機制,上面我們將spring的緩存機制使用了ehcache進行實現,所以使用方面就完全使用spring緩存機制就行了。
具體牽扯到幾個注解:
@Cacheable:負責將方法的返回值加入到緩存中,參數3
@CacheEvict:負責清除緩存,參數4
參數解釋:
不多說,直接上代碼:
@Servicepublic class CacheDemoServiceImpl implements CacheDemoService { /** * 緩存的key */ public static final String THING_ALL_KEY = "/"thing_all/""; /** * value屬性表示使用哪個緩存策略,緩存策略在ehcache.xml */ public static final String DEMO_CACHE_NAME = "demo"; @CacheEvict(value = DEMO_CACHE_NAME,key = THING_ALL_KEY) @Override public void create(Thing thing){ Long id = getNextId (); thing.setId (id); data.put (id, thing); } @Cacheable(value = DEMO_CACHE_NAME,key = "#thing.getId()+'thing'") @Override public Thing findById(Long id){ System.err.println ("沒有走緩存!" + id); return data.get (id); } @Cacheable(value = DEMO_CACHE_NAME,key = THING_ALL_KEY) @Override public List<Thing> findAll(){ return Lists.newArrayList (data.values ()); } @Override @CachePut(value = DEMO_CACHE_NAME,key = "#thing.getId()+'thing'") @CacheEvict(value = DEMO_CACHE_NAME,key = THING_ALL_KEY) public Thing update(Thing thing){ System.out.println (thing); data.put (thing.getId (), thing); return thing; } @CacheEvict(value = DEMO_CACHE_NAME) @Override public void delete(Long id){ data.remove (id); } }
5.只需要通過注解在service層方法上打注解便可以使用緩存,在find**上存入緩存,在delete**,update**上清除緩存。
Cache注解詳解
@CacheConfig:主要用于配置該類中會用到的一些共用的緩存配置。在這里@CacheConfig(cacheNames = "users"):配置了該數據訪問對象中返回的內容將存儲于名為users的緩存對象中,我們也可以不使用該注解,直接通過@Cacheable自己配置緩存集的名字來定義。
@Cacheable:配置了findByName函數的返回值將被加入緩存。同時在查詢時,會先從緩存中獲取,若不存在才再發起對數據庫的訪問。該注解主要有下面幾個參數:
除了這里用到的兩個注解之外,還有下面幾個核心注解:
@CachePut:配置于函數上,能夠根據參數定義條件來進行緩存,它與@Cacheable不同的是,它每次都會真是調用函數,所以主要用于數據新增和修改操作上。它的參數與@Cacheable類似,具體功能可參考上面對@Cacheable參數的解析
@CacheEvict:配置于函數上,通常用在刪除方法上,用來從緩存中移除相應數據。除了同@Cacheable一樣的參數之外,它還有下面兩個參數:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答
圖片精選