com.litongjava.tio.utils.cache.redis.RedisExpireUpdateTask Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tio-utils Show documentation
Show all versions of tio-utils Show documentation
t-io is a aio framework for java
package com.litongjava.tio.utils.cache.redis;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
import org.redisson.api.RBucket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.litongjava.tio.utils.lock.SetWithLock;
/**
* 定时更新redis的过期时间
* @author tanyaowu
* 2017年8月14日 下午1:34:06
*/
public class RedisExpireUpdateTask {
private static Logger log = LoggerFactory.getLogger(RedisExpireUpdateTask.class);
private static boolean started = false;
private static Set set = new HashSet<>();
private static SetWithLock setWithLock = new SetWithLock<>(set);
public static RedisCacheFactory redisCacheFactory;
public static void add(String cacheName, String key, long expire) {
ExpireVo expireVo = new ExpireVo(cacheName, key, expire);
setWithLock.add(expireVo);
}
public static void start(RedisCacheFactory redisCacheFactory) {
if (started) {
return;
}
RedisExpireUpdateTask.redisCacheFactory = redisCacheFactory;
synchronized (RedisExpireUpdateTask.class) {
if (started) {
return;
}
started = true;
}
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
WriteLock writeLock = setWithLock.writeLock();
writeLock.lock();
try {
Set set = setWithLock.getObj();
for (ExpireVo expireVo : set) {
log.debug("更新缓存过期时间, cacheName:{}, key:{}, expire:{}", expireVo.getCacheName(), expireVo.getKey(),
expireVo.getTimeToIdleSeconds());
RedisCache redisCache = redisCacheFactory.getCache(expireVo.getCacheName());
RBucket bucket = redisCache.getBucket(expireVo.getKey());
bucket.expireAsync(expireVo.getTimeToIdleSeconds(), TimeUnit.SECONDS);
}
set.clear();
} catch (Throwable e) {
log.error(e.getMessage(), e);
} finally {
writeLock.unlock();
try {
Thread.sleep(1000 * 10);
} catch (InterruptedException e) {
log.error(e.toString(), e);
}
}
}
}
}, RedisExpireUpdateTask.class.getName()).start();
}
/**
*
* @author tanyaowu
*/
private RedisExpireUpdateTask() {
// this.redisson = redisson;
}
}