Redis使用方法总结

Jedis

介绍

项目地址: https://github.com/redis/jedis

Jedis is a blazingly small and sane Redis java client.

Jedis was conceived to be EASY to use.

Jedis is fully compatible with redis 2.8.x, 3.x.x and above*.

Jedis 是Redis的Java实现的客户端。支持基本的数据类型如:String、Hash、List、Set、Sorted Set。

特点:使用阻塞的I/O,方法调用同步,程序流需要等到socket处理完I/O才能执行,不支持异步操作。Jedis客户端实例不是线程安全的,需要通过连接池来使用Jedis。

Jedis使用

JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");

/// Jedis implements Closeable. Hence, the jedis instance will be auto-closed after the last statement.try (Jedis jedis = pool.getResource()) { 
 /// ... do stuff here ... for example
  jedis.set("foo", "bar");  String foobar = jedis.get("foo");
  jedis.zadd("sose", 0, "car"); jedis.zadd("sose", 0, "bike"); 
  Set<String> sose = jedis.zrange("sose", 0, -1);
}/// ... when closing your application:pool.close();



//Pipeline使用方式
Jedis jedis = new Jedis();
Pipeline pipeline = jedis.pipelined();
int count = 5000;
for (int i = 0; i < count; i++) {
    String key = "key-" + (i + 1);
    String value = "value-" + (i + 1);
    pipeline.set(key, value);
    pipeline.expire(key, 2);
}
long start = System.currentTimeMillis();
pipeline.syncAndReturnAll();
log.info("Jedis cost:{} ms", System.currentTimeMillis() - start);

更详细的用法可以参照: https://github.com/redis/jedis/wiki/Getting-started


Redissson

项目地址: https://github.com/redisson/redisson

优点点:分布式锁,分布式集合,可通过Redis支持延迟队列。

Redissson使用


<dependency>
   <groupId>org.redisson</groupId>
   <artifactId>redisson</artifactId>
   <version>3.16.2</version>
</dependency>  

// 1. Create config objectConfig config = new Config();
config.useClusterServers()       
// use "rediss://" for SSL connection
      .addNodeAddress("redis://127.0.0.1:7181");// or read config from fileconfig = Config.fromYAML(new File("config-file.yaml")); 
      
// 2. Create Redisson instance// Sync and Async APIRedissonClient redisson = Redisson.create(config);
// Reactive APIRedissonReactiveClient redissonReactive = redisson.reactive();
// RxJava3 APIRedissonRxClient redissonRx = redisson.rxJava();

// 3. Get Redis based implementation of java.util.concurrent.ConcurrentMap
RMap<MyKey, MyValue> map = redisson.getMap("myMap");
RMapReactive<MyKey, MyValue> mapReactive = redissonReactive.getMap("myMap");
RMapRx<MyKey, MyValue> mapRx = redissonRx.getMap("myMap");

// 4. Get Redis based implementation of java.util.concurrent.locks.Lock
RLock lock = redisson.getLock("myLock");
RLockReactive lockReactive = redissonReactive.getLock("myLock");
RLockRx lockRx = redissonRx.getLock("myLock");

Lettuce

https://lettuce.io/
用于线程安全同步,异步和响应使用,支持集群,Sentinel,管道和编码器。

基于Netty框架的事件驱动的通信层,其方法调用是异步的。Lettuce的API是线程安全的,所以可以操作单个Lettuce连接来完成各种操作。


lettuce使用  https://github.com/lettuce-io/lettuce-core/tree/6.1.4.RELEASE
<dependency> 
   <groupId>io.lettuce</groupId>  
   <artifactId>lettuce-core</artifactId>   
    <version>6.1.4.RELEASE</version>
</dependency>

// 使用
RedisClient redisClient = RedisClient.create("redis://password@localhost:6379/0");
StatefulRedisConnection<String, String> connection = redisClient.connect();
RedisCommands<String, String> syncCommands = connection.sync();
syncCommands.set("key", "Hello, Redis!");
connection.close();
redisClient.shutdown();

附录

https://www.cnblogs.com/throwable/p/11601538.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值