Java操作Redis工具类
Redis 安装步骤可参考:
https://www.cnblogs.com/jonban/p/9217221.html
依赖 jar 包
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.0.1</version> </dependency>
RedisUtils.java
package javax.utils;import java.util.ArrayList;
import java.util.List;import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;/**
Redis 工具类
@author Logan
@version 1.0.0
*/
public class RedisUtils {private static ShardedJedisPool jedisPool = null;
static {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置最大对象数</span> jedisPoolConfig.setMaxTotal(20<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 最大能够保持空闲状态的对象数</span> jedisPoolConfig.setMaxIdle(10<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 超时时间</span> jedisPoolConfig.setMaxWaitMillis(10000<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 在获取连接的时候检查有效性, 默认false</span> jedisPoolConfig.setTestOnBorrow(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 在返回Object时, 对返回的connection进行validateObject校验</span> jedisPoolConfig.setTestOnReturn(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果是集群,可以全部加入list中</span> List<JedisShardInfo> shardInfos = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<JedisShardInfo><span style="color: rgba(0, 0, 0, 1)">(); JedisShardInfo shardInfo </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JedisShardInfo("192.168.32.10", 6379<span style="color: rgba(0, 0, 0, 1)">); shardInfo.setPassword(</span>"redis123."<span style="color: rgba(0, 0, 0, 1)">); shardInfos.add(shardInfo); jedisPool </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ShardedJedisPool(jedisPoolConfig, shardInfos);
}
/**
- 从连接池中获取一个 ShardedJedis 对象
*/
public static ShardedJedis getJedis() {
return jedisPool.getResource();
}/**
- 关闭 ShardedJedis 对象,放回池中
*/
public static void closeJedis(ShardedJedis jedis) {
jedis.close();
}/**
通过 key 获取 String 类型 Value
@param key 键
@return 值
*/
public static String get(String key) {
try (
ShardedJedis jedis = jedisPool.getResource();
) {</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> jedis.get(key);
}
}/**
通过 key 获取字节数组类型 Value
@param key 键
@return 值
*/
public static byte[] get(byte[] key) {
try (
ShardedJedis jedis = jedisPool.getResource();
) {</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> jedis.get(key);
}
}/**
设置 String 类型 key 和 value
@param key 键
@param value 值
@return
*/
public static String set(String key, String value) {
try (
ShardedJedis jedis = jedisPool.getResource();
) {</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> jedis.set(key, value);
}
}
/**
设置字节数组类型 key 和 value
@param key 键
@param value 值
@return
*/
public static String set(byte[] key, byte[] value) {
try (
ShardedJedis jedis = jedisPool.getResource();
) {</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> jedis.set(key, value);
}
}
/**
- 删除指定 key
*/
public static Long del(String key) {
try (
ShardedJedis jedis = jedisPool.getResource();
) {
return jedis.del(key);
}
}/**
- 左侧放入集合
- @param key 键
- @param values 值集合
- @return
*/
public static Long lpush(String key, String... values) {
try (
ShardedJedis jedis = jedisPool.getResource();
) {
return jedis.lpush(key, values);
}
}/**
- 左侧弹出一个元素
- @param key 指定键
- @return 左侧第一个元素
*/
public static String lpop(String key) {
try (
ShardedJedis jedis = jedisPool.getResource();
) {
return jedis.lpop(key);
}
}/**
- 右侧放入集合
- @param key 键
- @param values 值集合
- @return
*/
public static Long rpush(String key, String... values) {
try (
ShardedJedis jedis = jedisPool.getResource();
) {
return jedis.rpush(key, values);
}
}/**
- 右侧弹出一个元素
- @param key 指定键
- @return 右侧第一个元素
*/
public static String rpop(String key) {
try (
ShardedJedis jedis = jedisPool.getResource();
) {
return jedis.rpop(key);
}
}}
Java 操作 Redis 工具类.