前言:最近工作中使用到了redis緩存,故分享一點自己總結(jié)的東西,這篇文章使用的是StringRedisTemplate進(jìn)行學(xué)習(xí),這里值的說的是,(1)StringRedisTemplate在進(jìn)行批量刪除操作時我們需對template進(jìn)行序列化,(2)更新操作與添加操作一樣,接下來上代碼:
1.建立Spring boot項目,引入Redis依賴(pom.xml如下):
成都創(chuàng)新互聯(lián)是一家專注于成都做網(wǎng)站、網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)與策劃設(shè)計,寧陜網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:寧陜等地區(qū)。寧陜做網(wǎng)站價格咨詢:028-86922220
4.0.0
com.test
redis
0.0.1-SNAPSHOT
jar
redis
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.1.0.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-maven-plugin
2.編寫spring boot配置文件,這里我配置的內(nèi)容簡單,如需要其他的配置可以進(jìn)官網(wǎng)去查
#Redis
spring.redis.host=主機(jī)地址
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的各種操作方法進(jìn)行定義
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;
/**
* 存儲數(shù)據(jù)或修改數(shù)據(jù)
*
* @param modelMap
* @param mapName
*/
public void setKey(String mapName, Map modelMap) {
HashOperations hps = template.opsForHash();
hps.putAll(mapName, modelMap);
}
/**
* 獲取數(shù)據(jù)Map
*
* @param mapName
* @return
*/
public Map
(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
前臺展示結(jié)果:
(4)刪除以及獲取值操作:
@GetMapping( "/delete.do")
@ResponseBody
public Map deleteUser(HttpServletRequest request){
//獲取即將刪除的key值,這里我們做的批量刪除
List keys=new ArrayList<>();
keys.add("heheanme");
//開始執(zhí)行刪除操作
redisService.deleteData(keys);
//獲取map集合
Map modelMap1= redisService.getMapValue(mapName);
Object value= redisService.getValue(mapName, "name");
System.out.println(" value : "+value);
modelMap1.put("從緩存中根據(jù)key取到的value", value);
return modelMap1;
}
前臺顯示結(jié)果:
由此可見,操作成功