不足的地方请留言指出, 非常感谢!!!
- RedisClient 使用方式和 jedis 使用一样.
- 区别在于:
- jedis 使用完要 “jedis.close();”
- 而RedisClient不需要, 只管使用, 它自动释放连接
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*特别说明: 本 REDIS 工具类使用的是 JedisPool 连接池, 除了getRedis()方法之外的所有方法都实现了自动释放资源(还连接)
* 但不支持换库操作, 如果使用此工具类, 只能用同一个库.
*使用方法: @Autowired
* RedisClient redisClient
*
* 如果需要: (切换库, 使用管道, 使用事物)等操作 请使用本工具类的 getRedis(), 但使用完后
* 一定要手动还连接.
*/
@Component
public class RedisClient {
@Autowired
private JedisPool jedisPool;
public RedisClient() {
}
public Jedis getRedis() {
return jedisPool.getResource();
}
public String get(String key) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String set(String key, String value) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.set(key, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String set(String key, String value, String nxxx) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.set(key, value, nxxx);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String set(String key, String value, String nxxx, String expx, int time) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.set(key, value, nxxx, expx, time);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String set(String key, String value, String nxxx, String expx, long time) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.set(key, value, nxxx, expx, time);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Boolean exists(String key) {
Jedis jedis = null;
Boolean result = null;
try {
jedis = jedisPool.getResource();
result = jedis.exists(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long exists(String... key) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.exists(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long del(String key) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.del(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long del(final String... keys) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.del(keys);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String type(String key) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.type(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Set<String> keys(String pattern) {
Jedis jedis = null;
Set<String> result = null;
try {
jedis = jedisPool.getResource();
result = jedis.keys(pattern);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String randomkey() {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.randomKey();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String rename(String oldkey, String newkey) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.rename(oldkey, newkey);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long dbSize() {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.dbSize();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long expire(String key, int seconds) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.expire(key, seconds);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
// public String select(int index) {
// Jedis jedis = null;
// String result = null;
// try {
// jedis = jedisPool.getResource();
// result = jedis.select(index);
// System.out.println("jedis = " + jedis + ", result = " + result);
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// if (null != jedis) {
// jedis.close();
// }
// }
// return result;
// }
public Long move(String key, int dbIndex) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.move(key, dbIndex);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String getSet(String key, String value) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.getSet(key, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public List<String> mget(String... keys) {
Jedis jedis = null;
List<String> result = null;
try {
jedis = jedisPool.getResource();
result = jedis.mget(keys);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long setnx(String key, String value) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.setnx(key, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String setex(String key, int seconds, String value) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.setex(key, seconds, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String mset(String... keysvalues) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.mset(keysvalues);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long msetnx(String... keysvalues) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.msetnx(keysvalues);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long incr(String key) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.incr(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long incrBy(String key, long integer) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.incrBy(key, integer);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long decr(String key) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.decr(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long decrBy(String key, long integer) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.decrBy(key, integer);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long append(String key, String value) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.append(key, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String substr(String key, int start, int end) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.substr(key, start, end);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long rpush(String key, String... string) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.rpush(key, string);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long lpush(String key, String... string) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.lpush(key, string);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long llen(String key) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.llen(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public List<String> lrange(String key, long start, long end) {
Jedis jedis = null;
List<String> result = null;
try {
jedis = jedisPool.getResource();
result = jedis.lrange(key, start, end);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String ltrim(final String key, final long start, final long end) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.ltrim(key, start, end);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String lindex(String key, long index) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.lindex(key, index);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String lset(String key, long index, String value) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.lset(key, index, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long lrem(final String key, final long count, final String value) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.lrem(key, count, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String lpop(String key) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.lpop(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String rpop(String key) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.rpop(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String rpoplpush(String srckey, String dstkey) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.rpoplpush(srckey, dstkey);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long sadd(final String key, final String... members) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.sadd(key, members);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Set<String> smembers(String key) {
Jedis jedis = null;
Set<String> result = null;
try {
jedis = jedisPool.getResource();
result = jedis.smembers(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long smove(String srckey, String dstkey, String member) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.smove(srckey, dstkey, member);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long scard(String key) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.scard(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Boolean sismember(String key, String member) {
Jedis jedis = null;
Boolean result = null;
try {
jedis = jedisPool.getResource();
result = jedis.sismember(key, member);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Set<String> sinter(final String... keys) {
Jedis jedis = null;
Set<String> result = null;
try {
jedis = jedisPool.getResource();
result = jedis.sinter(keys);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long sinterstore(String dstkey, String... keys) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.sinterstore(dstkey, keys);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Set<String> sunion(String... keys) {
Jedis jedis = null;
Set<String> result = null;
try {
jedis = jedisPool.getResource();
result = jedis.sunion(keys);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long sunionstore(String dstkey, String... keys) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.sunionstore(dstkey, keys);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Set<String> sdiff(String... keys) {
Jedis jedis = null;
Set<String> result = null;
try {
jedis = jedisPool.getResource();
result = jedis.sdiff(keys);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long sdiffstore(String dstkey, String... keys) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.sdiffstore(dstkey, keys);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String srandmember(final String key) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.srandmember(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long hset(String key, String field, String value) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.hset(key, field, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public List<String> hmget(String key, String... fields) {
Jedis jedis = null;
List<String> result = null;
try {
jedis = jedisPool.getResource();
result = jedis.hmget(key, fields);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String hmset(String key, Map<String, String> hash) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.hmset(key, hash);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long hincrBy(String key, String field, long value) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.hincrBy(key, field, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Boolean hexists(String key, String field) {
Jedis jedis = null;
Boolean result = null;
try {
jedis = jedisPool.getResource();
result = jedis.hexists(key, field);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long hdel(final String key, final String... fields) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.hdel(key, fields);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long hlen(String key) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.hlen(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Set<String> hkeys(final String key) {
Jedis jedis = null;
Set<String> result = null;
try {
jedis = jedisPool.getResource();
result = jedis.hkeys(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public List<String> hvals(String key) {
Jedis jedis = null;
List<String> result = null;
try {
jedis = jedisPool.getResource();
result = jedis.hvals(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String hget(String key, String field) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.hget(key, field);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Map<String, String> hgetAll(String key) {
Jedis jedis = null;
Map<String, String> result = null;
try {
jedis = jedisPool.getResource();
result = jedis.hgetAll(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long getDB() {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.getDB();
System.out.println("jedis = " + jedis + ", result = " + result);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Boolean getbit(String key, long offset) {
Jedis jedis = null;
Boolean result = null;
try {
jedis = jedisPool.getResource();
result = jedis.getbit(key, offset);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String getrange(String key, long startOffset, long endOffset) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.getrange(key, startOffset, endOffset);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long hsetnx(String key, String field, String value) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.hsetnx(key, field, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long setrange(String key, long offset, String value) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.setrange(key, offset, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Boolean setbit(String key, long offset, String value) {
Jedis jedis = null;
Boolean result = null;
try {
jedis = jedisPool.getResource();
result = jedis.setbit(key, offset, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String psetex(final String key, final long milliseconds, final String value) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.psetex(key, milliseconds, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String asking() {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.asking();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public byte[] dump(final String key) {
Jedis jedis = null;
byte[] result = null;
try {
jedis = jedisPool.getResource();
result = jedis.dump(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public String echo(final String string) {
Jedis jedis = null;
String result = null;
try {
jedis = jedisPool.getResource();
result = jedis.echo(string);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
public Long geoadd(String key, double longitude, double latitude, String member) {
Jedis jedis = null;
Long result = null;
try {
jedis = jedisPool.getResource();
result = jedis.geoadd(key, longitude, latitude, member);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
jedis.close();
}
}
return result;
}
}
下面是Jedis连接池Bean
import org.springframework.beans.factory.annotation.Value;
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 RedisConfig {
@Bean
public JedisPoolConfig jedisPoolConfig(@Value("${spring.redis.jedis.pool.maxActive}") int maxTotal,
@Value("${spring.redis.jedis.pool.maxIdle}") int maxIdle,
@Value("${spring.redis.jedis.pool.maxWait}") int maxWaitMillis) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWaitMillis);
config.setTestOnBorrow(true);
return config;
}
@Bean
public JedisPool jedisPool(JedisPoolConfig config,
@Value("${spring.redis.host}") String host,
@Value("${spring.redis.port}") int port,
@Value("${spring.redis.timeout}") int timeOut,
@Value("${spring.redis.password}") String password,
@Value("${spring.redis.database}") int database) {
return new JedisPool(config, host, port, timeOut, password, database);
}
}