前言:最近工作中使用到了redis缓存,故分享一点自己总结的东西,这篇文章使用的是StringRedisTemplate进行学习,这里值的说的是,(1)StringRedisTemplate在进行批量删除操作时我们需对template进行序列化,(2)更新操作与添加操作一样,接下来上代码:
1.建立Spring boot项目,引入Redis依赖(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.test</groupId>
<artifactId>redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>redis</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.编写spring boot配置文件,这里我配置的内容简单,如需要其他的配置可以进官网去查
#Redis
spring.redis.host=主机地址
spring.redis.password=admin
spring.redis.port=6379
server.port=8081
3.接下里我们开始写测试
(1)建立实体类:
User:
package com.test.redis.entity;
public class User {
private Integer id;
private String name;
private String password;
public User() {
super();
}
public User(Integer id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
}
}
(2)service层,主要对redis的各种操作方法进行定义
RedisService:
package com.test.redis.service;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.stereotype.Service;
@Service
public class RedisService {
@Resource
private StringRedisTemplate template;
/**
* 存储数据或修改数据
*
* @param modelMap
* @param mapName
*/
public void setKey(String mapName, Map<String, Object> modelMap) {
HashOperations<String, Object, Object> hps = template.opsForHash();
hps.putAll(mapName, modelMap);
}
/**
* 获取数据Map
*
* @param mapName
* @return
*/
public Map<Object, Object> getMapValue(String mapName) {
HashOperations<String, Object, Object> hps = this.template.opsForHash();
return hps.entries(mapName);
}
/**
* 获取数据value
*
* @param mapName
* @param hashKey
* @return
*/
public Object getValue(String mapName, String hashKey) {
HashOperations<String, Object, Object> hps = this.template.opsForHash();
return hps.get(mapName, hashKey);
}
/**
* 批量删除缓存数据
*
* @param keys
*/
public void deleteData(List<String> keys) {
// 执行批量删除操作时先序列化template
template.setKeySerializer(new JdkSerializationRedisSerializer());
template.delete(keys);
}
}
(3)controller层代码,演示操作(添加与获取值):
package com.test.redis.web;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.test.redis.entity.User;
import com.test.redis.service.RedisService;
@Controller
public class UserController {
private static final String mapName="mapName";
@Autowired
private RedisService redisService;
@GetMapping( "/add.do")
@ResponseBody
public Map<Object, Object> addUser(HttpServletRequest request){
Map<String, Object> modelMap=new HashMap<String,Object>();
User user=new User();
user.setName("hehename");
user.setPassword("hehePassword");
//存放hash值
modelMap.put("name", user.getName());
modelMap.put("password", user.getPassword());
redisService.setKey(mapName, modelMap);
//获取map集合
Map<Object, Object> modelMap1= redisService.getMapValue(mapName);
Object value= redisService.getValue(mapName, "name");
System.out.println(" value : "+value);
modelMap1.put("从缓存中根据key取到的value", value);
return modelMap1;
}
}
前台展示结果:
(4)删除以及获取值操作:
@GetMapping( "/delete.do")
@ResponseBody
public Map<Object, Object> deleteUser(HttpServletRequest request){
//获取即将删除的key值,这里我们做的批量删除
List<String> keys=new ArrayList<>();
keys.add("heheanme");
//开始执行删除操作
redisService.deleteData(keys);
//获取map集合
Map<Object, Object> modelMap1= redisService.getMapValue(mapName);
Object value= redisService.getValue(mapName, "name");
System.out.println(" value : "+value);
modelMap1.put("从缓存中根据key取到的value", value);
return modelMap1;
}
前台显示结果:
由此可见,操作成功
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。