zoukankan      html  css  js  c++  java
  • java代码操作Redis

    1.导入需要的pom依赖

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <!--servlet  -->
        <servlet.version>4.0.0</servlet.version>
        <!--mysql-->
        <mysql.version>5.1.44</mysql.version>
        <!--6.jstl+standard  -->
        <jstl.version>1.2</jstl.version>
        <standard.version>1.1.2</standard.version>
        <!--7.tomcat-jsp-api  -->
        <tomcat.jsp.version>8.0.46</tomcat.jsp.version>
        <!--jackson-->
        <jackson.version>2.9.3</jackson.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>
    
    
        <dependency>
          <groupId>redis.clients</groupId>
          <artifactId>jedis</artifactId>
          <version>2.9.0</version>
        </dependency>
    
    
        <!--servlet  -->
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>${servlet.version}</version>
          <scope>provided</scope>
        </dependency>
    
    
        <!--6.jstl+standard  -->
        <!-- https://mvnrepository.com/artifact/jstl/jstl -->
        <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>${jstl.version}</version>
        </dependency>
    
    
        <!-- https://mvnrepository.com/artifact/taglibs/standard -->
        <dependency>
          <groupId>taglibs</groupId>
          <artifactId>standard</artifactId>
          <version>${standard.version}</version>
        </dependency>
    
        <!--7.tomcat-jsp-api  -->
        <dependency>
          <groupId>org.apache.tomcat</groupId>
          <artifactId>tomcat-jsp-api</artifactId>
          <version>${tomcat.jsp.version}</version>
        </dependency>
    
    
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-annotations</artifactId>
          <version>${jackson.version}</version>
        </dependency>
    
    
    
        <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
        <dependency>
          <groupId>dom4j</groupId>
          <artifactId>dom4j</artifactId>
          <version>1.6.1</version>
        </dependency>
    
    
        <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
        <dependency>
          <groupId>commons-beanutils</groupId>
          <artifactId>commons-beanutils</artifactId>
          <version>1.9.4</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
          <version>1.2</version>
        </dependency>

    2.开启虚拟机

     

    3.java代码操作redis

    package com.hmc.demo;
    
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.ScanResult;
    import redis.clients.jedis.Tuple;
    
    import java.util.List;
    
    /**
     * @author胡明财
     * @site www.xiaomage.com
     * @company xxx公司
     * @create  2019-10-13 10:57
     */
    public class Demo {
        public static void main(String[] args) {
            Jedis Jedis = new Jedis("192.168.197.128", 6379);
            Jedis.auth("123456");
             System.out.println(Jedis.ping());
    
            //选择数据库
            String select = Jedis.select(0);
           // Jedis.del("booklist");
            //Strng 字符串
            //set
            //Jedis.set("name","张三");
            //get获取
            System.out.println(Jedis.get("booklist"));
            //type获取类型
            //System.out.println(Jedis.type("name"));
            //del删除
            //Jedis.del("name");
            //expire设置超时时间
            //Jedis.expire("name",10);
    
            //hash(哈希)
    
    //        Jedis.hset("student","name","李四");
    //        Jedis.hset("student","age","13");
    //        Jedis.hset("student","addres","长沙");
    
    //        System.out.println(Jedis.hget("student", "name"));
    
            //list列表
    //        Jedis.lpush("names","张三","李四","王五");
    //        Long names = Jedis.llen("names");
    //        for (int i=0;i<names;i++){
            //从左到右
            //     System.out.println(Jedis.lpop("names"));
            //从左到右
            //System.out.println(Jedis.rpop("names"));
       // }
    
    
            //set集合
           // Jedis.sadd("username","admin","zs","ls");
    //        ScanResult<String> username = Jedis.sscan("username", 0);
    //        List<String> result = username.getResult();
    //        for (String s : result) {
    //            System.out.println(s);
    //        }
    
            //zset有序集合
    //        Jedis.zadd("sex",70,"男");
    //        Jedis.zadd("sex",80,"女");
    //        Jedis.zadd("sex",90,"不男不女");
    //        ScanResult<Tuple> zscan = Jedis.zscan("sex", 0);
    //        List<Tuple> result = zscan.getResult();
    //        for (Tuple tuple : result) {
    //            System.out.println("sex="+tuple.getElement()+",score="+tuple.getScore());
    //
    //        }
    
    
        }
    }

    已缓存的数据

    4.redis缓存数据库(利用查询进行缓存)

      public String list(HttpServletRequest request,HttpServletResponse response) throws Exception {
             //建立连接
              Jedis jedis = new Jedis("192.168.197.128", 6379);
              jedis.auth("123456");
               //获取缓存数据
                String booklist = jedis.get("booklist");
                if(booklist==null || "".equals(booklist)){
                     List<Book> list = this.bookdao.list(book, null);
                     System.out.println(list);
                     jedis.set("booklist",list.toString());
                     request.setAttribute("booklist", list);
                     return "list";
                }
                
                else {
                    //[{"bid":33,"bname":"圣墟第33章","price":33.0}
                    System.out.println(booklist);
                    request.setAttribute("booklist", booklist);
                    return "list";
                }
                
               
         }
         

    这是查询出来的数据

    看是否已经缓存成功

     因缓存数据过大,无法在右侧显示

    接下来用java代码看一下是否有数据

    清空缓存策略(执行增删改的时候)del即可

  • 相关阅读:
    spring学习(一)--spring简介
    The Evolved Packet Core
    Git版本控制入门学习
    分家后中国移动运营商2G/3G频率分配
    English name
    中国互联网五大势力竞争力报告
    ping
    mysql架构
    MySQL存储引擎
    79款 C/C++开发工具开源软件
  • 原文地址:https://www.cnblogs.com/xmf3628/p/11669317.html
Copyright © 2011-2022 走看看