redis初始化存储数据库字典表数据设计方案

随着项目访问量的并发,对数据库操作越来越多,为了优化系统,将原数据库字典表提到了JVM内存里,但是随着开始着手集群后,弊端就出来了!最近想着将字典数据放到redis里,并且能够尽量实现多场景便捷读取。

本文主要基于以下几点去设计:

1、数据归类,最好能像数据库中表结构一样归类

2、能够边界的查询出某个字典表LIST

3、能够便捷的根据字典表代码查询出某个字典项数据。

基于以上三点,我们想要的存储结构如下:

字典项:

数据:

下面就上实例代码:

public class Test {
    public static void main(String[] args) {
        //连接本地的 Redis 服务
        Jedis jedis = new Jedis("localhost");
        System.out.println("连接成功");
        //查看服务是否运行
        System.out.println("服务正在运行: "+jedis.ping());
        jedis.hset("dict:gx_yy_role","5","{\"name\":\"aaaa\"}");
        List<String> a = jedis.hvals("dict:gx_yy_role");
        System.out.println("服务正在运行: "+a.toString());
    }
}

注:这边dict是数据,gx_yy_role是表明,中间以:隔开,主要是为了分组。这分组方式是redis一个历史问题吧!主要是当时不支持hash,所以采用:去区分,咱们利用一下!

下面“5”是字典代码,另外一个是JSON格式的字符串!

1、写入方法:用hset方法。

2、读取list列表采用hvals方法

3、根据字典代码,读取某一项字典数据,采用hget方法

我们也可以用spring的

org.springframework.data.redis.core.RedisTemplate接口去写入读取数据:
package cn.**.**.**.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Map;

/**
 * @author <a href="mailto:[email protected]">mailto:[email protected]</a>
 * @version 2019/7/19 14:46
 * @description redis中字典表数据封装
 */
public class DictRedisUtils {
    org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(RedisUtils.class);
    @Autowired
    RedisUtils redisUtils;

    private RedisTemplate<String, Object> redisTemplate;

    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
    //数据类型 字典
    private final static String TYPE = "dict";

    /**
      * @description 存储单个字典项
      * @param
      * @return
      * @version 2.0, 2019/7/19 15:09
      * @author <a href="mailto:[email protected]">mailto:[email protected]</a>
      */
    public Boolean hset(String tableName,String key,Object value){
      if (StringUtils.isBlank(tableName) || StringUtils.isBlank(key) || value == null){
          return false;
      }
      String fKey = TYPE+":"+tableName;
      try {
          redisTemplate.opsForHash().put(fKey, key, value);
      }catch (Exception e){
          e.printStackTrace();
          return false;
      }
      return true;
    };

    /**
      * @description 批量保存字典信息
      * @param
      * @return
      * @version 2.0, 2019/7/19 15:26
      * @author <a href="mailto:[email protected]">mailto:[email protected]</a>
      */
    public Boolean hsets(String tableName,Map value){
        if (StringUtils.isBlank(tableName) || value == null || value.size() < 1){
            return false;
        }
        String fKey = TYPE+":"+tableName;
        try {
            redisTemplate.opsForHash().putAll(fKey, value);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    };

    /**
      * @description 查询单个字典项
      * @param
      * @return
      * @version 2.0, 2019/7/19 15:31
      * @author <a href="mailto:[email protected]">mailto:[email protected]</a>
      */
    public Object hget (String tableName, String key){
        if (StringUtils.isBlank(tableName) || StringUtils.isBlank(key)){
            return "";
        }
        String fKey = TYPE+":"+tableName;
        try {
            return redisTemplate.opsForHash().get(fKey, key);
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }

    /**
      * @description 查询字典表里所有字典项
      * @param
      * @return
      * @version 2.0, 2019/7/19 15:37
      * @author <a href="mailto:[email protected]">mailto:[email protected]</a>
      */
    public Object hget (String tableName){
        if (StringUtils.isBlank(tableName)){
            return "";
        }
        String fKey = TYPE+":"+tableName;
        try {
            return redisTemplate.opsForHash().values(fKey);
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }
}

上面是做的简单的读写,还有很多功能,如果需要可以去完善!

发布了57 篇原创文章 · 获赞 44 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/Tastill/article/details/96831216