redisキャッシュ期限切れリスニングツールクラス-期限切れキャッシュをリスニングして削除
5641 ワード
1.イベントはRedisの購読と配布機能(pub/sub)によって配布されるため、購読する必要がある
阅_keyevent@0__:expiredチャネル
0はdb 0が自分のdbindexに基づいて適切な数字を選択することを表す.
2.redisを修正する.confファイル
クライアントコマンドconfig set notify-keyspace-events Exの実行
# K , __keyspace@__
# E , __keysevent@__
# g del , expipre , rename , ...
# $ String
# l List
# s Set
# h Hash
# z
# x ( key )
# e ( key )
# A g$lshzxe , ”AKE”
package com.zjht.solar.listenter; import com.zjht.solar.act.constant.TransTypeString; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.data.redis.connection.jedis.JedisConnection; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPubSub; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; /** * 。 * Created by gaosiling on 2017/11/24。 */ public class KeyExpiredListener2 extends JedisPubSub implements ApplicationListener
{ private static final Logger logger = LoggerFactory.getLogger(KeyExpiredListener2.class); @Autowired private JedisConnectionFactory jedisConnectionFactory; @Autowired private RedisTemplate redisTemplate; // @Override public void onPSubscribe(String pattern, int subscribedChannels) { } // @Override public void onPMessage(String pattern, String channel, String message) { try { logger.info(" key=============" + message); if (message.indexOf(TransTypeString.ACTIVITYCODE) == -1) { return; } String key = message.substring("activityCode_".length()); logger.info(" " + key); // deleteKey(key); } catch (Exception ex) { logger.info(" " + ex); } } // , redis @Override public void onApplicationEvent(ContextRefreshedEvent event) { new Thread(new Runnable() { @Override public void run() { // , spring 。 logger.info(" redis "); JedisConnection jedisConnection = (JedisConnection) jedisConnectionFactory.getConnection(); Jedis jedis = jedisConnection.getNativeConnection(); jedis.psubscribe(new KeyExpiredListener2(), "__key*__:*"); } }).start(); } public void deleteKey(String key) { List redisActivity = new ArrayList<>(); if (redisActivity == null || redisActivity.size() == 0) { return; } Iterator it = redisActivity.iterator(); while (it.hasNext()) { String s = it.next(); if (s.equals(key)) { it.remove(); } } setActivity(key, redisActivity); } private void setActivity(String key, List list) { delete(key); if (list.size() == 0) { return; } redisTemplate.opsForList().leftPushAll(key, list); redisTemplate.expireAt(key, new Date()); JedisConnection jedisConnection = (JedisConnection) jedisConnectionFactory.getConnection(); Jedis jedis = jedisConnection.getNativeConnection(); for (String vo : list) { try { long time = new Date().getTime() - System.currentTimeMillis(); if (time < 0) { time = 0; } jedis.setex(TransTypeString.ACTIVITYCODE + new Date(), (int) (time / 1000), "nx"); } catch (Exception ex) { logger.info(" " + key + ex); } } jedisConnection.close(); } /** * 。 * * @param key key。 */ private void delete(String key) { redisTemplate.delete(key); } }