Jedis是Redis官方推荐的java连接开发工具!

测试

1.导入对应的依赖

    <dependencies>
        <!-- 导入jedis的包 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.3.0</version>
        </dependency>
        <!-- fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
    </dependencies>

2.编码测试

2.1连接数据库

2.2操作命令

2.3断开连接

package com.luoyufeng;

import redis.clients.jedis.Jedis;

public class TestPing {
    public static void main(String[] args) {
        // 1. new Jedis对象
        Jedis jedis = new Jedis("你的ip地址",6379);
        // 2. 所有的命令就是我们所有学习的命令
        System.out.println(jedis.ping());
    }
}

常用的API

Key

package com.luoyufeng;

import redis.clients.jedis.Jedis;

import java.util.Set;

public class TestKey {

    public static void main(String[] args) {
        Jedis jedis = new Jedis("你的ip地址",6379);

        System.out.println("清空数据:" + jedis.flushDB());
        System.out.println("判断某个键是否存在:" + jedis.exists("username"));
        System.out.println("新增<'username,'luoyufeng'>键值对:" + jedis.set("username","luoyufeng"));
        System.out.println("新增<'password,'password'>键值对:" + jedis.set("password","password"));
        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());
    }
}

String

package com.luoyufeng;

import redis.clients.jedis.Jedis;

import java.util.concurrent.TimeUnit;

public class TestString {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("你的ip地址",6379);
        //-----------------------------------------------------------------------------
        jedis.flushDB();
        System.out.println("============增加数据============");
        System.out.println(jedis.set("key1","value1"));
        System.out.println(jedis.set("key2","value2"));
        System.out.println(jedis.set("key3","value3"));
        System.out.println("删除键key2:" + jedis.del("key2"));
        System.out.println("获取键key2:" + jedis.get("key2"));
        System.out.println("修改key1:" + jedis.set("key1","value1Changed"));
        System.out.println("获取key1的值:" + jedis.get("key1"));
        System.out.println("在key3后面加入值:" + jedis.append("key3","End"));
        System.out.println("key3的值:" + jedis.get("key3"));
        System.out.println("增加多个键值对:" + jedis.mset("key01","value01","key02","value02","key03","value03"));
        System.out.println("获取多个键值对:" + jedis.mget("key01","key02","key03"));
        System.out.println("获取多个键值对:" + jedis.mget("key01","key02","key03","key04"));
        System.out.println("删除多个键值对:" + jedis.del("key01","key02"));
        System.out.println("获取多个键值对:" + jedis.mget("key01","key02","key03"));
        //-----------------------------------------------------------------------------
        jedis.flushDB();
        System.out.println("============新增键值对防止覆盖原先值============");
        System.out.println(jedis.setnx("key1","value1"));
        System.out.println(jedis.setnx("key2","value2"));
        System.out.println(jedis.setnx("key2","value2-new"));
        System.out.println(jedis.get("key1"));
        System.out.println(jedis.get("key2"));
        //-----------------------------------------------------------------------------
        System.out.println("============新增键值对并设置有效时间============");
        System.out.println(jedis.setex("key3",2,"value3"));
        System.out.println(jedis.get("key3"));
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(jedis.get("key3"));
        //-----------------------------------------------------------------------------
        System.out.println("============获取原值,更新为新值============");
        System.out.println(jedis.getSet("key2","key2GetSet"));
        System.out.println(jedis.get("key2"));
        System.out.println("获得key的值的:" + jedis.getrange("key2",2,4));
    }
}

List

package com.luoyufeng;

import redis.clients.jedis.Jedis;

public class TestList {

    public static void main(String[] args) {
        Jedis jedis = new Jedis("你的ip",6379);

        jedis.flushDB();
        System.out.println("===============添加一个list===============");
        jedis.lpush("collections","ArrayList","Vector","Stack","WeakHashMap",
                "LinkedHashMap");
        jedis.lpush("collections","HashSet");
        jedis.lpush("collections","TreeSet");
        jedis.lpush("collections","TreeMap");
        // -1 代表最后一个元素  -2 表示倒数第二个元素
        System.out.println("collections的内容:" + jedis.lrange("collections",0,-1));
        System.out.println("collections区间0-3的元素:" + jedis.lrange("collections",0,3));
        System.out.println("=============================================");
        // 删除指定的值,第二个参数为删除的个数,后add进去后的值先被删除,类似于出栈
        System.out.println("删除指定元素个数:" + jedis.lrem("collections",2,"HashMap"));
        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","EnumMap"));
        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,"LinkedArrayList"));
        System.out.println("collections的内容:" + jedis.lrange("collections",0,-1));
        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));
    }
}

Set

package com.luoyufeng;

import redis.clients.jedis.Jedis;

public class TestSet {


    public static void main(String[] args) {
        Jedis jedis = new Jedis("你的ip",6379);
        jedis.flushDB();
        System.out.println("===================向集合中添加元素===================");
        System.out.println(jedis.sadd("eleSet","e1","e2","e3","e4","e5"));
        System.out.println(jedis.sadd("eleSet","e6"));
        System.out.println(jedis.sadd("eleSet","e6"));
        System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
        System.out.println("删除一个元素e0:" + jedis.srem("eleSet","e0"));
        System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
        System.out.println("删除两个元素e7和e6" + jedis.srem("eleSet","e7","e6"));
        System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
        System.out.println("随机的移除集合中的一个元素:" + jedis.spop("eleSet"));
        System.out.println("随机的移除集合中的一个元素:" + jedis.spop("eleSet"));
        System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
        System.out.println("eleSet中包含的所有元素个数为:" + jedis.scard("eleSet"));
        System.out.println("e3是否存在eleSet中:" + jedis.sismember("eleSet","e3"));
        System.out.println("e1是否存在eleSet中:" + jedis.sismember("eleSet","e1"));
        System.out.println("e5是否存在eleSet中:" + jedis.sismember("eleSet","e5"));
        System.out.println("=========================================================");
        System.out.println(jedis.sadd("eleSet1","e1","e2","e4","e3","e0","e8","e7","e5"));
        System.out.println(jedis.sadd("eleSet2","e1","e2","e4","e3","e0","e8"));
        System.out.println("将eleSet1中删除e1并存入eleSet3中:" + jedis.smove("eleSet1","eleSet3","e1"));
        System.out.println("将eleSet1中删除e2并存入eleSet3中:" + jedis.smove("eleSet1","eleSet3","e2"));
        System.out.println("eleSet1中的元素:" + jedis.smembers("eleSet1"));
        System.out.println("eleSet3中的元素:" + jedis.smembers("eleSet3"));
        System.out.println("===================集合运算===================");
        System.out.println("eleSet1中的元素:" + jedis.smembers("eleSet1"));
        System.out.println("eleSet2中的元素:" + jedis.smembers("eleSet2"));
        System.out.println("eleSet1和eleSet2的交集:" + jedis.sinter("eleSet1","eleSet2"));
        System.out.println("eleSet1和eleSet2的并集:" + jedis.sunion("eleSet1","eleSet2"));
        System.out.println("eleSet1和eleSet2的差集:" + jedis.sdiff("eleSet1","eleSet2"));
        //求交集并将交集保存到eleSet4的集合中
        jedis.sinterstore("eleSet4","eleSet1","eleSet2");
        System.out.println("eleSet2中的元素:" + jedis.smembers("eleSet4"));
    }

}

Hash

Zset

最后修改:2021 年 03 月 12 日
如果觉得我的文章对你有用,请随意赞赏