2021-01-25Redis存取list

2023-11-15

redis存取list类型数据

首先,为了避免高并发给myql数据库增加负担,所有大多数时候会利用redis数据库解决这个问题。

1
首先启动redis服务,在bin目录下打开powshell命令如下`

PS D:\devtools\Redis-x64-5.0.10> .\redis-server.exe .\redis.windows.conf

如下图表示成功启动redis服务
在这里插入图片描述
2、spring boot项目中引入pom依赖步骤略过
3、项目方法中:在将数据存到MySQL数据库的同时,也将数据存到Redis中

 public JsonResult addMoney(@RequestBody User user) {
        JsonResult res=new JsonResult(0,"存款成功");
        redisTemplate.boundValueOps(user.getUserid()+"_money").increment(user.getMoney());
        userSrv.addMoney(user);
        String name=(String) redisTemplate.boundValueOps(user.getUserid()+"_name").get();
        History history = new History(user.getUserid(), "存款",name, user.getMoney(), new Date());
        redisTemplate.opsForList().leftPush(user.getUserid(),history);
        //historySrv.insertHistory(history);
        return res;
    }

4、查询方法中,在redis中取list类型数据并对日期进行了格式化处理

 public JsonResult selHistory(@PathVariable String userid,String pageNum,String pageSize) {
        JsonResult res = new JsonResult(1, "查询成功");

        List<History> list = redisTemplate.opsForList().range(userid,0,-1);
        if (list.size() == 0) {
            res.setData(list);
        } else {
            List datas = new ArrayList(list.size());
            for (History history : list) {
                try {
                    Map map = BeanUtils.describe(history);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String createTime = sdf.format(history.getTime());
                    map.put("time", createTime);
                    map.remove("class");
                    datas.add(map);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
            res.setData(datas);
        }

        return res;
        }

5、利用vue测试数据:结果如下
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

2021-01-25Redis存取list 的相关文章

随机推荐