资讯专栏INFORMATION COLUMN

二、Rdeis Jedis

caige / 2884人阅读

摘要:一八种数据类型二三四集群一概述是官方推荐的连接开发工具。使可以操作中间件。


一、Jedis 概述

Jedis 是 Redis 官方推荐的 Java 连接开发工具。使 Java 可以操作 Redis 中间件。


二、Jedis 示例

1. Ping

public class TestPing {    public static void main(String[] args) {        // 创建连接        Jedis jedis = new Jedis("127.0.0.1", 6379);        System.out.printf("测试连接: %s", jedis.ping()).println();        // 关闭连接        jedis.close();    }}

2. Password

public class TestPassword {    public static void main(String[] args) {        // 创建连接        Jedis jedis = new Jedis("127.0.0.1", 6379);        // 未设置密码,跳过//        System.out.printf("验证密码: %s", jedis.auth("123456")).println();        // 连接        jedis.connect();        // 断开连接        jedis.disconnect();        // 清除所有key        jedis.flushAll();        // 关闭连接        jedis.close();    }}

3. Key

public class TestKey {    public static void main(String[] args) {        // 创建连接        Jedis jedis = new Jedis("127.0.0.1", 6379);        System.out.println("清空数据: " + jedis.flushDB());        System.out.println("判断username键是否存在: " + jedis.exists("username"));        System.out.println("新增<"username","qs">的键值对: " + jedis.set("username", "qs"));        System.out.println("新增<"password","123">的键值对: " + jedis.set("password", "123"));        System.out.print("系统中所有的键如下: ");        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空间中一个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());        // 关闭连接        jedis.close();    }}

4. String

public class TestString {    public static void main(String[] args) {        // 创建连接        Jedis jedis = new Jedis("127.0.0.1", 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("修改key1: " + jedis.set("key1", "value1Changed"));        System.out.println("获取key1的值: " + jedis.get("key1"));        System.out.println("删除键key2: " + jedis.del("key2"));        System.out.println("获取键key2: " + jedis.get("key2"));        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("获得key2的值的字串: " + jedis.getrange("key2", 2, 4));        // 关闭连接        jedis.close();    }}

5. List

public class TestList {    public static void main(String[] args) {        // 创建连接        Jedis jedis = new Jedis("127.0.0.1", 6379);        jedis.flushDB();        System.out.println("===========添加一个list===========");        jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");        jedis.lpush("collections", "HashSet");        jedis.lpush("collections", "TreeSet");        jedis.lpush("collections", "TreeMap");        // -1代表倒数第一个元素,-2代表倒数第二个元素,end为-1表示查询全部        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添加元素,从列表右端,与lpush相对应: " + 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("===============================");        System.out.println("collections的长度: " + jedis.llen("collections"));        System.out.println("获取collections下标为2的元素: " + jedis.lindex("collections", 2));        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));        // 关闭连接        jedis.close();    }}

6. Set

public class TestSet {    public static void main(String[] args) {        // 创建连接        Jedis jedis = new Jedis("127.0.0.1", 6379);        jedis.flushDB();        System.out.println("============向集合中添加元素(不重复)============");        System.out.println(jedis.sadd("eleSet", "e1", "e2", "e4", "e3", "e0", "e8", "e7", "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(&           
               
                                           
                       
                 

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/124027.html

相关文章

  • Redis学习笔记:使用Jedis简单操作reids数据库

    摘要:,中导入包在中新建一个,在项目下创建文件夹,将包复制到中,并将包添加到编译环境中右键,目录结构大致如下,单例连接在外界访问服务时要开放防火墙的端口,不然会访问不到下输入当然也可以关闭防火墙。 1,Eclipse中导入jar包 在Eclipse中新建一个Java Project,在项目下创建lib文件夹,将jar包复制到lib中,并将jar包添加到编译环境中(右键lib-->Build ...

    sushi 评论0 收藏0
  • Redis学习笔记:使用Jedis简单操作reids数据库

    摘要:,中导入包在中新建一个,在项目下创建文件夹,将包复制到中,并将包添加到编译环境中右键,目录结构大致如下,单例连接在外界访问服务时要开放防火墙的端口,不然会访问不到下输入当然也可以关闭防火墙。 1,Eclipse中导入jar包 在Eclipse中新建一个Java Project,在项目下创建lib文件夹,将jar包复制到lib中,并将jar包添加到编译环境中(右键lib-->Build ...

    MangoGoing 评论0 收藏0
  • Redis学习笔记:使用Jedis简单操作reids数据库

    摘要:,中导入包在中新建一个,在项目下创建文件夹,将包复制到中,并将包添加到编译环境中右键,目录结构大致如下,单例连接在外界访问服务时要开放防火墙的端口,不然会访问不到下输入当然也可以关闭防火墙。 1,Eclipse中导入jar包 在Eclipse中新建一个Java Project,在项目下创建lib文件夹,将jar包复制到lib中,并将jar包添加到编译环境中(右键lib-->Build ...

    陈伟 评论0 收藏0
  • Java基础知识整理之操作Redis(

    摘要:操作之连接以及简单操作下载对应的驱动包下载创建一个连接类连接主机地址端口号登录密码连接服务器权限认证连接完成会返回缓存链接错误查询所有中的查询所有的为通配符清除所有的中的是清除所有的的命令如果清理完成,会返回完整的代码声明对象测试地址端口密 Java操作Redis之连接以及简单操作 1.下载对应的驱动包 下载 jedis.jar :https://mvnrepository.com/a...

    mingde 评论0 收藏0
  • Java基础知识整理之操作Redis(

    摘要:操作之连接以及简单操作下载对应的驱动包下载创建一个连接类连接主机地址端口号登录密码连接服务器权限认证连接完成会返回缓存链接错误查询所有中的查询所有的为通配符清除所有的中的是清除所有的的命令如果清理完成,会返回完整的代码声明对象测试地址端口密 Java操作Redis之连接以及简单操作 1.下载对应的驱动包 下载 jedis.jar :https://mvnrepository.com/a...

    winterdawn 评论0 收藏0

发表评论

0条评论

caige

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<