就先不连接数据库了 我们测试缓存
实体类:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private int id;
private String name;
private String sex;
private String addr;
}
service:
@Service
public class UserService {
public User findUserById(int id){
User user=new User(1001,"库里","男","勇士");
System.out.println("加入了service方法");
return user;
}
}
controller
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findUser")
public User findUserById(int id){
User user=userService.findUserById(id);
return user;
}
}
=============================================================
加入redis缓存
配置文件:
spring:
redis:
host: 192.168.200.166
password: 123.com
database: 10
port: 6380
在service中的方法上加注解
//在查询的方法上加入缓存
@Cacheable(cacheNames = "user",key = "#id")
//在查询的方法上加入缓存
@RequestMapping("/findUser")
public User findUserById(int id){
User user=userService.findUserById(id);
return user;
}
在启动类上加:
@EnableCaching ///开启spring的声明式缓存
即可完成!!!
解决乱码问题
配置类:
@Configuration
public class RedisConfig {
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
//缓存配置对象
RedisCacheConfiguration redisCacheConfiguration =
RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration =
redisCacheConfiguration.entryTtl(Duration.ofMinutes(30L)) //设置缓存的默 认超时时间:30 分钟
.disableCachingNullValues() //如果是空值,不缓存
.serializeKeysWith(RedisSerializationContext
.SerializationPair.fromSerializer(new StringRedisSerializer())) //设置 key 序列化器
.serializeValuesWith(RedisSerializationContext.
SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); //设置 value 序列化器
//重载就可以了
return RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}
}
解决:
保存一个集合
service:
@Cacheable(cacheNames = "userList")
public List<User> findUserList(){
User user1=new User(1001,"库里1","男","勇士");
User user2=new User(1002,"库里2","nan","勇士");
User user3=new User(1003,"库里3","男","勇士");
User user4=new User(1004,"库里4","nan","勇士");
List<User> list=new ArrayList<>();
list.add(user1);
list.add(user2);
list.add(user3);
list.add(user4);
return list;
}
controller
//做一个集合的
@RequestMapping("/findUserList")
public List<User> findUserList(){
List<User> userList = userService.findUserList();
return userList;
}
会有点小问题
key并不是我们想要的
@Cacheable(cacheNames = "userList",key = "'aa'")
还可以这么写:
@Cacheable(cacheNames = "user",key = "'u:'+#id")+ 和 ' ' 单引号
@Cacheable(cacheNames = "user",key = "'u:'+#id",condition = "#id == 1000")id为1000才会缓存
修改
service:
/**
* 修改操作
* @param user
* @return
*/
@CachePut(key = "'u'+#user.id")
public User updateUser(User user){
System.out.println("修改会一直进入service....");
user.setName("詹姆斯");
user.setAddr("湖人");
user.setSex("man");
//同步数据库
//上面我么可以连接数据库进行对数据库的修改操作
return user;
}
controller
@RequestMapping("/update")
public User updateUser(User user){
User user1 = userService.updateUser(user);
return user1;
}
目的就是同步数据库
================================================================
删除
service:
//删除 除了删除其他的都需要返回值
@CacheEvict(key = "'u'+#id")
public void deleteUser(int id){
System.out.println("删除了数据");
}
controller
@RequestMapping("/deleteUser")
public void deleteUser(int id){
userService.deleteUser(id);
}