枚举+链对缓存实现以读取为主的请求接口

一、需求背景

      需要给一份问卷进行授权,授权的角色固定为4类,分别是: 农户、企业、金融机构、政府用户,这4类角色定义在字典里面,并且很少改动。这4类角色下面,可能有多个角色,这些角色通过字典的key关联到上述4个类型中的一个;
      在新增、编辑问卷的时候,需要把这4类所有的角色都展示出来,供管理员选择。

二、需求分析

      由于涉及到的数据很少改动,且主要以查询为主,于是第一想法就是通过 枚举+链对实现,具体实现如下:

三、实现过程

1. 枚举类

import lombok.Getter;
import lombok.Setter;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.stream.Stream;

import static com.xxx.xxx.utils.StreamUtil.forEachIfPresent;

/**
 * Author:   wxz
 * Date:     2023.11.29 10:35
 * Description: 问卷调查授权枚举
 */
public enum PaperAuthorRoleEnum {
   
    ANONYMITY("un-limit", "不限", Collections.singletonList("")),
    AGRICULTURAL("member", "农户",Collections.singletonList("member")),
    ENTERPRISE("enterprises", "企业", Collections.singletonList("agricultural-enterprises")),
    FINANCIAL_INSTITUTION("finance", "金融机构",FinanceRoleKeys()),
    GOVERNMENT_USERS("government", "政府用户", GovernmentRoleKeys()),
    ;
    /**
     * 角色类型
     */
    @Getter
    private final String type;
    /**
     * 角色类型名称
     */
    @Getter
    private final String typeName;

    /**
     * 角色id集合
     */
    @Getter
    private final List<String> roleKeys;

    /**
     * 角色列表
     */
    @Getter
    private final List<PaperRole> paperRoleList;
    /**
     * 角色缓存数据
     */
    private static Map<String, SysRole> roleCache = new ConcurrentHashMap<>();


    PaperAuthorRoleEnum(String type, String typeName, List<String> roleKeys) {
   
        this.type = type;
        this.typeName = typeName;
        this.roleKeys = roleKeys;
        this.paperRoleList = getRolesFromCache(roleKeys);
    }

    /**
     * 根据角色类型查找枚举
     * @param type 角色类型
     * @return 类型枚举
     */
    public static PaperAuthorRoleEnum of(String type) {
   
        if (type == null) {
   
            return null;
        }
        PaperAuthorRoleEnum[] values = PaperAuthorRoleEnum.values();
        for (PaperAuthorRoleEnum a : values) {
   
            if (a.type.equals(type)) {
   
                return a;
            }
        }
        return null;
    }

    /**
     * 金融机构角色keys
     */
    private static List<String> FinanceRoleKeys() {
   
        List<String> list = new ArrayList<>();
        //银行机构有5个等级
        for (int i = 1; i < 5; i++) {
   
            list.add("bank_level_" + i);
        }
        return list;
    }

    /**
     * 政府角色keys
     */
    private static List<String> GovernmentRoleKeys() {
   
        return new ArrayList<>(Arrays.asList("collector","government","government-role"));
    }

    /**
     * 获取所有枚举的keys
     * @return
     */
    private List<String> getAllRolKeys() {
   
        List<String> roleKeys = new ArrayList<>();
        roleKeys.addAll(Collections.singletonList("member"));
        roleKeys.addAll(Collections.singletonList("agricultural-enterprises"));
        roleKeys.addAll(FinanceRoleKeys());
        roleKeys.addAll(GovernmentRoleKeys());
        return roleKeys;
    }

    /**
     * 从缓存中获取角色列表
     * @param roleKeys
     * @return
     */
    private  List<PaperRole> getRolesFromCache(List<String> roleKeys) {
   
        List<PaperRole> paperRoles = new ArrayList<>();
        Set<String> notExitKeys = new HashSet<>();
        boolean sign = (roleCache == null);
        //如果已经存在缓存里面,则优先从缓存里面获取
        if (sign) {
   
            notExitKeys.addAll(getAllRolKeys());
            roleCache = new ConcurrentHashMap<>();
        } else {
   
            roleKeys.forEach(key -> {
   
                if (!roleCache.containsKey(key)) {
   
                    notExitKeys.add(key);
                } else {
   
                    PaperRole paperRole =
                            new PaperRole().setRoleId(roleCache.get(key).getRoleId()).setRoleName(roleCache.get(key).getRoleName());
                    paperRoles.add(paperRole);
                }
            });
        }
        //不存在于缓存的数据,重新从数据库里面查询出来,放在缓存里面
        if (notExitKeys.size() > 0) {
   
            Optional<List<SysRole>> sysRoles =
                    Optional.ofNullable(SpringUtils.getBean(SysRoleMapper.class).selectListByRoleKeys(new ArrayList<>(notExitKeys)));
           // forEachIfPresent()方法参考:  https://blog.csdn.net/qq_37844454/article/details/134964619
            forEachIfPresent(sysRoles,item->{
   
                roleCache.put(item.getRoleKey(), item);
                PaperRole paperRole = new PaperRole().setRoleId(item.getRoleId()).setRoleName(item.getRoleName());
                paperRoles.add(paperRole);
            });
        }
        return paperRoles;
    }
    /**
     * 获取所有的枚举
     */
    public static PaperAuthorRoleEnum[] getValues() {
   
        return new PaperAuthorRoleEnum[]{
   AGRICULTURAL, ENTERPRISE, FINANCIAL_INSTITUTION, GOVERNMENT_USERS};
    }
}

2. 控制器调用

    //使用线程安装的链对做缓存,避免读多写少的数据频繁请求数据库
    private static ConcurrentLinkedQueue<PaperAuthorTypeDTO> dtoQueue = new ConcurrentLinkedQueue<>();
    
    @GetMapping(value = "/getRoleList")
    public AjaxResult getRoleList() {
   
        if (CollectionUtils.isEmpty(dtoQueue)) {
   
            dtoQueue = new ConcurrentLinkedQueue<>();
            Arrays.stream(PaperAuthorRoleEnum.getValues()).forEach(anEnum->{
   
                dtoQueue.offer(new PaperAuthorTypeDTO().setRoleId(anEnum.getType()).setRoleName(anEnum.getTypeName()));
            });
        }
        return AjaxResult.success(dtoQueue);
    }

相关推荐

  1. +缓存实现读取为主请求接口

    2023-12-14 11:04:02       31 阅读
  2. golang实现

    2023-12-14 11:04:02       18 阅读
  3. C++

    2023-12-14 11:04:02       8 阅读
  4. 接口 VS ,如何管理常量?

    2023-12-14 11:04:02       28 阅读
  5. golang接口//结构使用示例

    2023-12-14 11:04:02       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-14 11:04:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-14 11:04:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-14 11:04:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-14 11:04:02       18 阅读

热门阅读

  1. 如何避免缓存穿透、缓存击穿、缓存雪崩?

    2023-12-14 11:04:02       42 阅读
  2. CUDA锁页内存的使用

    2023-12-14 11:04:02       37 阅读
  3. 跨线程保持上下文

    2023-12-14 11:04:02       38 阅读
  4. linux的命令和vi的使用

    2023-12-14 11:04:02       33 阅读
  5. xtu oj 1271 color

    2023-12-14 11:04:02       37 阅读
  6. Ceph入门到精通-TCP和UDP相关的指标

    2023-12-14 11:04:02       27 阅读
  7. ceph-deploy 遇到的使用问题

    2023-12-14 11:04:02       40 阅读
  8. 堆和栈的区别

    2023-12-14 11:04:02       34 阅读