Redis (二)_ jedis的使用

本文介绍了如何使用Jedis客户端开发包连接Redis服务器并进行简单的键值对操作。通过两个示例演示了直接创建Jedis实例及使用Jedis连接池的方式进行数据存取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Jedis 是 Redis 官方首选的 Java 客户端开发包

  • 启动redis服务 (参考上篇文章)

·

java代码
  • 新建一个maven的java项目
  • 引入依赖
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
        </dependency>
  • 建立测试类
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Auther: curry
 * @Date: 2018/5/31 23:04
 * @Description:
 */

public class Test {
    @org.junit.Test
    public void demo1(){
        Jedis jedis = new Jedis("192.168.142.128",6379);
        jedis.set("name", "test");
        String name = jedis.get("name");
        System.err.println(name);
        jedis.close();

    }

    @org.junit.Test
    public void demo2(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(30);
        config.setMaxIdle(10);
        JedisPool jedisPool = new JedisPool(config,"192.168.142.128",6379);
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            jedis.set("name", "毛毛");
            String value = jedis.get("name");
            System.out.println(value);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(jedis != null){
                jedis.close();
            }
            if(jedisPool != null){
                jedisPool.destroy();
            }
        }
    }

}
  • 运行结果

enter image description here

源码下载:github

今天电脑开着虚拟机和idea,一直内存爆表,没法运行了快。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值