redis java jedispool_java连接Redis单线程版本_使用JedisPool

本文介绍了一个基于Jedis的Redis缓存配置方案及单例客户端的具体实现细节,包括如何设置缓存过期时间等关键操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

-----------------------------------------------------------------1

类1:JedisClientSingle:

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

@Component

public class JedisClientSingle {

@Autowired

private JedisPool jedisPool;

//获取key的value值

public String get(String key) {

Jedis jedis = jedisPool.getResource();

String str = "";

try {

str = jedis.get(key);

} finally {

try {

jedis.close();

} catch (Exception e) {

e.printStackTrace();

}

}

return str;

}

public String set(String key, String value) {

Jedis jedis = jedisPool.getResource();

String str = "";

try {

str = jedis.set(key, value);

} finally {

try {

jedis.close();

} catch (Exception e) {

e.printStackTrace();

}

}

return str;

}

public String setex(String key, int seconds, String value) {

Jedis jedis = jedisPool.getResource();

String str = "";

try {

str = jedis.setex(key, seconds, value);

} finally {

try {

jedis.close();

} catch (Exception e) {

e.printStackTrace();

}

}

return str;

}

}

--------------------------------------------------------------2

类2:RedisCacheConfiguration:

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.cache.annotation.CachingConfigurerSupport;

import org.springframework.cache.annotation.EnableCaching;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

@Configuration

@EnableCaching

public class RedisCacheConfiguration extends CachingConfigurerSupport {

Logger logger = LoggerFactory.getLogger(RedisCacheConfiguration.class);

@Value("${redis.master.host}")

private String redisHost;

@Value("${redis.master.port}")

private String redisPort;

@Value("${redis.master.password}")

private String redisPassword;

private int timeout = 0;

private int maxIdle = 20;                    //最大等待连接中的数量

private long maxWaitMillis = 5000;            //最大建立连接等待时间

@Bean

public JedisPool redisPoolFactory() {

logger.info("JedisPool注入成功!!");

logger.info("redis地址:" + redisHost + ":" + redisPort);

JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

jedisPoolConfig.setMaxIdle(maxIdle);

jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);

JedisPool jedisPool = new JedisPool(jedisPoolConfig, redisHost, Integer.parseInt(redisPort), timeout, redisPassword);

return jedisPool;

}

}

-------------------------------------------------------------3

使用:

jedisClientSingle.setex(userInfo.getUserName(), 1800, subject.getSession().getId().toString());

完!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值