@Java中使用Jedis操作Redis之一

依赖的jar包:jedis

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

使用单连接

此方式仅建议用于开发环境做调试用。

public class TestJedis {
    
    public static void main(String[] args) {
        // 创建连接,需要制定ip和端口号
        String host = "127.0.0.1";
        int port = 6379;
        Jedis client = new Jedis(host, port);

        // 执行set指令,jedis中一个命令对应一个方法~
        String result = client.set("key-string", "Hello, Redis!");
        System.out.println( String.format("set指令执行结果:%s", result) );

        // 执行get指令
        String value = client.get("key-string");
        System.out.println( String.format("get指令执行结果:%s", value) );
client.close();//注意关闭连接 } }

运行上述代码,控制台输出:

set指令执行结果:OK
get指令执行结果:Hello, Redis!

使用连接池

此方式适用于仅使用单个Redis实例的场景。创建连接是一个比较耗费资源的操作,如果每次使用redis都要创建一个连接是不合适的!!所以使用连接池!!

public class TestJedis {

    public static void main(String[] args) {
        // 生成连接池配置信息
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(10);
        config.setMaxActive(10);
        config.setMaxWait(20);
        
        // 在应用初始化的时候生成连接池
        JedisPool pool = new JedisPool(config, "127.0.0.1", 6379);

        // 在业务操作时,从连接池获取连接
        Jedis client = pool.getResource();
        try {
            // 执行指令
            String result = client.set("key-string", "Hello, Redis!");
            System.out.println(String.format("set指令执行结果:%s", result));
            String value = client.get("key-string");
            System.out.println(String.format("get指令执行结果:%s", value));
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            // 业务操作完成,将连接返回给连接池
            if (null != client) {
                pool.returnResource(client);
            }
        } // end of try block

        // 应用关闭时,释放连接池资源
        pool.destroy();
    }

}

运行上述代码,控制台输出:

set指令执行结果:OK
get指令执行结果:Hello, Redis!

使用连接池+分布式

在规模较大的系统中,往往会有多个Redis实例做负载均衡。并且还实现主从备份,当主实例发生故障时,切换至从实例提供服务。
类似于Memcached的客户端,Jedis也提供了客户端分布式操作的方式,采用一致性哈希算法。

public class TestJedis {

    public static void main(String[] args) {
        // 生成多机连接信息列表
        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
        shards.add( new JedisShardInfo("127.0.0.1", 6379) );
        shards.add( new JedisShardInfo("192.168.56.102", 6379) );

        // 生成连接池配置信息
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(10);
        config.setMaxTotal(30);
        config.setMaxWaitMillis(3*1000);

        // 在应用初始化的时候生成连接池
        ShardedJedisPool pool = new ShardedJedisPool(config, shards);

        // 在业务操作时,从连接池获取连接
        ShardedJedis client = pool.getResource();
        try {
            // 执行指令
            String result = client.set("key-string", "Hello, Redis!");
            System.out.println( String.format("set指令执行结果:%s", result) );
            String value = client.get("key-string");
            System.out.println( String.format("get指令执行结果:%s", value) );
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            // 业务操作完成,将连接返回给连接池
            if (null != client) {
                pool.returnResource(client);
            }
        } // end of try block

        // 应用关闭时,释放连接池资源
        pool.destroy();
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值