Redis 缓存池 Jedis实现

一、 引入 maven 依赖

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

二、 配置链接信息

#default value
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.min-idle=8
spring.redis.jedis.pool.enabled=true

三、 配置 Jedis 链接池客户端

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class JedisConfig {

    @Autowired
    private RedisProperties redisProperties;

    @Bean
    public JedisPool jedisPool() {
        RedisProperties.Jedis jedisConf = redisProperties.getJedis();   // properties 里 spring.redis 对应的配置文件
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();        // 创建链接池的配置文件
        jedisPoolConfig.setMaxIdle(jedisConf.getPool().getMaxIdle());
        jedisPoolConfig.setMaxTotal(jedisConf.getPool().getMaxIdle());
        jedisPoolConfig.setMinIdle(jedisConf.getPool().getMinIdle());
        return new JedisPool(jedisPoolConfig, redisProperties.getHost(), redisProperties.getPort()); //创建链接池
    }

}

四、 链接池预热,启动时对链接池座预热,执行一些简单的命令,提前初始化好链接, 使用Jedis 链接池内的客户端进行 存储 和 查询

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPooled;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Component
public class JedisPoolDemo {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 链接池预热
     */
    @PostConstruct
    public void preActivePool() {
        List<Jedis> minIdleJedisList = new ArrayList<>(jedisPool.getMinIdle());

        for (int i=0; i<jedisPool.getMinIdle(); i++) {
            Jedis jedis;
            try{
                jedis = jedisPool.getResource();
                minIdleJedisList.add(jedis);
                jedis.ping();
            }catch (Exception e) {
                e.printStackTrace();
            }finally {
                //不能立马返回链接池,否则最后只会创建一个链接
            }
        }

        for(int i=0; i<minIdleJedisList.size(); i++) {
            try {
                Jedis jedis = minIdleJedisList.get(i);
                jedis.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        System.out.println("链接池链接有" + minIdleJedisList.size() + "个 已经预热完毕");
    }

    public void get(String key) {
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            System.out.println(key + "的值为:" + jedis.get(key));

            jedis.set("hello", "world");
            System.out.println("hello 取到到值为: " + jedis.get("hello") );
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(jedis != null) {
                jedis.close();  //归还链接池
            }
        }
    }

    /**
     * 更简单的方式使用链接池
     */
    public void easyGet() {
        JedisPooled jedisPooled = new JedisPooled("127.0.0.1",6379);
        jedisPooled.set("easy", "this is very easy");
        System.out.println(jedisPooled.get("easy"));
    }

}

 

五、测试,调用 get 方法,返回结果如下:

 

六、 如果是集群模式, 则使用如下方式

Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379));
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7380));
JedisCluster jedis = new JedisCluster(jedisClusterNodes);
jedis.sadd("planets", "Mars");

 

posted @ 2022-05-05 17:37  长弓射大狗  阅读(369)  评论(0)    收藏  举报