zoukankan      html  css  js  c++  java
  • redis入门案例

    1.引入jar包

    <!-- jedis -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>${jedis.version}</version>
            </dependency>
    
            <!--添加spring-datajar包  -->
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
                <version>1.4.1.RELEASE</version>
            </dependency>

    2.String类型测试

    //测试字符串 IP:6379
        @Test
        public void testString(){
            Jedis jedis = new Jedis("192.168.126.166",6379);
            jedis.set("aa","1807班");
            System.out.println(jedis.get("aa"));
        }

    3.hash类型数据测试

    //测试hash数据存储
        @Test
        public void testHash(){
            Jedis jedis = new Jedis("192.168.126.166",6379);
            jedis.hset("dog", "id", "100");
            jedis.hset("dog", "name", "中华田园犬");
            jedis.hset("dog", "age", "88");
            System.out.println(jedis.hget("dog", "name"));
            System.out.println(jedis.hgetAll("dog"));
            
        }

    4.List类型测试

    //测试List集合
        @Test
        public void testList(){
            Jedis jedis = new Jedis("192.168.126.166",6379);
            jedis.lpush("idList", "1","2","3");
            System.out.println(jedis.lpop("idList"));
        }

    5.事务控制

    //redis中事务控制
        @Test
        public void testTx(){
            Jedis jedis = new Jedis("192.168.126.166",6379);
            Transaction transaction = jedis.multi();    //开启事务.
            transaction.set("aa", "aaa");
            transaction.set("bb", "bbb");
            transaction.exec();    //事务提交
            System.out.println("执行成功!!!");
            //transaction.discard();    //事务回滚
        }
  • 相关阅读:
    算法图解之散列表
    算法图解之快速排序
    算法图解之分而治之
    __setitem__,__getitem,__delitem__的作用
    算法图解之递归
    Python开发不可不知的虚拟环境
    静态属性property的本质和应用
    SQLmap详解
    windows提权备忘录
    linux提权备忘录
  • 原文地址:https://www.cnblogs.com/gxlaqj/p/11588201.html
Copyright © 2011-2022 走看看