Redis -- Jedis

文章目录

  • 1.Jedis理解
  • 2.测试
    • 2.1 导入对应的依赖
    • 2.2 编码测试
  • 3.常用的KPI
    • 3.1对key操作的命令
    • 3.2 对String操作的命令
    • 3.3 对List操作的命令
    • 3.4 对Set操作的命令
    • 3.4 对Set操作的命令
  • 4.通过Jedis再次理解事务
    • 4.1 成功
    • 4.2 失败

1.Jedis理解

什么是Jedis?

是Redis官方推荐的java连接开发工具!使用Java操作Redis中间件!

2.测试

2.1 导入对应的依赖

<dependencies>
        <!--导入jedis的包-->
        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>4.2.3</version>
        </dependency>

        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.59</version>
        </dependency>
</dependencies>

2.2 编码测试

  • 连接数据库
  • 操作命令
  • 断开连接

开启本地redis:
Redis -- Jedis_第1张图片

package com.comple;

import redis.clients.jedis.Jedis;

public class TestPing {
    public static void main(String[] args) {
    //启动redis-server
        Jedis jedis = new Jedis("127.0.0.1",6379);
        System.out.println(jedis.ping());
    }
}

输出:
Redis -- Jedis_第2张图片

3.常用的KPI

3.1对key操作的命令

Jedis jedis = new Jedis("127.0.0.1",6379);
System.out.println("清空数据: "+jedis.flushDB());
System.out.println("判断某个键是否存在: "+jedis.exists("username"));
System.out.println("新增<'username','111'>的键值对: "+jedis.set("username","111"));
System.out.println("新增<'password','111'>的键值对: "+jedis.set("password","111"));
System.out.println("系统中的所有键如下: ");
Set<String> keys = jedis.keys("*");
System.out.println(keys);
System.out.println("删除键password: "+jedis.del("password"));
System.out.println("判断password键是否存在: "+jedis.exists("password"));
System.out.println("查看键username所存储的值的类型: "+jedis.type("username"));
System.out.println("随机返回key空间的一个: "+jedis.randomKey());
System.out.println("重命名key: "+jedis.rename("username","name"));
System.out.println("取出修改后的name: "+jedis.get("name"));
System.out.println("按索引查询: "+jedis.select(0));
System.out.println("删除当前选择数据库中的所有key: "+jedis.flushDB());
System.out.println("返回当前数据库中key的数目: "+jedis.dbSize());
System.out.println("删除所有数据库中的key: "+jedis.flushAll());

Redis -- Jedis_第3张图片

3.2 对String操作的命令

Jedis jedis = new Jedis("127.0.0.1",6379);

jedis.flushDB();
System.out.println("===============增加数据==================");
System.out.println(jedis.set("k1","v1"));
System.out.println(jedis.set("k2","v2"));
System.out.println(jedis.set("k3","v3"));
System.out.println("删除键k2: "+jedis.del("k2"));
System.out.println("获取键k2的值: "+jedis.get("k2"));
System.out.println("修改k1的值: "+jedis.set("k1","v1Changed"));
System.out.println("获取键k1的值: "+jedis.get("k1"));
System.out.println("在k3后面加入值: "+jedis.set("k3","end"));
System.out.println("获取键k3的值: "+jedis.get("k3"));
System.out.println("增加多个键值对: "+jedis.mset("k4","v4","k5","v5","k6","v6"));
System.out.println("获取多个键值对: "+jedis.mget("k4","k5","k6"));
System.out.println("获取多个键值对(包含不存在的): "+jedis.mget("k4","k5","k6","k7"));
System.out.println("删除多个键值对: "+jedis.del("k4","k5"));
System.out.println("获取多个键值对: "+jedis.mget("k4","k5","k6"));

Redis -- Jedis_第4张图片

jedis.flushDB();
System.out.println("==========新增键值对防止覆盖原先值===========");
System.out.println(jedis.setnx("k1","v1"));
System.out.println(jedis.setnx("k2","v2"));
System.out.println(jedis.setnx("k2","v2-new"));
System.out.println(jedis.get("k1"));
System.out.println(jedis.get("k2"));

System.out.println("==========新增键值对并设置有效时间=========");
System.out.println(jedis.setex("k3",2,"v3"));
System.out.println(jedis.get("k3"));
try {
        TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
        e.printStackTrace();
}
System.out.println(jedis.get("k3"));
System.out.println("==========获取原值,更新为新值===========");
System.out.println(jedis.getSet("k2","k2GetSet"));
System.out.println(jedis.get("k2"));
System.out.println("获取k2的值的子串: "+jedis.getrange("k2",2,4));

Redis -- Jedis_第5张图片

3.3 对List操作的命令

Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.flushDB();
System.out.println("==========添加一个list===========");     jedis.lpush("collections","ArrayList","Vector","Stack","HashMap","LinkedList");
jedis.lpush("collections","HashSet");
jedis.lpush("collections","TreeSet");
jedis.lpush("collections","TreeMap");
System.out.println("collections的内容: "+jedis.lrange("collections",0,-1));
System.out.println("collections区间0-3的元素: "+jedis.lrange("collections",0,3));

在这里插入图片描述

System.out.println("===========删除列表指定的值============");
System.out.println("删除指定元素的个数: "+jedis.lrem("collections",2,"HashMap"));
//count > 0 : 从表头开始向表尾搜索,移除与 value 相等的元素,数量为 count 。
//count < 0 : 从表尾开始向表头搜索,移除与 value 相等的元素,数量为 count 的绝对值。
//count = 0 : 移除表中所有与 value 相等的值。
System.out.println("collections的内容: "+jedis.lrange("collections",0,-1));
System.out.println("删除下标0-3区间之外的元素: "+jedis.ltrim("collections",0,3));
System.out.println("collections的内容: "+jedis.lrange("collections",0,-1));
System.out.println("collections列表出栈(左端): "+jedis.lpop("collections"));
System.out.println("collections的内容: "+jedis.lrange("collections",0,-1));
System.out.println("collections添加元素(从列表右端): "+jedis.rpush("collections","ArrayList"));
System.out.println("collections的内容: "+jedis.lrange("collections",0,-1));
System.out.println("collections列表出栈(右端): "+jedis.rpop("collections"));
System.out.println("collections的内容: "+jedis.lrange("collections",0,-1));
System.out.println("修改collections指定下标1的内容: "+jedis.lset("collections",1,"Stack"));
System.out.println("collections的内容: "+jedis.lrange("collections",0,-1));
System.out.println("===============");
System.out.println("collections的长度: "+jedis.llen("collections"));
System.out.println("获取collections下标为2的元素: "+jedis.lindex("collections",2));

Redis -- Jedis_第6张图片

System.out.println("==============排序==================");
jedis.lpush("sortedList","3","6","2","0","7","4");
System.out.println("sortedList排序前: "+jedis.lrange("sortedList",0,-1));
System.out.println(jedis.sort("sortedList"));
System.out.println("sortedList排序后: "+jedis.lrange("sortedList",0,-1));

Redis -- Jedis_第7张图片

3.4 对Set操作的命令

Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.flushDB();
System.out.println("============向集合中添加元素(不重复)=========");      System.out.println(jedis.sadd("set1","s1","s2","s3","s4","s5","s6"));
System.out.println(jedis.sadd("set1","s5"));
System.out.println(jedis.sadd("set1","s5"));
System.out.println("set1的所有元素为: "+jedis.smembers("set1"));

Redis -- Jedis_第8张图片

System.out.println("========删除元素==========");
System.out.println("删除一个元素s1: "+jedis.srem("set1","s1"));
System.out.println("set1的所有元素为: "+jedis.smembers("set1"));
System.out.println("删除两个元素s5,s6: "+jedis.srem("set1","s5","s6"));
System.out.println("set1的所有元素为: "+jedis.smembers("set1"));

Redis -- Jedis_第9张图片

System.out.println("========随机移除========");
System.out.println("随机移除集合中的一个元素: "+jedis.spop("set1"));
System.out.println("随机移除集合中的一个元素: "+jedis.spop("set1"));
System.out.println("set1的所有元素为: "+jedis.smembers("set1"));

Redis -- Jedis_第10张图片

System.out.println("============包含=============");
System.out.println("set1中包含元素的个数: "+jedis.scard("set1"));
System.out.println("s3是否包含在set1中: "+jedis.sismember("set1","s3"));
System.out.println("s1是否包含在set1中: "+jedis.sismember("set1","s1"));

Redis -- Jedis_第11张图片

System.out.println(jedis.sadd("set1","e1","e2","e3","e4","e5","e6","e7"));
System.out.println(jedis.sadd("set2","e1","e2","e3","e4","e5"));
System.out.println("=========删除并存入===========");
System.out.println("将set1中的e1删除并存入set3中: "+jedis.smove("set1","set3","e1"));
System.out.println("将set2中的e2删除并存入set3中: "+jedis.smove("set2","set3","e2"));
System.out.println("set1的所有元素为: "+jedis.smembers("set1"));
System.out.println("set3的所有元素为: "+jedis.smembers("set3"));

Redis -- Jedis_第12张图片

System.out.println("============集合运算================");
System.out.println("set1的所有元素为: "+jedis.smembers("set1"));
System.out.println("set2的所有元素为: "+jedis.smembers("set2"));
System.out.println("set1和set2的交集: "+jedis.sinter("set1","set2"));
System.out.println("set1和set2的并集: "+jedis.sunion("set1","set2"));
System.out.println("set1和set2的差集: "+jedis.sdiff("set1","set2"));
//求交集并将交集保存到dstkey:set4中
jedis.sinterstore("set4","set1","set2");
System.out.println("set4的所有元素为: "+jedis.smembers("set4"));

Redis -- Jedis_第13张图片

3.4 对Set操作的命令

Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.flushDB();
Map<String,String> map = new HashMap<String, String>();
map.put("k1","v1");
map.put("k2","v2");
map.put("k3","v3");
map.put("k4","v4");
//添加名称为hash(key)的hash元素
jedis.hmset("hash",map);
//向名称为hash的hash中添加key为k5,value为v5的元素
jedis.hset("hash","k5","v5");
System.out.println("散列hash的所有键值对: "+jedis.hgetAll("hash"));
System.out.println("散列hash的所有键: "+jedis.hkeys("hash"));
System.out.println("散列hash的所有值: "+jedis.hvals("hash"));
System.out.println("==========value加一个整数=========");
System.out.println("将k6保存的值加上一个整数,如果k6不存在则添加k6: "+jedis.hincrBy("hash","k6",6));
System.out.println("散列hash的所有键值对: "+jedis.hgetAll("hash"));
System.out.println("将k6保存的值加上一个整数,如果k6不存在则添加k6: "+jedis.hincrBy("hash","k6",3));
System.out.println("散列hash的所有键值对: "+jedis.hgetAll("hash"));
System.out.println("==========删除===========");
System.out.println("删除一个或多个键值对: "+jedis.hdel("hash","k1","k2"));
System.out.println("散列hash的所有键值对: "+jedis.hgetAll("hash"));
System.out.println("=========================");
System.out.println("键值对的个数: "+jedis.hlen("hash"));
System.out.println("==============包含===============");
System.out.println("判断hash中是否存在k2: "+jedis.hexists("hash","k3"));
System.out.println("判断hash中是否存在k8: "+jedis.hexists("hash","k8"));
System.out.println("===========获取值===========");
System.out.println("获取hash中key为k3的值: "+jedis.hmget("hash","k3"));
System.out.println("获取hash中key为k3和k4的值: "+jedis.hmget("hash","k3","k4"));

Redis -- Jedis_第14张图片

4.通过Jedis再次理解事务

4.1 成功

public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1",6379);
        jedis.flushDB();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username","111");
        jsonObject.put("password","111");

        Transaction multi = jedis.multi();//开启事务
        String res = jsonObject.toJSONString();


        try {
            multi.set("user1",res);
            multi.set("user2",res);
            multi.exec();//执行事务
        } catch (Exception e) {
            multi.discard();//取消事务
            e.printStackTrace();
        } finally {
            System.out.println(jedis.get("user1"));
            System.out.println(jedis.get("user2"));
            jedis.close();//关闭连接
        }
    }

Redis -- Jedis_第15张图片

4.2 失败

try {
        multi.set("user1",res);
        multi.set("user2",res);

        int i = 1/0;//代码执行异常,事务执行失败!!!
        multi.exec();//执行事务
} catch (Exception e) {
        multi.discard();//取消事务
        e.printStackTrace();
} finally {
        System.out.println(jedis.get("user1"));
        System.out.println(jedis.get("user2"));
        jedis.close();//关闭连接
}

Redis -- Jedis_第16张图片

你可能感兴趣的:(redis,java,数据库)