1 配置文件
jedis:
pool:
host: 127.0.0.1
port: 6379
password: 123456
timeout: 7200
ssl: false
config:
maxIdle: 100
maxTotal: 1000
minIdle: 50
maxWaitMillis: 1000
testOnBorrow: true
2 创建jedis连接池
package com.bigdata.admin.util.redis;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Component
@Slf4j
public class JedisService {
@Bean
public JedisPoolConfig jedisPoolConfig(@Value("${jedis.pool.config.maxTotal}") int maxActive,
@Value("${jedis.pool.config.maxIdle}") int maxIdle,
@Value("${jedis.pool.config.minIdle}") int minIdle,
@Value("${jedis.pool.config.maxWaitMillis}") long maxWaitMillis,
@Value("${jedis.pool.config.testOnBorrow}") boolean testOnBorrow) {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(maxActive);
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMinIdle(minIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
jedisPoolConfig.setTestOnBorrow(testOnBorrow);
return jedisPoolConfig;
}
@Bean
public JedisPool jedisPool(@Value("${jedis.pool.host}") String host,
@Value("${jedis.pool.password}") String password,
@Value("${jedis.pool.port}") int port,
@Value("${jedis.pool.timeout}") int timeout, JedisPoolConfig jedisPoolConfig) {
log.info("=====创建JedisPool连接池=====");
if (StringUtils.isNotEmpty(password)) {
return new JedisPool(jedisPoolConfig, host, port, timeout, password);
}
return new JedisPool(jedisPoolConfig, host, port, timeout);
}
}
3 创建工具类
package com.bigdata.admin.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Component
@Slf4j
public class RedisUtil {
@Autowired
private JedisPool jedisPool;
public String get(String key) {
Jedis jedis = null;
String value = null;
try {
jedis = jedisPool.getResource();
value = jedis.get(key);
} catch (Exception e) {
log.error("redis get", e);
} finally {
returnResource(jedis);
}
return value;
}
public String set(String key, String value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.set(key, value);
} catch (Exception e) {
log.error("redis set", e);
return null;
} finally {
returnResource(jedis);
}
}
public Long del(String... keys) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.del(keys);
} catch (Exception e) {
log.error("redis del", e);
return 0L;
} finally {
returnResource(jedis);
}
}
public Long append(String key, String str) {
Jedis jedis = null;
Long res = null;
try {
jedis = jedisPool.getResource();
res = jedis.append(key, str);
} catch (Exception e) {
log.error("redis append", e);
return 0L;
} finally {
returnResource(jedis);
}
return res;
}
public Boolean exists(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.exists(key);
} catch (Exception e) {
log.error("redis exists", e);
return false;
} finally {
returnResource(jedis);
}
}
public String flushDB() {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.flushDB();
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedis);
}
return null;
}
public Long expire(String key, int value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.expire(key, value);
} catch (Exception e) {
log.error(e.getMessage());
return 0L;
} finally {
returnResource(jedis);
}
}
public Long ttl(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.ttl(key);
} catch (Exception e) {
log.error(e.getMessage());
return 0L;
} finally {
returnResource(jedis);
}
}
public String setex(String key, int seconds, String value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.setex(key, seconds, value);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedis);
}
return null;
}
public Long setnx(String key, String value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.setnx(key, value);
} catch (Exception e) {
log.error(e.getMessage());
return 0L;
} finally {
returnResource(jedis);
}
}
public String getSet(String key, String value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.getSet(key, value);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedis);
}
return null;
}
public String setex(String key, String value, int seconds) {
Jedis jedis = null;
String res = null;
try {
jedis = jedisPool.getResource();
res = jedis.setex(key, seconds, value);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
returnResource(jedis);
}
return res;
}
public Long setrange(String key, String str, int offset) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.setrange(key, offset, str);
} catch (Exception e) {
log.error(e.getMessage());
return 0L;
} finally {
returnResource(jedis);
}
}
public List<String> mget(