介绍
Redis的缓存是Redis内部用于存储键值对 数据结构的一种基础数据结构。在Redis中,所有的键值对都是通过字典这种数据结构来存储的,在Redis 中 字典是很重要的一个角色,不仅可以用于存储数据库中的键值对,还能处理其他复杂的数据结构,例如哈希、集合等复杂的数据结构。
一、Redis字典缓存的关键点
二、示例代码
创建实体类
package com.wyl.redis.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
/**
* @Description
* @Author wuyilong
* @Date 2024-07-03
*/
@Data
@TableName("full_city")
@Entity
@Table(name="full_city")
public class FullCity extends Model<FullCity> {
private static final long serialVersionUID = 1L;
/**
* 主键id
*/
@TableId(value = "id", type = IdType.AUTO)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
* 名称
*/
@TableField("name")
private String name;
/**
* 行政编码
*/
@TableField("code")
private String code;
/**
* 全名称
*/
@TableField("full_name")
private String fullName;
/**
* 级别,1省,2市,3区,4街道
*/
@TableField("level")
private Integer level;
/**
* 创建时间
*/
@TableField("create_time")
private Date createTime;
/**
* 中心点
*/
@TableField("center")
private String center;
/**
* 是否被撤销,0否,1是
*/
@TableField("is_revoke")
private Integer isRevoke;
/**
* 父级编码
*/
private String parentCode;
@Override
public Serializable pkVal() {
return this.id;
}
}
service层
package com.wyl.redis.service.impl;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.map.MapUtil;
import com.wyl.redis.bean.DictionaryBean;
import com.wyl.redis.constant.DictionaryConst;
import com.wyl.redis.entity.FullCity;
import com.wyl.redis.service.DictionaryOperate;
import com.wyl.redis.service.FullCityService;
import com.wyl.redis.vo.FullCityVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @Description
* @Author WuYiLong
* @Date 2024/7/3 17:36
*/
@Slf4j
@Service
public class FullCityOperate implements DictionaryOperate {
@Autowired
private FullCityService fullCityService;
@Autowired
private RedisTemplate redisTemplate;
@Override
public List list(String key) {
if(!redisTemplate.hasKey(key)) {
List<FullCity> list = fullCityService.list();
List<DictionaryBean> dictionaryBeans = list.stream().map(m -> {
DictionaryBean dictionaryBean = new DictionaryBean();
dictionaryBean.setCode(m.getCode());
dictionaryBean.setName(m.getName());
dictionaryBean.setLevel(m.getLevel());
dictionaryBean.setParentCode(m.getParentCode());
return dictionaryBean;
}).collect(Collectors.toList());
redisTemplate.opsForValue().set(key,dictionaryBeans);
return dictionaryBeans;
}
List<DictionaryBean> list = (List<DictionaryBean>)redisTemplate.opsForValue().get(key);
return list;
}
@Override
public List<Tree<String>> tree(String key) {
if(!redisTemplate.hasKey(key)) {
List<FullCity> list = fullCityService.list();
List<Tree<String>> build = TreeUtil.build(list, "0", (t1, t2) -> {
t2.setId(t1.getCode());
t2.setName(t1.getName());
t2.setParentId(t1.getParentCode());
});
redisTemplate.opsForValue().set(key,build);
return build;
}
List<Tree<String>> trees = (List<Tree<String>>)redisTemplate.opsForValue().get(key);
return trees;
}
@Override
public String codeNameMap(String key, String code) {
if(!redisTemplate.opsForHash().hasKey(key,code)) {
FullCityVo fullCityVo = fullCityService.getByCode(code);
if(fullCityVo != null) {
redisTemplate.opsForHash().putIfAbsent(key,fullCityVo.getCode(),fullCityVo.getName());
return fullCityVo.getName();
}
return null;
}
String name = (String)redisTemplate.opsForHash().get(key, code);
return name;
}
@Override
public String nameCodeMap(String key, String name) {
if(!redisTemplate.opsForHash().hasKey(key,name)) {
FullCityVo fullCityVo = fullCityService.getByFullName(name);
if(fullCityVo != null) {
redisTemplate.opsForHash().putIfAbsent(key,fullCityVo.getFullName(),fullCityVo.getCode());
return fullCityVo.getCode();
}
return null;
}
String code = (String)redisTemplate.opsForHash().get(key, name);
return code;
}
@Override
public String supportType() {
return DictionaryConst.FULL_CITY;
}
}
package com.wyl.redis.service.impl;
import cn.hutool.core.lang.tree.Tree;
import com.wyl.redis.constant.DictionaryConst;
import com.wyl.redis.exception.BusinessException;
import com.wyl.redis.service.DictionaryOperate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.*;
/**
* @Description
* @Author WuYiLong
* @Date 2024/7/3 17:23
*/
@Slf4j
@Component
public class DictionaryService implements ApplicationContextAware {
private Map<String,DictionaryOperate> dictionaryMaps = new HashMap<>();
@Autowired
private RedisTemplate redisTemplate;
public DictionaryOperate buildDictionaryOperate(String key) {
DictionaryOperate dictionaryOperate = dictionaryMaps.get(key);
if(dictionaryOperate == null) {
throw new BusinessException("字典的key不存在");
}
return dictionaryOperate;
}
public List list(String key) {
String listKey = DictionaryConst.DIC+key+DictionaryConst.LIST;
if(key.contains(":")) {
String[] split = key.split(":");
key = split[0];
listKey = DictionaryConst.DIC+key+DictionaryConst.LIST+":"+split[1];
}
List list = buildDictionaryOperate(key).list(listKey);
return list;
}
public List<Tree<String>> tree(String key) {
String listKey = DictionaryConst.DIC+key+DictionaryConst.TREE;
if(key.contains(":")) {
String[] split = key.split(":");
key = split[0];
listKey = DictionaryConst.DIC+key+DictionaryConst.TREE+":"+split[1];
}
List<Tree<String>> tree =buildDictionaryOperate(key).tree(listKey);
return tree;
}
public String codeNameMap(String key, String code) {
String name = buildDictionaryOperate(key).codeNameMap(DictionaryConst.DIC+key+":codeNameMap", code);
return name;
}
public String nameCodeMap(String key, String name) {
String code = buildDictionaryOperate(key).nameCodeMap(DictionaryConst.DIC+key+":nameCodeMap", name);
return code;
}
public void refresh() {
Set keys = redisTemplate.keys("dic*");
keys.forEach(v->{
redisTemplate.delete(v);
});
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, DictionaryOperate> dictionaryOperateMap = applicationContext.getBeansOfType(DictionaryOperate.class);
dictionaryOperateMap.forEach((k,v)->{
dictionaryMaps.put(v.supportType(),v);
});
}
}
controller层
package com.wyl.redis.controller;
import com.wyl.common.bean.ResponseData;
import com.wyl.redis.service.impl.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
*
* @Description
* @Author WuYiLong
* @Date 2024/7/8 10:21
*/
@Api(tags = "字典api")
@RestController
@RequestMapping(value = "dictionary")
public class DictionaryController {
@Autowired
private DictionaryService dictionaryService;
@ApiOperation(value = "字典刷新")
@GetMapping(value = "refresh")
public ResponseData refresh() {
dictionaryService.refresh();
return ResponseData.success();
}
@ApiOperation(value = "字典列表")
@GetMapping(value = "list")
public ResponseData list(String key) {
return ResponseData.successInstance(dictionaryService.list(key));
}
@ApiOperation(value = "字典树")
@GetMapping(value = "tree")
public ResponseData tree(String key) {
return ResponseData.successInstance(dictionaryService.tree(key));
}
@ApiOperation(value = "根据code获取名称")
@GetMapping(value = "codeNameMap")
public ResponseData codeNameMap(String key, String code) {
return ResponseData.successInstance(dictionaryService.codeNameMap(key,code));
}
@ApiOperation(value = "根据名称获取code")
@GetMapping(value = "nameCodeMap")
public ResponseData nameCodeMap(String key, String name) {
return ResponseData.successInstance(dictionaryService.nameCodeMap(key, name));
}
}
测试:
根据code获取名称
字典列表
字典树:
原文始发于微信公众号(Java技术前沿):Spring Boot 实战: springboot集成redis之字典缓存
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/299652.html