返回顶部
首页 > 资讯 > 数据库 >redis监听key过期事件的详细步骤
  • 382
分享到

redis监听key过期事件的详细步骤

redis监听key过期事件redis监听key 2022-08-10 20:08:19 382人浏览 八月长安
摘要

目录1、配置Redis.conf文件2、 配置一个key过期事件的监听器3、订阅key过期事件4、发起订阅1、配置redis.conf文件 配置文件默认是#注释了的,改为notify-keyspace-even

1、配置redis.conf文件

配置文件默认是#注释了的,改为notify-keyspace-events Ex    重启redis,记住指定redis.conf配置文件启动

如果是阿里云的redis,进入redis管理页面==>>左边导航栏的参数设置

redis监听key过期事件的详细步骤

2、 配置一个key过期事件的监听器

package com.shinedata.config.redis;
 
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.JedisPubSub;
 

public class RedisKeyExpiredListener extends JedisPubSub {
    
    private static Logger logger	= LoggerFactory.getLogger(RedisKeyExpiredListener.class);
 
    
    @Override
    public void onMessage(String channel, String message) {
        logger.info("channel{" + channel + "}message{" + message + "}");
    }
 
    
    @Override
    public void onPMessage(String pattern, String channel, String message) {
        logger.info("Redis订阅监听超时通知开始pattern{" + pattern + "}channel{" + channel + "}message{" + message + "}");
        long starTime = System.currentTimeMillis();
        if (StringUtils.isBlank(message)) {
            logger.info("Redis订阅监听超时通知,message为空");
            return;
        }
        long endTime = System.currentTimeMillis();
        logger.info("Redis订阅监听超时通知完成pattern{" + pattern + "}channel{" + channel + "}message{" + message + "}共耗时{"
                + (endTime - starTime) / 1000 + "}秒");
    }
}

3、订阅key过期事件

key过期时redis会自动把key推进订阅事件,配置上面自己的监听器,有key过期事件会通过onMessage方法进入,处理自己的业务逻辑,删除key不会通知

package com.shinedata.config.redis;
 
import redis.clients.jedis.Jedis;
 

public class RedisKeyExpiredSubscriberRunnable implements Runnable{
 
    @Override
    public void run() {
        Jedis jedis = JedisUtils.getJedis();
        RedisKeyExpiredListener listener=new RedisKeyExpiredListener();
        jedis.subscribe(listener,"__keyevent@1__:expired");
        JedisUtils.returnResource(jedis);
    }
}

索引库根据自己业务需要选择,__keyevent@1__:expired       1代表为订阅索引库1中的key过期,索引库有0-15

如果不写为全部索引库,为 __keyevent@__:expired

4、发起订阅

这里在程序应用启动时发起(使用spring自带的Runner,在服务启动时会执行相应的run方法)

package com.shinedata.config.redis;
 
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
 

@Component
public class RedisPublisherRunner implements CommandLineRunner {
 
    @Override
    public void run(String... args) throws Exception {
        new Thread(new RedisKeyExpiredSubscriberRunnable()).start();
    }
}

测试

package com.shinedata;
 
import com.Google.common.collect.Lists;
import com.shinedata.config.redis.JedisUtils;
import com.shinedata.enums.InstScaleEnum;
import com.shinedata.enums.SchoolTypeEnum;
import com.shinedata.enums.StatusEnum;
import com.shinedata.enums.UserTypeEnum;
import com.shinedata.order.UserInstOrder;
import com.shinedata.order.UserOrder;
import com.shinedata.service.order.OrderAsyncService;
import com.shinedata.util.RetryUtils;
import com.shinedata.util.list.ListUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
import com.shinedata.service.UserService;
import com.shinedata.timer.HuifuBillTimer;
import redis.clients.jedis.Jedis;
 
import Java.util.List;
 
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class )//这里是启动类
public class HuiBillTest extends BaseTest {
	@Autowired
	UserService userService;
 
	@Autowired
	private HuifuBillTimer huifuBillTimer;
 
	@Autowired
	OrderAsyncService orderAsyncService;
	
	@Test
	public void sendHuifuBill() throws InterruptedException {
		Jedis jedis = JedisUtils.getJedis();
		jedis.select(1);
		jedis.setex("ypp", 20, "ypp");
	}
 
}

结果:

redis监听key过期事件的详细步骤

其他配置redis和工具类,这里配置的默认索引库为0.如果操作其他索引库,自行配置或者直接使用jedis.select(int database)

JedisConfig

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;
 

@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);
		}
	}
	
	
	static {
		initialPool();
	}
	
	
	private static synchronized void poolInit() {
		if (jedisPool == null) {
			initialPool();
		}
	}
	
	
	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;
	}
	
	
	@SuppressWarnings("deprecation")
	public static void returnResource(final Jedis jedis) {
		if (jedis != null) {
			jedis.close();
		}
	}
}

JedisUtils

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;
 

@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();
    }
 
    
    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);
        }
    }
 
 
    
    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);
        }
    }
 
    
    public static Long del(String... keys) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.del(keys);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long append(String key, String str) {
 
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.append(key, str);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Boolean exists(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        Boolean exists;
        try {
            exists = jedis.exists(key);
        } finally {
            jedis.close();
        }
        return exists;
    }
 
    
    public static Long setnx(String key, String value) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.setnx(key, value);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    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;
    }
 
    
    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;
    }
 
    
    public static List<String> mget(String... keys) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.mget(keys);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static String mset(String... keysValues) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.mset(keysValues);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long msetnx(String... keysValues) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.msetnx(keysValues);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static String getSet(String key, String value) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.getSet(key, value);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    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);
        }
    }
 
    
    public static Long incr(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.incr(key);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long incrBy(String key, long integer) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.incrBy(key, integer);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long decr(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.decr(key);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long decrBy(String key, long integer) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.decrBy(key, integer);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long strLen(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.strlen(key);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    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);
        }
    }
 
    
    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);
        }
    }
 
    
    public static String hmset(String key, Map<String, String> hash) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.hmset(key, hash);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static String hget(String key, String failed) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.hget(key, failed);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long expire(String key, int seconds) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.expire(key, seconds);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static List<String> hmget(String key, String... fields) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.hmget(key, fields);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    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);
        }
    }
 
    
    public static Boolean hexists(String key, String field) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.hexists(key,
                    field);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long hlen(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.hlen(key);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long hdel(String key, String... fields) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.hdel(key
                    , fields);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Set<String> hkeys(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.hkeys(key);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static List<String> hvals(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.hvals(key);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Map<String, String> hgetall(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.hgetAll(key);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long lpush(String key, String... strs) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.lpush(key, strs);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long rpush(String key, String... strs) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.rpush(key, strs);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    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);
        }
    }
 
    
    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);
        }
    }
 
    
    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);
        }
    }
 
    
    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);
        }
    }
 
    
    public static String lpop(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.lpop(key);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static String rpop(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.rpop(key);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static String rpoplpush(String srckey, String dsTKEy) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.rpoplpush(srckey, dstkey);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static String lindex(String key,
                                long index) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.lindex(key, index);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long llen(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.llen(key);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static List<String> lrange(String key,
                                      long start,
                                      long end) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.lrange(key, start, end);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long sadd(String key, String... members) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.sadd(key, members);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long srem(String key, String... members) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.srem(key, members);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static String spop(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.spop(key);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Set<String> sdiff(String... keys) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.sdiff(keys);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long sdiffstore(String dstkey, String... keys) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.sdiffstore(dstkey, keys);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Set<String> sinter(String... keys) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.sinter(keys);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long sinterstore(String dstkey, String... keys) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.sinterstore(dstkey, keys);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Set<String> suNIOn(String... keys) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.sunion(keys);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long sunionstore(String dstkey, String... keys) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.sunionstore(dstkey, keys);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    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);
        }
    }
 
    
    public static Long scard(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.scard(key);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Boolean sismember(String key, String member) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.sismember(key, member);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static String srandmember(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.srandmember(key);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Set<String> smembers(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.smembers(key);
        } finally {
            returnResource(jedis);
        }
    }
 
 
    
    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);
        }
    }
 
    
    public static Long zrem(String key, String... members) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.zrem(key, members);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    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);
        }
    }
 
    
    public static Long zrank(String key, String member) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.zrank(key, member);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Long zrevrank(String key, String member) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.zrevrank(key, member);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Set<String> zrevrange(String key, long start, long end) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.zrevrange(key, start, end);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Set<String> zrangebyscore(String key, String max, String min) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.zrevrangeByScore(key, max, min);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Set<String> zrangeByScore(String key, double max, double min) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.zrevrangeByScore(key, max, min);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    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);
        }
    }
 
    
    public static Long zcard(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.zcard(key);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static Double zscore(String key, String member) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.zscore(key, member);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    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);
        }
    }
 
    
    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);
        }
    }
 
    
    @Deprecated
    public static Set<String> keys(String pattern) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.keys(pattern);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    public static String type(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.type(key);
        } finally {
            returnResource(jedis);
        }
    }
 
    
    
    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);
        }
    }
    
    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);
        }
    }
    
    public static Boolean getbit(String key, long offset) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.getbit(key,offset);
        } finally {
            returnResource(jedis);
        }
    }
    
    public static Long bitcount(String key) {
        Jedis jedis = jedisUtils.jedisConfig.getJedis();
        try {
            return jedis.bitcount(key);
        } finally {
            returnResource(jedis);
        }
    }
    
    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);
        }
    }
    
    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);
        }
    }
    
    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);
        }
    }
 
 
    
    @SuppressWarnings("deprecation")
    public static void returnResource(final Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }
}

代码复制粘贴可用,需要修改为自己的redis链接信息

到此这篇关于redis监听key过期事件的详细步骤的文章就介绍到这了,更多相关redis监听key过期事件内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

您可能感兴趣的文档:

--结束END--

本文标题: redis监听key过期事件的详细步骤

本文链接: https://lsjlt.com/news/33635.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • redis监听key过期事件的详细步骤
    目录1、配置Redis.conf文件2、 配置一个key过期事件的监听器3、订阅key过期事件4、发起订阅1、配置redis.conf文件 配置文件默认是#注释了的,改为notify-keyspace-even...
    99+
    2022-08-10
    redis监听key过期事件 redis监听key
  • Redis监听过期的key实现流程详解
    目录一、简介二、maven依赖三、编码实现3.1、application.properties3.2、Redis配置类3.3、监听器3.4、服务类3.5、工具类四、测试4.1、测试类...
    99+
    2023-02-28
    Redis监听过期的key Redis监听key
  • 怎么在Redis集群中监听过期key
    这篇文章将为大家详细讲解有关怎么在Redis集群中监听过期key,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。redis.host1: 10.11...
    99+
    2024-04-02
  • Redis监听过期的key实现流程是什么
    本篇内容介绍了“Redis监听过期的key实现流程是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、简介我们来个最简单的集群架构,如下...
    99+
    2023-07-05
  • SpringBoot如何监听redis Key变化事件案例详解
    目录一 、功能概览二、事件类型三、配置三、案例代码新增和修改都是set指令删除过期总结键空间通知(keyspace notification) 一 、功能概览 键空间通知使得客户端可...
    99+
    2022-11-13
    springboot监听redis key变化 springboot监听redis过期key
  • JavaScript事件监听器详细介绍
    目录什么是事件1. 事件绑定2. 常见事件什么是事件 HTML 事件是发生在 HTML 元素上的“行为 ”。比如:页面上的 按钮被点击、鼠标移动到元素之上、按...
    99+
    2024-04-02
  • Redis Server启动过程的详细步骤
    目录1、 初始化参数配置2、 加载并解析配置文件3、 初始化服务器内部变量4、执行事件驱动框架本文基于社区版Redis 4.0.8 1、 初始化参数配置 由函数initServer...
    99+
    2024-04-02
  • JavaGUI事件监听机制超详细讲解
    1.一个事件模型中有上对象:事件源,事件以及监听程序 2.事件监听机制: 事件源        事件发生的地方事件    ...
    99+
    2023-03-03
    JavaGUI事件监听 Java事件监听事件
  • Spring的事件和监听器-同步与异步详解
    目录Spring的事件和监听器-同步与异步1、首先新建StartWorkflowEvent.java,2、新建一个监听器StartWorkflowListener.java3、创建一...
    99+
    2024-04-02
  • Redis 事务与过期时间详细介绍
    Redis 事务与过期时间详细介绍 一、Redis事务: Redis中支持事务,事务即为当我们需要执行几条命令时,要么这几条命令都不执行,要么都执行: 1、开始事务写入: multi 2、然后...
    99+
    2022-06-04
    详细介绍 事务 时间
  • 详解SpringBoot实现事件同步与异步监听
    目录简介事件监听简述实例同步监听(无序)同步监听(有序)异步监听(无序)简介 说明 本文用示例介绍SpringBoot中的事件的用法及原理。 事件监听简述 事件的发布与监听从属于观察...
    99+
    2024-04-02
  • Redis的详细安装步骤
    一、Redis简介 Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理。    它支持字符串(string)、哈希表(hash)、列表(list)、集合(set)、有序集合(zset / so...
    99+
    2023-09-03
    redis 数据库 java
  • springboot使用redis的详细步骤
    目录springboot使用redisspringboot使用redis步骤StringRedisTemplate和RedisTemplate总结 springboot使用...
    99+
    2024-04-02
  • Flume监听oracle表增量的步骤详解
    需求:获取oracle表增量信息,发送至udp514端口,支持ip配置 步骤: (1)需要的jar  oracle的 odbc5.jar(oracle安装目录  /jdbc/lib下查找...
    99+
    2024-04-02
  • JavaScript给事件委托批量添加事件监听详细流程
    1.什么是事件委托 事件委托:利用事件冒泡的特性,将本应该注册在子元素上的处理事件注册在父元素上,这样点击子元素时发现其本身没有相应事件就到父元素上寻找作出相应。这样做的优势有: ...
    99+
    2024-04-02
  • Redis通过scan查找不过期的 key(方法详解)
    Redis Scan 命令用于迭代数据库中的数据库键。 SCAN 返回一个包含两个元素的数组, 第一个元素是用于进行下一次迭代的新游标, 而第二个元素则是一个数组, 这个数组中包含了...
    99+
    2024-04-02
  • 详细聊聊Redis的过期策略
    保存过期时间 Redis可以为每个key设置过期时间,会将每个设置了过期时间的key放入一个独立的字典中。 typedef struct redisDb { int id; //i...
    99+
    2024-04-02
  • Flutter事件监听与EventBus事件的应用详解
    目录一 指针事件 Pointer二 手势识别三 跨组件事件的传递在Flutter中,手势有两个不同的层次: 第一层:原始指针事件(Pointer Events) 第二层:手势识别(G...
    99+
    2023-05-15
    Flutter事件监听 Flutter EventBus
  • springboot 事件监听器的案例详解
    目录前言引导案例一、通过实现ApplicationListener接口实现步骤1、自定义一个事件类(对象),继承ApplicationEvent2、自定义业务类实现Applicati...
    99+
    2024-04-02
  • Vue中通过minio上传文件的详细步骤
    目录1、minio的安装2、minio.js文件2.1连接minio2.2通过stream流上传2.3通过带预签名的url上传(最好是minio设置了链接永久访问)2.4删除对象补充...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作