<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
首先我們知道jpa,jdbc這些東西都是一些規範,比如jdbc,要要連線到資料庫,都是需要用到資料庫連線,預處理,結果集這三個物件,無論是連線到mysql還是oracle都是需要用到這個三個物件的,這是一種規範,而SpringCache是一種作為快取的規範,具體實現有redis,EhCahe等
1.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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.yl</groupId> <artifactId>cache_redis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>cache_redis</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <artifactId>spring-boot-starter-data-redis</artifactId> <artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.application.properties
# redis的設定 spring.redis.host=192.168.244.135 spring.redis.port=6379 spring.redis.password=root123
3.實體類
package com.yl.cache_redis.domain; import java.io.Serializable; public class User implements Serializable { private Integer id; private String username; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + ''' + ", password='" + password + ''' + '}'; } }
4.service
package com.yl.cache_redis; import com.yl.cache_redis.domain.User; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service public class UserService { @Cacheable(cacheNames = "u1") //這個註解作用就是將方法的返回值存到快取中 public User getUserById(Integer id) { System.out.println("getUserById:" + id); User user = new User(); user.setId(id); user.setUsername("root"); user.setPassword("root"); return user; } }
5.主程式,加上開啟快取的註解
package com.yl.cache_redis; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication @EnableCaching //開啟快取功能 public class CacheRedisApplication { public static void main(String[] args) { SpringApplication.run(CacheRedisApplication.class, args); } }
6.測試
6.1)userservice沒加@Cacheable註解時
6.2)userservice加@Cacheable註解後,發現sevice中的方法只呼叫了一次
6.3)在redis中也可以看到快取中有資料,key為定義好的cacheNames+::+方法的引數
1.SpringCache預設使用cacheNames和方法中的引數結合組成key的,那麼如果有多個引數呢?它又是如何組成key的呢?我們可以指定key嗎?
2.如何自定義key呢?
1)自定義key
package com.yl.cache_redis; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.Arrays; @Component public class MyKeyGenerator implements KeyGenerator { @Override public Object generate(Object target, Method method, Object... params) { return target.toString() + ":" + method.getName() + ":" + Arrays.toString(params); } }
2)測試
1.使用@CachePut註解來更新,注意:@CachePut中的key要和@Cacheable中的key一樣,否則更新不了!
1.使用@CacheEvict註解,主要key和要@Cacheable中的key一致
2.測試
1.@Caching註解,可以組合多個註解
2.@CacheConfig註解
1.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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.yl</groupId> <artifactId>ehcache</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ehcache</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.6</version> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.實體類
package com.yl.ehcache.model; import java.io.Serializable; public class User implements Serializable { private Integer id; private String username; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + ''' + ", password='" + password + ''' + '}'; } }
3.service
package com.yl.ehcache.service; import com.yl.ehcache.model.User; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service public class UserService { @Cacheable(cacheNames = "user") public User getUserById(Integer id) { System.out.println("getUserById()..."); User user = new User(); user.setId(id); user.setUsername("root"); user.setPassword("root"); return user; } @CacheEvict(cacheNames = "user") public void delete(Integer id) { System.out.println("delete"); }
4.主程式
package com.yl.ehcache; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication @EnableCaching public class EhcacheApplication { public static void main(String[] args) { SpringApplication.run(EhcacheApplication.class, args); } }
5.ehcache.xml
<ehcache> <diskStore path="java.io.tmpdir/shiro-spring-sample"/> <defaultCache maxElementsInMemory = "1000" eternal = "false" timeToIdleSeconds = "120" timeToLiveSeconds = "120" overflowToDisk = "false" diskPersistent = "false" diskExpiryThreadIntervalSeconds = "120"/> <cache name = "user" maxElementsInMemory = "1000" eternal = "false" overflowToDisk = "true" diskPersistent = "true" diskExpiryThreadIntervalSeconds = "600"/> </ehcache>
6.測試
到此這篇關於SpringBoot與SpringCache的文章就介紹到這了,更多相關SpringBoot與SpringCache內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45