記在前面:
@Cacheable@CachePut存入緩存里面的數據是方法執行的返回值
所以加了@CachePut的類的返回值要和加了@Cacheable的類的返回值一樣
我的環境:
springboot2.x,idea
目錄結構
mysql里面的表結構
CREATE TABLE `user` ( `id` int(30) NOT NULL AUTO_INCREMENT, `username` varchar(30) DEFAULT NULL, `password` varchar(30) DEFAULT NULL, PRIMARY KEY (`id`) )
配置文件application.properties

server.port=8181 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://127.0.0.1:3306/cache?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456
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.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.swd</groupId> <artifactId>mydemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mydemo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
啟動類

package com.swd.mydemo; import org.apache.ibatis.annotations.Mapper; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication @EnableCaching @MapperScan("com.swd.mydemo.dao") public class MydemoApplication { public static void main(String[] args) { SpringApplication.run(MydemoApplication.class, args); } }
controller層
package com.swd.mydemo.controller; import com.swd.mydemo.bean.User; import com.swd.mydemo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; /** * @program: mydemo * @Date: 2019/12/25 14:36 * @Author: lhh * @Description: */ @RestController public class controller { @Autowired UserService userService; @GetMapping("user/{id}") public User getUserById(@PathVariable("id") Integer id) { return userService.getUserById(id); } @GetMapping("user") public void updateUserById(User user) { userService.updateUserById(user); } }
bean層
package com.swd.mydemo.bean; import lombok.Data; /** * @program: mydemo * @Date: 2019/12/25 14:30 * @Author: lhh * @Description: */ @Data public class User { private int id; private String username; private String password; }
dao層
import com.swd.mydemo.bean.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; /** * @program: mydemo * @Date: 2019/12/25 14:32 * @Author: lhh * @Description: */ @Mapper public interface UserDao { @Select("select * from user where id = #{id}") User getById(Integer id); @Update("update user set username = #{username} , password = #{password} where id = #{id}") void updateById(User user); }
service層
package com.swd.mydemo.service; import com.swd.mydemo.bean.User; import com.swd.mydemo.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; /** * @program: mydemo * @Date: 2019/12/25 14:34 * @Author: lhh * @Description: */ @Service public class UserService { @Autowired UserDao userDao; @Cacheable(cacheNames = "userw",key = "#id")//key里面的id取得是參數id(url傳過來的是1) public User getUserById(Integer id) { System.out.println("查詢"+id+"員工"); User user = userDao.getById(id); return user; } @CachePut(cacheNames = "userw",key = "#result.id")//result.id和user.id都可以(user.id是1) public User updateUserById(User user) { System.out.println("更新"+user.getId()+"員工"+user.getUsername()); userDao.updateById(user); return user; } }
在chrome中測試
在chrome多點幾次回車查詢,在idea控制台里面,public User getUserById(Integer id)這個方法只執行了一次,其他都是從緩存里面取的結果到chrome界面里
更新
頁面沒報錯,控制台顯示更新成功
結果:
數據庫表里面更新成功
緩存更新成功,頁面取的數據是從已經更新了的緩存里面取出來的
在來看idea控制台,可見從數據庫里面查數據只查詢了一次,其他都是從緩存里面查詢出來的,牛皮!!
如果方便請點個贊呀,兄弟們。想要源碼的可以留言評論哦。
記在后面:
@Cacheable標注的方法執行之前先來檢查緩存中有沒有這個數據,默認按照參數的值作為key去查詢緩存,
如果沒有就運行方法並將結果放入緩存;以后再來調用就可以直接使用緩存中的數據;
@CachePut:既調用方法,又更新緩存數據;
修改了數據庫的某個數據,同時更新緩存;
@CacheEvict:緩存清除
key:指定要清除的數據
allEntries = true:指定清除這個緩存中所有的數據
二、上面已經完成了cache緩存,下面我們來整合redis
目錄結構,新加了myconfig
在pom文件里面加上
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
新建一個config路徑,在此路徑下面新建一個RedisConfig,這里的配置,可以讓cache的緩存直接存在redis里面,key和value也已經序列化了,存在redis里面的也是json格式的,還有一個點就是你的實體類要
implements Serializable
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.text.SimpleDateFormat; import java.time.Duration; /** * @program: mydemo * @Date: 2019/12/26 16:48 * @Author: lhh * @Description: */ @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Bean public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){ /** * 配置自己的redisTemplate * StringRedisTemplate 默認使用使用StringRedisSerializer來序列化 * RedisTemplate 默認使用JdkSerializationRedisSerializer來序列化 */ RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); //開啟默認類型 ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); objectMapper.setDateFormat(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")); // 使用Jackson2JsonRedisSerialize替換默認序列化 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); //設置key和value的序列化規則 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean public RedisCacheManager cacheManager(RedisConnectionFactory factory) { RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); //解決查詢緩存轉換異常的問題 ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); //配置序列化(解決亂碼的問題) RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ZERO) .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(factory) .cacheDefaults(config) .build(); return cacheManager; } }