jedis 和 redisson 有哪些区别?

  Jedis 和 Redisson 都是Java中对Redis操作的封装。Jedis 只是简单的封装了 Redis 的API库,可以看作是Redis客户端,它的方法和Redis 的命令很类似。Redisson 不仅封装了 redis ,还封装了对更多数据结构的支持,以及锁等功能,相比于Jedis 更加大。但Jedis相比于Redisson 更原生一些,更灵活。

Redis官方对Java 语言的封装框架推荐的有十多种(Redis 官网),主要是Jedis 、Redisson。

Jedis

Jedis 是Java 实现的Redis 客户端,它的API提供了全面的类似于Redis 原生命令的支持。相比于其他Redis 封装框架更加原生。

它的使用主要是使用JedisPool

初始化:

// 创建JedisPool所需的连接池配置
JedisPoolConfig poolConfig = new JedisPoolConfig();
 
// 最大连接数,默认8
poolConfig.setMaxTotal(1024);
 
// 最大空闲数,默认8
poolConfig.setMaxIdle(100);
 
// poolConfig 各种配置
 
/// 是否启用pool的jmx管理功能, 默认true
poolConfig.setJmxEnabled(true);
 
// 创建JedisPool连接池
jedisPool = new JedisPool(poolConfig, HOST, PORT, TIMEOUT, PASSWORD);

简单使用的demo:

/**
     * 同步获取Jedis
     * @return
     */
    public synchronized static Jedis getJedis(){
 
        if(jedisPool != null){
            //获取Jedis对象
            Jedis jedis = jedisPool.getResource();
            return jedis;
        }
        return null;
    }
    
    /**
     * 释放jedis资源
     */
    public static void releaseResource(Jedis jedis){
        if( jedis !=null ){
            jedis.close();
        }
    }

Redisson

Redisson是一个在Redis的基础上实现的Java驻内存数据网格(In-Memory Data Grid)。它不仅提供了一系列的分布式的Java常用对象,还提供了许多分布式服务。其中包括Bitset, Set, MultiMap, SortedSet, Map, List, Queue, BlockingQueue, Deque, BlockingDeque, Semaphore, Lock, AtomicLong, CountDownLatch, Publish/Subscribe, Bloom filter, Remote service, Spring cache, Executor service, Live Object service, Scheduler service。Redisson提供了使用Redis的最简单和最便捷的方法。Redisson的宗旨是促进使用者对Redis的关注分离(Separation of Concern),从而让使用者能够将精力更集中地放在处理业务逻辑上。(参考)

简单使用的代码demo:

//创建配置  
Config config = new Config();  
  
//指定编码,默认编码为org.redisson.codec.JsonJacksonCodec   
config.setCodec(new org.redisson.client.codec.StringCodec());  
  
//指定使用单节点部署方式  
config.useSingleServer().setAddress("redis://127.0.0.1:6379");  
  
config.useSingleServer().setClientName("root");
config.useSingleServer().setPassword("abcabc");
 
//创建redisson客户端
RedissonClient redisson = Redisson.create(config);  
 
RBucket<String> keyObject = redisson.getBucket("key");  
keyObject.set("value");  
 
//关闭RedissonClient  
redisson.shutdown();  

猜你喜欢

转载自www.cnblogs.com/jxxblogs/p/12241080.html
今日推荐