今天一个同事搞了大半天都没弄出来,然后老大叫我接手。记录分享出来主要是让新同学少走弯路
目前采用Jedis实现,后续推出Spring data redis版本
首先订阅是阻塞的,所以单独开一个线程订阅通道,应用启动Runner
Runner执行的时间是spring容器启动完成之后,就会紧接着执行这个接口实现类的run方法
package com.shinedata.config.redis.pubsub;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
* @ClassName RedisPublisherRunner 应用启用发起订阅
* @Author yupanpan
*/
@Component
public class RedisSubscriberRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
//topic发布订阅
new Thread(new RedisSubscriberRunnable()).start();
//key过期订阅
new Thread(new RedisKeyExpireSubscriberRunnable()).start();
//订阅报告同步事件
new Thread(new EvaluationReportSubscriberRunnable()).start();
}
}
这里以订阅报告同步为例,以下为一个Runnable
//测评报告发布订阅通道 public static final String EVALUATION_REPORT="evaluationReportChannel";
package com.shinedata.config.redis.pubsub;
import com.shinedata.config.redis.JedisUtils;
import com.shinedata.config.redis.RedisKeyExpireEventConstants;
import com.shinedata.config.redis.listener.EvaluationReportSubscriberListener;
import redis.clients.jedis.Jedis;
/**
* @ClassName EvaluationReportSubscriberRunnable
* @Author yupanpan
*/
public class EvaluationReportSubscriberRunnable implements Runnable{
@Override
public void run() {
Jedis jedis = JedisUtils.getJedis();
EvaluationReportSubscriberListener listener=new EvaluationReportSubscriberListener();
jedis.subscribe(listener, RedisKeyExpireEventConstants.EVALUATION_REPORT);
JedisUtils.returnResource(jedis);
}
}
监听器
主要作用就是接受订阅后redis推送过来的消息
package com.shinedata.config.redis.listener;
import com.shinedata.config.redis.pojo.ChannelMessage;
import com.shinedata.config.redis.thread.TopicMessageThreadLocal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.JedisPubSub;
/**
* @ClassName EvaluationReportSubscriberListener
* @Author yupanpan
*/
public class EvaluationReportSubscriberListener extends JedisPubSub {
private static Logger logger = LoggerFactory.getLogger(EvaluationReportSubscriberListener.class);
/**
* @Title: onMessage
* @Description: 取得订阅的消息后的处理
* @param channel 频道
* @param message 消息内容
* @author
* @date
*/
@Override
public void onMessage(String channel, String message) {
TopicMessageThreadLocal instance = TopicMessageThreadLocal.getInstance();
instance.set(new ChannelMessage(channel,message));
logger.info("Redis key监听事件通知Thread ID[{}] {}",Thread.currentThread().getId(),instance.get().toString());
}
instance.remove();
}
TopicMessageThreadLocal为自定义的一个ThreadLocal和ChannelMessage实体类,在使用完成之后记住清理ThreadLocal的内存
package com.shinedata.config.redis.thread;
import com.shinedata.config.redis.pojo.ChannelMessage;
/**
* @ClassName TopicThreadLocal
* @Author yupanpan
*/
public class TopicMessageThreadLocal {
private static InheritableThreadLocal channelMessage = new InheritableThreadLocal<>();
private static TopicMessageThreadLocal topicMessageThreadLocal = null;
private TopicMessageThreadLocal() {
}
public ChannelMessage get() {
return channelMessage.get();
}
public void set(ChannelMessage message) {
channelMessage.set(message);
}
public void remove() {
channelMessage.remove();
}
public InheritableThreadLocal getThreadLocal() {
return channelMessage;
}
public static TopicMessageThreadLocal getInstance(){
synchronized (TopicMessageThreadLocal.class){
if(topicMessageThreadLocal==null){
topicMessageThreadLocal=new TopicMessageThreadLocal();
}
return topicMessageThreadLocal;
}
}
}
package com.shinedata.config.redis.pojo;
import lombok.Data;
import lombok.ToString;
/**
* @ClassName ChannelMessage
* @Author yupanpan
*/
@Data
@ToString
public class ChannelMessage {
private String channel;
private String message;
public ChannelMessage() {
}
public ChannelMessage(String channel, String message) {
this.channel = channel;
this.message = message;
}
}
发布消息
package com.shinedata.config.redis.pubsub;
import com.shinedata.config.redis.JedisUtils;
import com.shinedata.config.redis.RedisKeyExpireEventConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
/**
* @ClassName RedisPublisher 发布
* @Author yupanpan
*/
public class RedisPublisher {
private static Logger logger = LoggerFactory.getLogger(RedisPublisher.class);
public static void evaluationReportPublish(String message) {
Jedis jedis= JedisUtils.getJedis();
Long subNum = jedis.publish(RedisKeyExpireEventConstants.EVALUATION_REPORT, message);
logger.info("订阅者数量[{}]",subNum);
//释放连接
JedisUtils.returnResource(jedis);
}
}
启动应用postman测试
@GetMapping(value = "/publish")
public void publishTest() {
RedisPublisher.evaluationReportPublish("6666666666666666666666");
}
最后附上Jedis配置类和工具类
package com.shinedata.config.redis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* @ClassName JedisConfig
* @Author yupanpan
*/
@Component
public class JedisConfig extends RedisProperties {
private static final Logger logger = LoggerFactory.getLogger(JedisConfig.class);
private static JedisPool jedisPool = null;
private static void initialPool() {
try {
JedisPoolConfig config = new JedisPoolConfig();
//最大空闲连接数, 应用自己评估,不要超过ApsaraDB for Redis每个实例最大的连接数
config.setMaxIdle(500);
config.setMinIdle(100);
//最大连接数, 应用自己评估,不要超过ApsaraDB for Redis每个实例最大的连接数
config.setMaxTotal(6000);
config.setMaxWaitMillis(5000);
//每timeBetweenEvictionRunsMillis毫秒秒检查一次连接池中空闲的连接,把空闲时间超过minEvictableIdleTimeMillis毫秒的连接断开,直到连接池中的连接数到minIdle为止
config.setTimeBetweenEvictionRunsMillis(600);
config.setMinEvictableIdleTimeMillis(100);
config.setTestOnBorrow(true);
config.setTestOnReturn(false);
jedisPool = new JedisPool(config, redisHost, redisPort, 5000, redisPassword);
} catch (Exception e) {
if (jedisPool != null) {
jedisPool.close();
}
logger.error("初始化Redis连接池失败", e);
}
}
/**
* 初始化Redis连接池
*/
static {
initialPool();
}
/**
* 在多线程环境同步初始化
*/
private static synchronized void poolInit() {
if (jedisPool == null) {
initialPool();
}
}
/**
* 同步获取Jedis实例
*
* @return Jedis
*/
public Jedis getJedis() {
if (jedisPool == null) {
poolInit();
}
Jedis jedis = null;
try {
if (jedisPool != null) {
jedis = jedisPool.getResource();
}
} catch (Exception e) {
logger.error("同步获取Jedis实例失败" + e.getMessage(), e);
returnResource(jedis);
}
return jedis;
}
/**
* 释放jedis资源
*
* @param jedis
*/
@SuppressWarnings("deprecation")
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
}
package com.shinedata.config.redis;
import org.checkerframework.checker.units.qual.min;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.BinaryClient;
import redis.clients.jedis.BitOP;
import redis.clients.jedis.BitPosParams;
import redis.clients.jedis.Jedis;
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @ClassName JedisUtils
* @Author yupanpan
*/
@Component
public class JedisUtils {
private static final Logger logger = LoggerFactory.getLogger(JedisUtils.class);
@Autowired
private JedisConfig jedisConfig;
private static JedisUtils jedisUtils;
@PostConstruct
public void init() {
jedisUtils = this;
jedisUtils.jedisConfig = this.jedisConfig;
}
public static Jedis getJedis() {
return jedisUtils.jedisConfig.getJedis();
}
/**
* 获取指定key的值,如果key不存在返回null,如果该Key存储的不是字符串,会抛出一个错误
*
* @param key
* @return
*/
public static String get(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.get(key);
} finally {
returnResource(jedis);
}
}
public static byte[] get(byte[] key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.get(key);
} finally {
returnResource(jedis);
}
}
/**
* 设置key的值为value
*
* @param key
* @param value
* @return
*/
public static String set(String key, String value) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.set(key, value);
} finally {
returnResource(jedis);
}
}
public static String set(byte[] key, byte[] value) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.set(key, value);
} finally {
returnResource(jedis);
}
}
/**
* 删除指定的key,也可以传入一个包含key的数组
*
* @param keys
* @return
*/
public static Long del(String... keys) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.del(keys);
} finally {
returnResource(jedis);
}
}
/**
* 通过key向指定的value值追加值
*
* @param key
* @param str
* @return
*/
public static Long append(String key, String str) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.append(key, str);
} finally {
returnResource(jedis);
}
}
/**
* 判断key是否存在
*
* @param key
* @return
*/
public static Boolean exists(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
Boolean exists;
try {
exists = jedis.exists(key);
} finally {
jedis.close();
}
return exists;
}
/**
* 设置key value,如果key已经存在则返回0
*
* @param key
* @param value
* @return
*/
public static Long setnx(String key, String value) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.setnx(key, value);
} finally {
returnResource(jedis);
}
}
/**
* 设置key value并指定这个键值的有效期-秒
*
* @param key
* @param seconds
* @param value
* @return
*/
public static String setex(String key, String value, int seconds) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
String setex;
try {
setex = jedis.setex(key, seconds, value);
} finally {
jedis.close();
}
return setex;
}
public static String setex(byte[] key, byte[] value, int seconds) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
String setex;
try {
setex = jedis.setex(key, seconds, value);
} finally {
jedis.close();
}
return setex;
}
/**
* 通过key 和offset 从指定的位置开始将原先value替换
*
* @param key
* @param offset
* @param str
* @return
*/
public static Long setrange(String key, int offset, String str) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
Long setrange;
try {
setrange = jedis.setrange(key, offset, str);
} finally {
jedis.close();
}
return setrange;
}
/**
* 通过批量的key获取批量的value
*
* @param keys
* @return
*/
public static List mget(String... keys) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.mget(keys);
} finally {
returnResource(jedis);
}
}
/**
* 批量的设置key:value,也可以一个
*
* @param keysValues
* @return
*/
public static String mset(String... keysValues) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.mset(keysValues);
} finally {
returnResource(jedis);
}
}
/**
* 批量的设置key:value,可以一个,如果key已经存在则会失败,操作会回滚
*
* @param keysValues
* @return
*/
public static Long msetnx(String... keysValues) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.msetnx(keysValues);
} finally {
returnResource(jedis);
}
}
/**
* 设置key的值,并返回一个旧值
*
* @param key
* @param value
* @return
*/
public static String getSet(String key, String value) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.getSet(key, value);
} finally {
returnResource(jedis);
}
}
/**
* 通过下标 和key 获取指定下标位置的 value
*
* @param key
* @param startOffset
* @param endOffset
* @return
*/
public static String getrange(String key, int startOffset, int endOffset) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.getrange(key, startOffset, endOffset);
} finally {
returnResource(jedis);
}
}
/**
* 通过key 对value进行加值+1操作,当value不是int类型时会返回错误,当key不存在是则value为1
*
* @param key
* @return
*/
public static Long incr(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.incr(key);
} finally {
returnResource(jedis);
}
}
/**
* 通过key给指定的value加值,如果key不存在,则这是value为该值
*
* @param key
* @param integer
* @return
*/
public static Long incrBy(String key, long integer) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.incrBy(key, integer);
} finally {
returnResource(jedis);
}
}
/**
* 对key的值做减减操作,如果key不存在,则设置key为-1
*
* @param key
* @return
*/
public static Long decr(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.decr(key);
} finally {
returnResource(jedis);
}
}
/**
* 减去指定的值
*
* @param key
* @param integer
* @return
*/
public static Long decrBy(String key, long integer) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.decrBy(key, integer);
} finally {
returnResource(jedis);
}
}
/**
* 通过key获取value值的长度
*
* @param key
* @return
*/
public static Long strLen(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.strlen(key);
} finally {
returnResource(jedis);
}
}
/**
* 通过key给field设置指定的值,如果key不存在则先创建,如果field已经存在,返回0
*
* @param key
* @param field
* @param value
* @return
*/
public static Long hsetnx(String key, String field, String value) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.hsetnx(key, field, value);
} finally {
returnResource(jedis);
}
}
/**
* 通过key给field设置指定的值,如果key不存在,则先创建
*
* @param key
* @param field
* @param value
* @return
*/
public static Long hset(String key, String field, String value) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.hset(key, field, value);
} finally {
returnResource(jedis);
}
}
/**
* 通过key同时设置 hash的多个field
*
* @param key
* @param hash
* @return
*/
public static String hmset(String key, Map hash) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.hmset(key, hash);
} finally {
returnResource(jedis);
}
}
/**
* 通过key 和 field 获取指定的 value
*
* @param key
* @param failed
* @return
*/
public static String hget(String key, String failed) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.hget(key, failed);
} finally {
returnResource(jedis);
}
}
/**
* 设置key的超时时间为seconds
*
* @param key
* @param seconds
* @return
*/
public static Long expire(String key, int seconds) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.expire(key, seconds);
} finally {
returnResource(jedis);
}
}
/**
* 通过key 和 fields 获取指定的value 如果没有对应的value则返回null
*
* @param key
* @param fields 可以是 一个String 也可以是 String数组
* @return
*/
public static List hmget(String key, String... fields) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.hmget(key, fields);
} finally {
returnResource(jedis);
}
}
/**
* 通过key给指定的field的value加上给定的值
*
* @param key
* @param field
* @param value
* @return
*/
public static Long hincrby(String key, String field, Long value) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.hincrBy(key, field,
value);
} finally {
returnResource(jedis);
}
}
/**
* 通过key和field判断是否有指定的value存在
*
* @param key
* @param field
* @return
*/
public static Boolean hexists(String key, String field) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.hexists(key,
field);
} finally {
returnResource(jedis);
}
}
/**
* 通过key返回field的数量
*
* @param key
* @return
*/
public static Long hlen(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.hlen(key);
} finally {
returnResource(jedis);
}
}
/**
* 通过key 删除指定的 field
*
* @param key
* @param fields 可以是 一个 field
* 也可以是 一个数组
* @return
*/
public static Long hdel(String key, String... fields) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.hdel(key
, fields);
} finally {
returnResource(jedis);
}
}
/**
* 通过key返回所有的field
*
* @param key
* @return
*/
public static Set hkeys(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.hkeys(key);
} finally {
returnResource(jedis);
}
}
/**
* 通过key返回所有和key有关的value
*
* @param key
* @return
*/
public static List hvals(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.hvals(key);
} finally {
returnResource(jedis);
}
}
/**
* 通过key获取所有的field和value
*
* @param key
* @return
*/
public static Map hgetall(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.hgetAll(key);
} finally {
returnResource(jedis);
}
}
/**
* 通过key向list头部添加字符串
*
* @param key
* @param strs 可以是一个string 也可以是string数组
* @return 返回list的value个数
*/
public static Long lpush(String key, String... strs) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.lpush(key, strs);
} finally {
returnResource(jedis);
}
}
/**
* 通过key向list尾部添加字符串
*
* @param key
* @param strs 可以是一个string 也可以是string数组
* @return 返回list的value个数
*/
public static Long rpush(String key, String... strs) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.rpush(key, strs);
} finally {
returnResource(jedis);
}
}
/**
* 通过key在list指定的位置之前或者之后 添加字符串元素
*
* @param key
* @param where LIST_POSITION枚举类型
* @param pivot list里面的value
* @param value 添加的value
* @return
*/
public static Long linsert(String key, BinaryClient.LIST_POSITION where, String pivot, String value) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.linsert(key, where, pivot, value);
} finally {
returnResource(jedis);
}
}
/**
* 通过key设置list指定下标位置的value
* 如果下标超过list里面value的个数则报错
*
* @param key
* @param index 从0开始
* @param value
* @return 成功返回OK
*/
public static String lset(String key, Long index, String value) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.lset(key, index, value);
} finally {
returnResource(jedis);
}
}
/**
* 通过key从对应的list中删除指定的count个 和 value相同的元素
*
* @param key
* @param count 当count为0时删除全部
* @param value
* @return 返回被删除的个数
*/
public static Long lrem(String key, long count, String value) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.lrem(key, count, value);
} finally {
returnResource(jedis);
}
}
/**
* 通过key保留list中从strat下标开始到end下标结束的value值
*
* @param key
* @param start
* @param end
* @return 成功返回OK
*/
public static String ltrim(String key, long start, long end) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.ltrim(key, start, end);
} finally {
returnResource(jedis);
}
}
/**
* 通过key从list的头部删除一个value,并返回该value
*
* @param key
* @return
*/
public static String lpop(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.lpop(key);
} finally {
returnResource(jedis);
}
}
/**
* 通过key从list尾部删除一个value,并返回该元素
*
* @param key
* @return
*/
public static String rpop(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.rpop(key);
} finally {
returnResource(jedis);
}
}
/**
* 通过key从一个list的尾部删除一个value并添加到另一个list的头部,并返回该value
* 如果第一个list为空或者不存在则返回null
*
* @param srckey
* @param dstkey
* @return
*/
public static String rpoplpush(String srckey, String dstkey) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.rpoplpush(srckey, dstkey);
} finally {
returnResource(jedis);
}
}
/**
* 通过key获取list中指定下标位置的value
*
* @param key
* @param index
* @return 如果没有返回null
*/
public static String lindex(String key,
long index) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.lindex(key, index);
} finally {
returnResource(jedis);
}
}
/**
* 通过key返回list的长度
*
* @param key
* @return
*/
public static Long llen(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.llen(key);
} finally {
returnResource(jedis);
}
}
/**
* 通过key获取list指定下标位置的value
* 如果start 为 0 end 为 -1 则返回全部的list中的value
*
* @param key
* @param start
* @param end
* @return
*/
public static List lrange(String key,
long start,
long end) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.lrange(key, start, end);
} finally {
returnResource(jedis);
}
}
/**
* 通过key向指定的set中添加value
*
* @param key
* @param members 可以是一个String 也可以是一个String数组
* @return 添加成功的个数
*/
public static Long sadd(String key, String... members) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.sadd(key, members);
} finally {
returnResource(jedis);
}
}
/**
* 通过key删除set中对应的value值
*
* @param key
* @param members 可以是一个String 也可以是一个String数组
* @return 删除的个数
*/
public static Long srem(String key, String... members) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.srem(key, members);
} finally {
returnResource(jedis);
}
}
/**
* 通过key随机删除一个set中的value并返回该值
*
* @param key
* @return
*/
public static String spop(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.spop(key);
} finally {
returnResource(jedis);
}
}
/**
* 通过key获取set中的差集
* 以第一个set为标准
*
* @param keys 可以 是一个string 则返回set中所有的value 也可以是string数组
* @return
*/
public static Set sdiff(String... keys) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.sdiff(keys);
} finally {
returnResource(jedis);
}
}
/**
* 通过key获取set中的差集并存入到另一个key中
* 以第一个set为标准
*
* @param dstkey 差集存入的key
* @param keys 可以 是一个string 则返回set中所有的value 也可以是string数组
* @return
*/
public static Long sdiffstore(String dstkey, String... keys) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.sdiffstore(dstkey, keys);
} finally {
returnResource(jedis);
}
}
/**
* 通过key获取指定set中的交集
*
* @param keys 可以 是一个string 也可以是一个string数组
* @return
*/
public static Set sinter(String... keys) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.sinter(keys);
} finally {
returnResource(jedis);
}
}
/**
* 通过key获取指定set中的交集 并将结果存入新的set中
*
* @param dstkey
* @param keys 可以 是一个string 也可以是一个string数组
* @return
*/
public static Long sinterstore(String dstkey, String... keys) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.sinterstore(dstkey, keys);
} finally {
returnResource(jedis);
}
}
/**
* 通过key返回所有set的并集
*
* @param keys 可以 是一个string 也可以是一个string数组
* @return
*/
public static Set sunion(String... keys) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.sunion(keys);
} finally {
returnResource(jedis);
}
}
/**
* 通过key返回所有set的并集,并存入到新的set中
*
* @param dstkey
* @param keys 可以 是一个string 也可以是一个string数组
* @return
*/
public static Long sunionstore(String dstkey, String... keys) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.sunionstore(dstkey, keys);
} finally {
returnResource(jedis);
}
}
/**
* 通过key将set中的value移除并添加到第二个set中
*
* @param srckey 需要移除的
* @param dstkey 添加的
* @param member set中的value
* @return
*/
public static Long smove(String srckey, String dstkey, String member) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.smove(srckey, dstkey, member);
} finally {
returnResource(jedis);
}
}
/**
* 通过key获取set中value的个数
*
* @param key
* @return
*/
public static Long scard(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.scard(key);
} finally {
returnResource(jedis);
}
}
/**
* 通过key判断value是否是set中的元素
*
* @param key
* @param member
* @return
*/
public static Boolean sismember(String key, String member) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.sismember(key, member);
} finally {
returnResource(jedis);
}
}
/**
* 通过key获取set中随机的value,不删除元素
*
* @param key
* @return
*/
public static String srandmember(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.srandmember(key);
} finally {
returnResource(jedis);
}
}
/**
* 通过key获取set中所有的value
*
* @param key
* @return
*/
public static Set smembers(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.smembers(key);
} finally {
returnResource(jedis);
}
}
/**
* 通过key向zset中添加value,score,其中score就是用来排序的
* 如果该value已经存在则根据score更新元素
*
* @param key
* @param score
* @param member
* @return
*/
public static Long zadd(String key, double score, String member) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.zadd(key, score, member);
} finally {
returnResource(jedis);
}
}
/**
* 通过key删除在zset中指定的value
*
* @param key
* @param members 可以 是一个string 也可以是一个string数组
* @return
*/
public static Long zrem(String key, String... members) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.zrem(key, members);
} finally {
returnResource(jedis);
}
}
/**
* 通过key增加该zset中value的score的值
*
* @param key
* @param score
* @param member
* @return
*/
public static Double zincrby(String key, double score, String member) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.zincrby(key, score, member);
} finally {
returnResource(jedis);
}
}
/**
* 通过key返回zset中value的排名
* 下标从小到大排序
*
* @param key
* @param member
* @return
*/
public static Long zrank(String key, String member) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.zrank(key, member);
} finally {
returnResource(jedis);
}
}
/**
* 通过key返回zset中value的排名
* 下标从大到小排序
*
* @param key
* @param member
* @return
*/
public static Long zrevrank(String key, String member) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.zrevrank(key, member);
} finally {
returnResource(jedis);
}
}
/**
* 通过key将获取score从start到end中zset的value
* socre从大到小排序
* 当start为0 end为-1时返回全部
*
* @param key
* @param start
* @param end
* @return
*/
public static Set zrevrange(String key, long start, long end) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.zrevrange(key, start, end);
} finally {
returnResource(jedis);
}
}
/**
* 通过key返回指定score内zset中的value
*
* @param key
* @param max
* @param min
* @return
*/
public static Set zrangebyscore(String key, String max, String min) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.zrevrangeByScore(key, max, min);
} finally {
returnResource(jedis);
}
}
/**
* 通过key返回指定score内zset中的value
*
* @param key
* @param max
* @param min
* @return
*/
public static Set zrangeByScore(String key, double max, double min) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.zrevrangeByScore(key, max, min);
} finally {
returnResource(jedis);
}
}
/**
* 返回指定区间内zset中value的数量
*
* @param key
* @param min
* @param max
* @return
*/
public static Long zcount(String key, String min, String max) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.zcount(key, min, max);
} finally {
returnResource(jedis);
}
}
/**
* 通过key返回zset中的value个数
*
* @param key
* @return
*/
public static Long zcard(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.zcard(key);
} finally {
returnResource(jedis);
}
}
/**
* 通过key获取zset中value的score值
*
* @param key
* @param member
* @return
*/
public static Double zscore(String key, String member) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.zscore(key, member);
} finally {
returnResource(jedis);
}
}
/**
* 通过key删除给定区间内的元素
*
* @param key
* @param start
* @param end
* @return
*/
public static Long zremrangeByRank(String key, long start, long end) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.zremrangeByRank(key, start, end);
} finally {
returnResource(jedis);
}
}
/**
* 通过key删除指定score内的元素
*
* @param key
* @param start
* @param end
* @return
*/
public static Long zremrangeByScore(String key, double start, double end) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.zremrangeByScore(key, start, end);
} finally {
returnResource(jedis);
}
}
/**
* 返回满足pattern表达式的所有key
* keys(*) 尽量不要用此方法,数据量太多会导致线程阻塞,匹配可控制范围数据量小情况下可以使用
* 返回所有的key
*
* @param pattern
* @return
*/
@Deprecated
public static Set keys(String pattern) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.keys(pattern);
} finally {
returnResource(jedis);
}
}
/**
* 通过key判断值得类型
*
* @param key
* @return
*/
public static String type(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.type(key);
} finally {
returnResource(jedis);
}
}
/**bit map*/
/**
* 设置key指定偏移量的值
* @author yupanpan
* @date 2019/11/15 11:09
* @param key
* @param offset
* @param value
* @return java.lang.Boolean
*/
public static Boolean setbit(String key, long offset, boolean value) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.setbit(key,offset,value);
} finally {
returnResource(jedis);
}
}
/**
* 设置key指定偏移量的值
* @author yupanpan
* @date 2019/11/15 11:09
* @param key
* @param offset
* @param value 只能是1或0字符串
* @return java.lang.Boolean
*/
public static Boolean setbit(String key, long offset, String value) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.setbit(key,offset,value);
} finally {
returnResource(jedis);
}
}
/**
* 获取key指定偏移量的值
* @author yupanpan
* @date 2019/11/15 11:09
* @param key
* @param offset
* @return java.lang.Boolean
*/
public static Boolean getbit(String key, long offset) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.getbit(key,offset);
} finally {
returnResource(jedis);
}
}
/**
* 统计key 偏移量被设置为1的bit数.
* @author yupanpan
* @date 2019/11/15 11:09
* @param key
* @return java.lang.Boolean
*/
public static Long bitcount(String key) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.bitcount(key);
} finally {
returnResource(jedis);
}
}
/**
* 一般情况下,给定的整个字符串都会被进行计数,通过指定额外的 start 或 end 参数,可以让计数只在特定的位上进行。
* start 和 end 参数的设置和 GETRANGE 命令类似,都可以使用负数值:比如 -1 表示最后一个位,而 -2 表示倒数第二个位,以此类推。
* 不存在的 key 被当成是空字符串来处理,因此对一个不存在的 key 进行 BITCOUNT 操作,结果为 0 。
* @author yupanpan
* @date 2019/11/15 11:12
* @param key
* @param start
* @param end
* @return java.lang.Long
*/
public static Long bitcount(String key, long start, long end) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.bitcount(key, start, end);
} finally {
returnResource(jedis);
}
}
/**
* 对一个或多个保存二进制位的字符串 key 进行位元操作,并将结果保存到 destkey 上,时间复杂度:O(N),尽量少用
* 支持 AND 、 OR 、 NOT 、 XOR 这四种操作中的任意一种参数--BitOP
* @author yupanpan
* @date 2019/11/15 11:14
* @param op
* @param destKey
* @param srcKeys
* @return java.lang.Long
*/
public static Long bitop(BitOP op, String destKey, String... srcKeys) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.bitop(op, destKey, srcKeys);
} finally {
returnResource(jedis);
}
}
/**
* 返回字符串里面第一个被设置为value的bit位/偏移量
* @author yupanpan
* @date 2019/11/15 11:18
* @param key
* @param value true在redis bitmap二进制字符串代表为1 false为0
* @return java.lang.Long
*/
public static Long bitpos(String key, boolean value) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.bitpos(key,value);
} finally {
returnResource(jedis);
}
}
public static Long bitpos(String key, boolean value, BitPosParams params) {
Jedis jedis = jedisUtils.jedisConfig.getJedis();
try {
return jedis.bitpos(key,value,params);
} finally {
returnResource(jedis);
}
}
/**
* 释放jedis资源
*
* @param jedis
*/
@SuppressWarnings("deprecation")
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
}