使用java代码操作Redis

一、在pom.xml里添加依赖

      <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
      </dependency>

二、连接Redis

Jedis jedis =new Jedis("192.168.124.129",6379);
        jedis.auth("123456");
        //测试连接(打印pong就算连接成功)
        System.out.println(jedis.ping());

三、操作数据

//操作字符串
        //插入数据
//        jedis.set("aaa", "zs");
        //获取数据
//        System.out.println(jedis.get("aaa"));

     //操作哈希
        //插入数据
//        jedis.hset("user1  ","uname","ls");
//        jedis.hset("useq r1","sex","女");
        //获取数据
//        System.out.println(jedis.hget("user1", "sex"));
//        System.out.println(jedis.hgetAll("user1"));

    //操作列表(堆栈结构)
     jedis.lpush("hobby","a","b","c","d");
     //从栈顶开始取值
        System.out.println(jedis.lpop("hobby"));
     //从栈底开始取值
        System.out.println(jedis.rpop("hobby"));

四、运用

@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //首页第一次是读取数据库,后面读取缓存(在没有增删改的情况下)
        Jedis jedis =new Jedis("192.168.124.129",6379);
        jedis.auth("123456");
        //从缓存中获取当前登录的用户信息
        Map<String,String> currentUser =jedis.hgetAll("currentUser");
        if (currentUser !=null && currentUser.size()>0){
            req.setAttribute("msg","从缓存中获取数据");
            req.setAttribute("currentUser",currentUser);
        }
        else {
            //第一次登录,第一次访问首页数据
            req.setAttribute("msg","从数据库中获取数据");
            String uname ="tianqi";
            String upass ="123456";
            //接下来把数据中的对应对象存储到缓存中
            jedis.hset("currentUser","uname","tianqi");
            jedis.hset("currentUser","upass","123456");
            //此时能获取到值,原因是上面已经将数据存储到缓存中
            currentUser = jedis.hgetAll("currentUser");
            req.setAttribute("currentUser",currentUser);

        }
        req.getRequestDispatcher("/home.jsp").forward(req,resp);
    }

五、实战

  1.首页第一次是读取数据库,后面读取缓存(在没有增删改的情况):

 // 从Redis中获取数据
    String bolgListALL =jedis.get("blogList");
                if(bolgListALL != null && bolgListALL.length()>0) {
        System.out.println("使用Redis查询");
        request.setAttribute("blogList", JSON.parse(bolgListALL));
    }else {
// 从数据库中查询数据
        List<Map<String, Object>> blogList = this.blogDao.freemarker_list(title, null);
// 放入缓存
        jedis.set("blogList", JSON.toJSONString(blogList));
        //传到jsp页面
        request.setAttribute("blogList", blogList);
        System.out.println("从缓存中拿数据");

  2.增删改的时候,要顺带更新缓存,下一次再次访问首页要保证redis中数据跟mysql数据是一致:

public String add() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Map parameterMap = request.getParameterMap();
        try {
            // 将博客添加到数据库中 
            this.blogDao.add(parameterMap);
            //清除Redis缓存
            jedis.del("blogList");
            // 获取当前博客的id
            int maxId = (int) this.blogDao.maxId();
            // 添加到lucene 中
            addIndex(maxId + "", parameterMap);
            // 进行网页静态化
            addStaticPage(maxId + "", parameterMap);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return "blogList";
    }
原文地址:https://www.cnblogs.com/psyu/p/11550776.html