一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第20天,点击查看活动详情。
3.1 添加pom.xml依赖
注:在项目1中已经完成添加
3.2 访问Redis
在com.example.myFirstBlog文件夹下创建 TestRedis.java 用于测试访问redis 代码如下,运行后能够正常输出即访问成功。
package com.example.myFirstBlog;
import redis.clients.jedis.Jedis;
public class TestRedis {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.set("1","China");
jedis.set("2","USA");
System.out.println(jedis.get("1"));
}
}
3.3 在application.yml里添加redis 配置
spring:
redis:
host: 127.0.0.1
port: 6379
3.4 对实体类进行序列化
在User类的代码改为:
package com.example.myFirstBlog.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
private int id;
private String name;
private String pwd;
}
3.5 编写缓存业务逻辑代码
在service 文件夹下创建 RedisService.java ,编写缓存的业务逻辑代码,业务逻辑:在用户查询数据时,首先判断redis缓存中是否存在,如果存在直接返回即可,如果不存在则到数据库中去查询,在数据库中如果存在,放入到redis缓存中并返回结果,如果不存在则返回Null空值
(如果不给key value加序列化,则在redis中会出现乱码如下图)
package com.example.myFirstBlog.service;
import com.example.myFirstBlog.entity.User;
import com.example.myFirstBlog.mapper.UserMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class RedisService {
@Resource
private UserMapper userMapper;
@Resource
private RedisTemplate redisTemplate;
public User findByID(Integer id){
//序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
ValueOperations ops = redisTemplate.opsForValue();
Object o = ops.get("redis::selectByID:"+id);
if (o != null){
return (User) o;
}else{
User user = userMapper.queryUserByID(id);
if (user != null){
ops.set("redis::selectByID:"+id,user);
return user;
}
}
return null;
}
}
3.6 创建RedisUserController
在controller文件夹下创建RedisUserController.java
package com.example.myFirstBlog.controller;
import com.example.myFirstBlog.entity.User;
import com.example.myFirstBlog.service.RedisService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class RedisUserController {
@Resource
private RedisService redisService;
@RequestMapping ("find/{id}")
public User findByID(@PathVariable Integer id){
return redisService.findByID(id);
}
}
3.7 启动项目
在地址栏输入 localhost:8084/find/1 返回显示结果则说明搭建成功。
4. 实现在项目启动时,将数据库中的数据放入redis缓存中
注:需要在1.2.3项目基础上搭建
4.1 在RedisUserController 类中加入如下代码
@Resource
private UserMapper userMapper;
@Resource
private RedisTemplate redisTemplate;
private static final String ALL_USER = "ALL_USER_LIST";
@PostConstruct
public void contextInitialized(){
List<User> userList = userMapper.queryUserList();
//清除缓存中的数据
redisTemplate.delete(ALL_USER);
redisTemplate.opsForList().leftPushAll(ALL_USER,userList);
List<User> queryUserList = redisTemplate.opsForList().range(ALL_USER,0,-1);
System.out.println("缓存中目前的用户数:" + queryUserList.size() + "人");
}
4.2 结果展示
如果输出的人数和你放入的数据条数相同,则表示启动项目时数据成功放入了缓存当中。