博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现自己的权限管理系统(十三):redis做缓存
阅读量:3888 次
发布时间:2019-05-23

本文共 2771 字,大约阅读时间需要 9 分钟。

一、自定义KEY

@Getterpublic enum CacheKeyConstants {    SYSTEM_ACLS,    USER_ACLS;}

二、实现

1、首先生成key

2、拿到连接池

3、调用redis的方法

public class SysCacheService {    @Resource(name = "redisPool")    private RedisPool redisPool;    //CacheKeyConstants指定前缀,系统级别    public void saveCache(String toSavedValue, int timeoutSeconds, CacheKeyConstants prefix) {        saveCache(toSavedValue, timeoutSeconds, prefix, null);    }    //用户级别的    public void saveCache(String toSavedValue, int timeoutSeconds, CacheKeyConstants prefix, String... keys) {        if (toSavedValue == null) {            return;        }        ShardedJedis shardedJedis = null;        try {            //首先生成key            String cacheKey = generateCacheKey(prefix, keys);            //拿到资源            shardedJedis = redisPool.instance();            //调用redis方法            shardedJedis.setex(cacheKey, timeoutSeconds, toSavedValue);        } catch (Exception e) {            log.error("save cache exception, prefix:{}, keys:{}", prefix.name(), JsonMapper.obj2String(keys), e);        } finally {            redisPool.safeClose(shardedJedis);        }    }    public String getFromCache(CacheKeyConstants prefix, String... keys) {        ShardedJedis shardedJedis = null;        String cacheKey = generateCacheKey(prefix, keys);        try {            shardedJedis = redisPool.instance();            String value = shardedJedis.get(cacheKey);            return value;        } catch (Exception e) {            log.error("get from cache exception, prefix:{}, keys:{}", prefix.name(), JsonMapper.obj2String(keys), e);            return null;        } finally {            redisPool.safeClose(shardedJedis);        }    }    //生成CacheKey方法    private String generateCacheKey(CacheKeyConstants prefix, String... keys) {        String key = prefix.name();        if (keys != null && keys.length > 0) {            key += "_" + Joiner.on("_").join(keys);        }        return key;    }}

三、缓存加在哪里?

缓存会造成数据的延缓:需要特定场景(假如加在拦截上,拦截会查多次数据库,更新的时候缓存会给管理员造成困惑,不知道是否做了更新)

这里举例:加载查询当前用户所有权限上,用户的每一次访问,都会从查是否拥有这个权限,所有可以加在这里,缓解数据库类压力

public List
getCurrentUserAclListFromCache() { int userId = RequestHolder.getCurrentUser().getId(); String cacheValue = sysCacheService.getFromCache(CacheKeyConstants.USER_ACLS, String.valueOf(userId)); //缓存中没有 if (StringUtils.isBlank(cacheValue)) { //数据库取 List
aclList = getCurrentUserAclList(); if (CollectionUtils.isNotEmpty(aclList)) { sysCacheService.saveCache(JsonMapper.obj2String(aclList), 600, CacheKeyConstants.USER_ACLS, String.valueOf(userId)); } return aclList; } return JsonMapper.string2Obj(cacheValue, new TypeReference
>() { }); }

 

 

转载地址:http://itphn.baihongyu.com/

你可能感兴趣的文章
Linux创始者托瓦兹谈及IoT --「安全在其次」
查看>>
传感器数据分析(Sensor Data Analytics)是什么?
查看>>
智能硬件开发如何选择低功耗MCU?
查看>>
阿里感悟(十)如何写好简历
查看>>
阿里感悟(十一)如何准备面试
查看>>
软件架构入门
查看>>
80 多个 Linux 系统管理员必备的监控工具
查看>>
OOD的原则
查看>>
Tool to trace local function calls in Linux
查看>>
Linux 下查询 DNS 服务器信息
查看>>
ulimit 里的 file size 的 block 单位是多少?
查看>>
linux下查看端口对应的进程
查看>>
将 gdb 用作函数跟踪器 (Function Tracer)
查看>>
原 GCC一些有用的技巧
查看>>
yum 变量追加的方法
查看>>
2倍速的下一代Bluetooth,「Bluetooth 5」发布
查看>>
Top 10 “Yum” installables to be productive as a developer on Red Hat Enterprise Linux
查看>>
[小技巧] Vim 如果去除 “existing swap file” 警告
查看>>
如何在linux下检测内存泄漏
查看>>
十年生聚,Vim 8.0 发布了!
查看>>