Java Code Examples for org.redisson.api.RAtomicLong#expireAsync()

The following examples show how to use org.redisson.api.RAtomicLong#expireAsync() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CacheSyncManager.java    From j360-dubbo-app-all with Apache License 2.0 5 votes vote down vote up
/**
 * 读取count
 * @param id
 * @param cacheModelEnum
 * @return
 */
public Long readBizCount(Long id, CacheModelEnum cacheModelEnum) {
    String redisKey = String.format(cacheModelEnum.getRedisKey(), id);
    RAtomicLong rCacheLong = redissonClient.getAtomicLong(redisKey);
    RAtomicLong rResultLong = getSourceToCacheOnce(id, cacheModelEnum, rCacheLong);
    if (Objects.isNull(rResultLong)) {
        return 0L;
    }
    rCacheLong.expireAsync(AppConfig.COMMON_COUNT_CACHE_DAYS, TimeUnit.DAYS);
    return rResultLong.get();
}
 
Example 2
Source File: CacheSyncManager.java    From j360-dubbo-app-all with Apache License 2.0 5 votes vote down vote up
/**
 * 用于重试级别的MQ模式的Consumer回写Redis
 * 返回false则进行业务重试,对于mq不返回ack重试
 * @return
 */
private boolean retryCountToCache(Long id, int retryTimes, CacheModelEnum cacheModelEnum, RAtomicLong rCacheLong) {
    //默认锁10秒
    RedisLock redisLock = new RedisLock(redissonClient, rCacheLong.getName());
    if (retryTimes == 0) {
        retryTimes = Integer.MAX_VALUE;
    }
    while (retryTimes > 0) {
        retryTimes--;
        if (rCacheLong.isExists()) {
            return true;
        }
        try {
            //尝试等待锁100ms
            if (redisLock.lock(100)) {
                try {
                    if (!rCacheLong.isExists()) {
                        Long count = cacheDao.getCount(cacheModelEnum, id);
                        rCacheLong.set(Optional.ofNullable(count).orElse(0L));
                        rCacheLong.expireAsync(AppConfig.COMMON_COUNT_CACHE_DAYS, TimeUnit.DAYS);
                    }
                } finally {
                    redisLock.unlock();
                }
                return true;
            }
        } catch (InterruptedException e) {
            log.error("读取回源数据失败: [id={},model={}]", id, cacheModelEnum, e);
        }
    }
    return false;
}
 
Example 3
Source File: CacheSyncManager.java    From j360-dubbo-app-all with Apache License 2.0 5 votes vote down vote up
private RAtomicLong getSourceToCacheOnce(Long id, CacheModelEnum cacheModelEnum, RAtomicLong rCacheLong) {
    //在锁内完成数据的读取和回写,释放锁
    if (rCacheLong.isExists()) {
        return rCacheLong;
    }
    //默认锁10秒
    RedisLock redisLock = new RedisLock(redissonClient, rCacheLong.getName());
    try {
        //尝试等待锁100ms
        int retry = 2;
        while (retry > 0) {
            retry--;
            if (redisLock.lock(100)) {
                try {
                    if (!rCacheLong.isExists()) {
                        Long count = cacheDao.getCount(cacheModelEnum,id);
                        rCacheLong.set(Optional.ofNullable(count).orElse(0L));
                        rCacheLong.expireAsync(AppConfig.COMMON_COUNT_CACHE_DAYS, TimeUnit.DAYS);
                    }
                } finally {
                    redisLock.unlock();
                }
                if (rCacheLong.isExists()) {
                    return rCacheLong;
                }
            }
        }
    } catch (InterruptedException e) {
        log.error("读取回源数据失败: [id={},model={}]", id, cacheModelEnum, e);
    }
    //回源获取数据失败,需要进去MQ模式
    return null;
}
 
Example 4
Source File: CacheSyncUtil.java    From j360-dubbo-app-all with Apache License 2.0 5 votes vote down vote up
/**
 * 用于重试级别的MQ模式的Consumer回写Redis
 * 返回false则进行业务重试,对于mq不返回ack重试
 * @param id
 * @param column
 * @param key
 * @param retryTimes
 * @param rPostLong
 * @param redissonClient
 * @param baseDao
 * @return
 */
public static boolean retryCountToCache(Long id, String column, String key, int retryTimes, RAtomicLong rPostLong, RedissonClient redissonClient,BaseDao baseDao) {
    //默认锁10秒
    RedisLock redisLock = new RedisLock(redissonClient, key);
    while (retryTimes > 0) {
        retryTimes--;
        if (rPostLong.isExists()) {
            return true;
        }
        try {
            //尝试等待锁100ms
            if (redisLock.lock(100)) {
                try {
                    if (!rPostLong.isExists()) {
                        Long count = baseDao.getCount(column,id);
                        rPostLong.set(Optional.ofNullable(count).orElse(0L));
                        rPostLong.expireAsync(AppConfig.COMMON_COUNT_CACHE_DAYS, TimeUnit.DAYS);
                    }
                } finally {
                    redisLock.unlock();
                }
                return true;
            }
        } catch (InterruptedException e) {
            log.error("读取回源数据失败: [id={},column={},key={}]", id, column, key, e);
            rPostLong = null;
        }
    }
    return false;
}
 
Example 5
Source File: CacheSyncUtil.java    From j360-dubbo-app-all with Apache License 2.0 5 votes vote down vote up
public static RAtomicLong getSourceToCacheOnce(Long id, String column,String key, RAtomicLong rCacheLong, RedissonClient redissonClient,BaseDao baseDao) {
    //在锁内完成数据的读取和回写,释放锁
    if (rCacheLong.isExists()) {
        return rCacheLong;
    }
    //默认锁10秒
    RedisLock redisLock = new RedisLock(redissonClient, key);
    try {
        //尝试等待锁100ms
        int retry = 2;
        while (retry > 0) {
            retry--;
            if (redisLock.lock(100)) {
                try {
                    if (!rCacheLong.isExists()) {
                        Long count = baseDao.getCount(column,id);
                        rCacheLong.set(Optional.ofNullable(count).orElse(0L));
                        rCacheLong.expireAsync(AppConfig.COMMON_COUNT_CACHE_DAYS, TimeUnit.DAYS);
                    }
                } finally {
                    redisLock.unlock();
                }
                if (rCacheLong.isExists()) {
                    return rCacheLong;
                }
            }
        }
    } catch (InterruptedException e) {
        log.error("读取回源数据失败: [id={},column={},key={}]", id, column, key, e);
    }
    //回源获取数据失败,需要进去MQ模式
    return null;
}