Jedis06

Jedis06,第1张

Jedis06

文章目录

一、导入依赖二、编码测试四、开放端口6379四、重启redis-server
使用Java来 *** 作Redis,Jedis是Redis官方推荐使用的Java连接redis的客户端。

一、导入依赖


    redis.clients
    jedis
    3.2.0



    com.alibaba
    fastjson
    1.2.70

二、编码测试

连接数据库

修改redis的配置文件

vim /usr/local/bin/myconfig/redis.conf

四、开放端口6379

firewall-cmd --zone=public --add-port=6379/tcp --permanet

重启防火墙服务

systemctl restart firewalld.service

四、重启redis-server

[root@AlibabaECS bin]# redis-server myconfig/redis.conf

*** 作命令
TestPing.java

public class TestPing {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.xx.xxx", 6379);
        String response = jedis.ping();
        System.out.println(response); // PONG
    }
}

事务

public class TestTX {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("39.99.xxx.xx", 6379);

        JSonObject jsonObject = new JSONObject();
        jsonObject.put("hello", "world");
        jsonObject.put("name", "kuangshen");
        // 开启事务
        Transaction multi = jedis.multi();
        String result = jsonObject.toJSONString();
        // jedis.watch(result)
        try {
            multi.set("user1", result);
            multi.set("user2", result);
            // 执行事务
            multi.exec();
        }catch (Exception e){
            // 放弃事务
            multi.discard();
        } finally {
            // 关闭连接
            System.out.println(jedis.get("user1"));
            System.out.println(jedis.get("user2"));
            jedis.close();
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5705992.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存