SpringBoot使用Jedis访问Redis

1、pom文件引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>

2、RedisConnect类

/**
 * @author Han, WeChat:autumn6980
 * @date 2021/4/10
 */
public class RedisConnect {
    // Redis服务器节点信息
    // ip替换成你的真实ip或者域名即可
    private static final String[] nodeConfigs = {"ip1:6379", "ip2:6379"};
    // Redis访问用户名
    private static final String username = "";
    // Redis访问密码
    private static final String password = "";
    private static final JedisCluster jedisCluster;

    static {
        Set<HostAndPort> nodes = new HashSet<>();
        for (String node : nodeConfigs) {
            String[] parts = StringUtils.split(node, ":");
            Assert.state(parts.length == 2, "redis node should be defined as 'host:port', not '" + node + "'");
            nodes.add(new HostAndPort(parts[0], Integer.parseInt(parts[1])));
        }
        // 没有配置连接池使用默认的连接池
        // return new JedisCluster(nodes);

        // 创建集群对象
        if (StringUtils.isNotEmpty(password)) {
            jedisCluster = new JedisCluster(nodes, 6000, 1500, 3, username, password, "default", jedisPoolConfig());
        } else {
            jedisCluster = new JedisCluster(nodes, 6000, jedisPoolConfig());
        }
    }

    public static JedisCluster getConnection() {
        return jedisCluster;
    }

    public static JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMinIdle(5);
        jedisPoolConfig.setMaxIdle(20);
        jedisPoolConfig.setMaxWaitMillis(24000);
        return jedisPoolConfig;
    }
}

3、RedisClientFactory类

/**
 * @author Han, WeChat:autumn6980
 * @date 2021/4/10
 */
public class RedisClientFactory {

    public static JedisCluster defaultClient() {
        return RedisConnect.getConnection();
    }
}

4、RedisUtil类

这里只给了get和set的示例

/**
 * @author Han, WeChat:autumn6980
 * @date 2021/4/10
 */
public class RedisUtil {
    private static final String OK = "OK";

    private static JedisCluster redis() {
        return RedisClientFactory.defaultClient();
    }

    public static boolean set(String key, String value) {
        String result = redis().set(key, value);
        return OK.equals(result);
    }

    public static boolean set(String key, String value, int secondsToExpire) {
        SetParams setParams = new SetParams();
        setParams.ex(secondsToExpire);
        String result = redis().set(key, value, setParams);
        return OK.equals(result);
    }

    public static boolean set(String key, Object value) {
        String json = JSON.toJSONString(value);
        return set(key, json);
    }

    public static boolean set(String key, Object value, int secondsToExpire) {
        String json = JSON.toJSONString(value);
        return set(key, json, secondsToExpire);
    }

    public static <T> T get(String key, TypeReference<T> type) {
        String result = redis().get(key);
        return JSON.parseObject(result, type);
    }

    public static String get(String key) {
        return redis().get(key);
    }

    public static void main(String[] args) {
        set("currentDatetime", new Date(), 60);
        Date currentDatetime = get("currentDatetime", new TypeReference<Date>() {
        });
        System.out.println(currentDatetime);
    }
}

http://www.hanyehong.com/2021/04/11/SpringBoot%E4%BD%BF%E7%94%A8Jedis%E8%AE%BF%E9%97%AERedis/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值