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

The following examples show how to use org.redisson.api.RAtomicLong#incrementAndGet() . 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 6 votes vote down vote up
/**
 * MQ异步写去count
 * 需要修改cache的值
 * @param cacheSyncMessage
 */
public void writeMQCount(CacheSyncMessage cacheSyncMessage) {
    //封装
    RAtomicLong rCacheLong = redissonClient.getAtomicLong(cacheSyncMessage.getCacheKey());
    CacheModelEnum cacheModelEnum = CacheModelEnum.lookup(cacheSyncMessage.getIndex());

    //同步redis+db
    if (retryCountToCache(cacheSyncMessage.getId(), 0, cacheModelEnum, rCacheLong)) {
        //修改redis数据
        long value = cacheSyncMessage.isAdd()?rCacheLong.incrementAndGet():rCacheLong.decrementAndGet();
        if (value > 0) {
            writeValue(value, cacheSyncMessage.getId(), cacheModelEnum);
        }
    }

    rCacheLong.expire(AppConfig.COMMON_COUNT_CACHE_DAYS, TimeUnit.DAYS);
}
 
Example 2
Source File: CacheSyncUtil.java    From j360-dubbo-app-all with Apache License 2.0 6 votes vote down vote up
public static Long writeCount(DefaultAsyncEventBus eventBus,Long id, String column,String key, RedissonClient redissonClient,BaseDao baseDao) {
    RAtomicLong rCacheLong = redissonClient.getAtomicLong(key);
    RAtomicLong rResultLong = getSourceToCacheOnce(id, column, key, rCacheLong, redissonClient, baseDao);
    if (Objects.isNull(rResultLong)) {
        //TODO 进入MQ模式
        //mqProducter.send(message);
        return 0L;
    }

    long cacheCount = rResultLong.incrementAndGet();
    //进入步长模式,调用Event
    CacheSyncEvent event = new CacheSyncEvent();
    event.setCacheValue(cacheCount);
    eventBus.post(event);

    return cacheCount;
}
 
Example 3
Source File: AtomicLongExamples.java    From redisson-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();

    RAtomicLong atomicLong = redisson.getAtomicLong("myLong");
    atomicLong.getAndDecrement();
    atomicLong.getAndIncrement();
    
    atomicLong.addAndGet(10L);
    atomicLong.compareAndSet(29, 412);
    
    atomicLong.decrementAndGet();
    atomicLong.incrementAndGet();
    
    atomicLong.getAndAdd(302);
    atomicLong.getAndDecrement();
    atomicLong.getAndIncrement();
    
    redisson.shutdown();
}
 
Example 4
Source File: IpRule.java    From kk-anti-reptile with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected boolean doExecute(HttpServletRequest request, HttpServletResponse response) {
    String ipAddress = getIpAddr(request);
    List<String> ignoreIpList = properties.getIpRule().getIgnoreIp();
    if (ignoreIpList != null && ignoreIpList.size() > 0) {
        for (String ignoreIp : ignoreIpList) {
            if (ignoreIp.endsWith("*")) {
                ignoreIp = ignoreIp.substring(0, ignoreIp.length() - 1);
            }
            if (ipAddress.startsWith(ignoreIp)) {
                return false;
            }
        }
    }
    String requestUrl = request.getRequestURI();
    //毫秒,默认5000
    int expirationTime = properties.getIpRule().getExpirationTime();
    //最高expirationTime时间内请求数
    int requestMaxSize = properties.getIpRule().getRequestMaxSize();
    RAtomicLong rRequestCount = redissonClient.getAtomicLong(RATELIMITER_COUNT_PREFIX.concat(requestUrl).concat(ipAddress));
    RAtomicLong rExpirationTime = redissonClient.getAtomicLong(RATELIMITER_EXPIRATIONTIME_PREFIX.concat(requestUrl).concat(ipAddress));
    if (!rExpirationTime.isExists()) {
        rRequestCount.set(0L);
        rExpirationTime.set(0L);
        rExpirationTime.expire(expirationTime, TimeUnit.MILLISECONDS);
    } else {
        RMap rHitMap = redissonClient.getMap(RATELIMITER_HIT_CRAWLERSTRATEGY);
        if ((rRequestCount.incrementAndGet() > requestMaxSize) || rHitMap.containsKey(ipAddress)) {
            //触发爬虫策略 ,默认10天后可重新访问
            long lockExpire = properties.getIpRule().getLockExpire();
            rExpirationTime.expire(lockExpire, TimeUnit.SECONDS);
            //保存触发来源
            rHitMap.put(ipAddress, requestUrl);
            LOGGER.info("Intercepted request, uri: {}, ip:{}, request :{}, times in {} ms。Automatically unlock after {} seconds", requestUrl, ipAddress, requestMaxSize, expirationTime,lockExpire);
            return true;
        }
    }
    return false;
}
 
Example 5
Source File: RedissonInstrumentationTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void testAtomicLong() {
    try (Scope scope = tracer.startRootTransaction(getClass().getClassLoader()).withName("transaction").activateInScope()) {
        RAtomicLong atomicLong = redisson.getAtomicLong("AtomicLong");
        atomicLong.incrementAndGet();

        assertThat(atomicLong.get()).isEqualTo(1);
    }

    assertTransactionWithRedisSpans("INCR", "GET");
}
 
Example 6
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 writeBizCount(boolean add,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)) {
        //TODO 进入MQ模式
        CacheSyncMessage cacheSyncMessage = new CacheSyncMessage();
        cacheSyncMessage.setTable(cacheModelEnum.getTable());
        cacheSyncMessage.setColumn(cacheModelEnum.getColumn());
        cacheSyncMessage.setCacheKey(redisKey);
        cacheSyncMessage.setId(id);
        cacheSyncMessage.setIndex(cacheModelEnum.getIndex());
        cacheSyncProducer.send(cacheSyncMessage);
        return 0L;
    }

    //执行cache同步
    long cacheCount = add?rResultLong.incrementAndGet():rResultLong.decrementAndGet();
    if (cacheCount < 0) {
        rCacheLong.set(0L);
    }

    //TODO 进入步长模式,调用Event
    CacheSyncEvent event = new CacheSyncEvent();
    event.setTable(cacheModelEnum.getTable());
    event.setColumn(cacheModelEnum.getColumn());
    event.setCacheKey(redisKey);
    event.setIndex(cacheModelEnum.getIndex());
    event.setCacheValue(cacheCount);
    event.setId(id);
    defaultAsyncEventBus.post(event);

    return cacheCount;
}
 
Example 7
Source File: DelayedTask.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public Object call() throws Exception {
    try {
        Thread.sleep(delay);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    RAtomicLong counter = redisson.getAtomicLong(counterName);
    counter.incrementAndGet();
    return null;
}