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

The following examples show how to use org.redisson.api.RAtomicLong#set() . 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: IpRule.java    From kk-anti-reptile with Apache License 2.0 6 votes vote down vote up
/**
 * 重置已记录规则
 * @param request 请求
 * @param realRequestUri 原始请求uri
 */
@Override
public void reset(HttpServletRequest request, String realRequestUri) {
    String ipAddress = getIpAddr(request);
    String requestUrl = realRequestUri;
    /**
     * 重置计数器
     */
    int expirationTime = properties.getIpRule().getExpirationTime();
    RAtomicLong rRequestCount = redissonClient.getAtomicLong(RATELIMITER_COUNT_PREFIX.concat(requestUrl).concat(ipAddress));
    RAtomicLong rExpirationTime = redissonClient.getAtomicLong(RATELIMITER_EXPIRATIONTIME_PREFIX.concat(requestUrl).concat(ipAddress));
    rRequestCount.set(0L);
    rExpirationTime.set(0L);
    rExpirationTime.expire(expirationTime, TimeUnit.MILLISECONDS);
    /**
     * 清除记录
     */
    RMap rHitMap = redissonClient.getMap(RATELIMITER_HIT_CRAWLERSTRATEGY);
    rHitMap.remove(ipAddress);
}
 
Example 2
Source File: CacheSyncManager.java    From j360-dubbo-app-all with Apache License 2.0 6 votes vote down vote up
private boolean checkStepCondition(CacheSyncEvent cacheSyncEvent, RAtomicLong rCacheLong) {
    //
    RAtomicLong rRanageAtomicLong = redissonClient.getAtomicLong(rCacheLong.getName() + "_range");
    long ttl = rRanageAtomicLong.remainTimeToLive();
    long value = rRanageAtomicLong.get();

    if (ttl < AppConfig.COMMON_COUNT_RANGE_DAYS * 24 * 3600 * 1000 - AppConfig.CACHE_TIME_RANGE) {
        rRanageAtomicLong.expire(AppConfig.COMMON_COUNT_RANGE_DAYS, TimeUnit.DAYS);
        return true;
    }
    if (cacheSyncEvent.getCacheValue()-value >= AppConfig.CACHE_COUNT_RANGE) {
        rRanageAtomicLong.set(cacheSyncEvent.getCacheValue());
        return true;
    }
    return false;
    //return true;
}
 
Example 3
Source File: RedissonAtomicLongTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
    RAtomicLong al = redisson.getAtomicLong("test");
    Assert.assertEquals(0, al.get());
    Assert.assertEquals(0, al.getAndIncrement());
    Assert.assertEquals(1, al.get());
    Assert.assertEquals(1, al.getAndDecrement());
    Assert.assertEquals(0, al.get());
    Assert.assertEquals(0, al.getAndIncrement());
    Assert.assertEquals(1, al.getAndSet(12));
    Assert.assertEquals(12, al.get());
    al.set(1);

    long state = redisson.getAtomicLong("test").get();
    Assert.assertEquals(1, state);
    al.set(Long.MAX_VALUE - 1000);

    long newState = redisson.getAtomicLong("test").get();
    Assert.assertEquals(Long.MAX_VALUE - 1000, newState);
}
 
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: RedissionUtilsTest.java    From Redis_Learning with Apache License 2.0 5 votes vote down vote up
/** 
 * RAtomicLong ӳ��Ϊredis server��string ���� 
 * string����ֵ 
 * �鿴���м�---->keys * 
 * �鿴key������--->type testAtomicLong 
 * �鿴key��ֵ ---->get testAtomicLong  
 */  
@Test  
public void testGetRAtomicLong() {  
    RAtomicLong rAtomicLong=RedissionUtils.getInstance().getRAtomicLong(redisson, "testAtomicLong");  
    rAtomicLong.set(100);  
    System.out.println(rAtomicLong.addAndGet(200));  
    System.out.println(rAtomicLong.decrementAndGet());  
    System.out.println(rAtomicLong.get());  
}
 
Example 6
Source File: TracingRedissonTest.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void test_atomic_long() {
  RAtomicLong atomicLong = client.getAtomicLong("atomic_long");

  atomicLong.set(10);
  assertEquals(10, atomicLong.get());

  List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(2, spans.size());
  checkSpans(spans);
  assertNull(tracer.activeSpan());
}
 
Example 7
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 8
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 9
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 10
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 11
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;
}
 
Example 12
Source File: ShopifyJacksonDecoder.java    From shopify-api-java-wrapper with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(Response response, Type type) throws IOException
{
    Collection<String> shopifyApiCallLimitHeader = response.headers().get("HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT");

    String[] callLimitValues = shopifyApiCallLimitHeader.iterator().next().split("/");

    if(callLimitValues[0] != null && callLimitValues[0] != "")
    {
        Long createdCalls = Long.parseLong(callLimitValues[0]);

        Long remainingCalls = _shopifyRedissonManager.calculateAvalableCredits(createdCalls);

        RedissonClient redisson = _shopifyRedissonManager.getRedissonClient();

        // Lock per shopify store. The lock is distributed, so it will work for multiple threads and applications.
        RLock lock = redisson.getLock(_shopifyRedissonManager.getMyShopifyUrl());

        RAtomicLong remainingCreditsAtomic = redisson.getAtomicLong(_shopifyRedissonManager.getRemainingCreditsKey());
        RAtomicLong lastRequestTimeAtomic = redisson.getAtomicLong(_shopifyRedissonManager.getLastRequestTimeKey());

        remainingCreditsAtomic.set(remainingCalls);
        lastRequestTimeAtomic.set(System.currentTimeMillis());

        lock.unlock();
    }

    return super.decode(response, type);
}
 
Example 13
Source File: RedissonAtomicLongTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAndDelete() {
    RAtomicLong al = redisson.getAtomicLong("test");
    al.set(10);
    assertThat(al.getAndDelete()).isEqualTo(10);
    assertThat(al.isExists()).isFalse();

    RAtomicLong ad2 = redisson.getAtomicLong("test2");
    assertThat(ad2.getAndDelete()).isZero();
}
 
Example 14
Source File: RedissonAtomicLongTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompareAndSetZero() {
    RAtomicLong al = redisson.getAtomicLong("test");
    Assert.assertTrue(al.compareAndSet(0, 2));
    Assert.assertEquals(2, al.get());

    RAtomicLong al2 = redisson.getAtomicLong("test1");
    al2.set(0);
    Assert.assertTrue(al2.compareAndSet(0, 2));
    Assert.assertEquals(2, al2.get());

}
 
Example 15
Source File: RedissonAtomicLongTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetThenIncrement() {
    RAtomicLong al = redisson.getAtomicLong("test");
    al.set(2);
    Assert.assertEquals(2, al.getAndIncrement());
    Assert.assertEquals(3, al.get());
}
 
Example 16
Source File: RequestLimitInterceptor.java    From shopify-api-java-wrapper with Apache License 2.0 4 votes vote down vote up
@Override
public void apply(RequestTemplate template)
{
    Boolean tryGetCredit = true;

    RedissonClient redisson = _shopifyRedissonManager.getRedissonClient();

    while (tryGetCredit)
    {
        // Lock per shopify store. The lock is distributed, so it will work for multiple threads and applications.
        RLock lock = redisson.getLock(_shopifyRedissonManager.getMyShopifyUrl());

        RAtomicLong isDefaultRemainingCreditsValueSet = redisson.getAtomicLong(_shopifyRedissonManager.getIsDefaultRemainingCreditsValueSetKey());

        RAtomicLong remainingCreditsAtomic = redisson.getAtomicLong(_shopifyRedissonManager.getRemainingCreditsKey());

        if(isDefaultRemainingCreditsValueSet.get() == 0)
        {
            remainingCreditsAtomic.set(_shopifyRedissonManager.getCreditLimit());
            isDefaultRemainingCreditsValueSet.set(1);
        }

        RAtomicLong lastRequestTimeAtomic = redisson.getAtomicLong(_shopifyRedissonManager.getLastRequestTimeKey());
        Long remainingCredits = remainingCreditsAtomic.get();

        if(remainingCredits > 0)
        {
            // These values are set here, because a request can be made while the current request is still in progress.
            // We set the actual values inside the decoder (when the request is complete), but if we don't set them here
            // as well a raised condition can occur.
            remainingCreditsAtomic.set(remainingCredits - 1);
            lastRequestTimeAtomic.set(System.currentTimeMillis());

            tryGetCredit = false;
            lock.unlock();
        }
        else
        {
            // Check if there were enough time since the last request time.
            // If the latest request's remaining calls were 0 and no calls were made after that, the remaining credits
            // will not be updated. This is why the last request time is used as well.
            long availableCalls = (long)Math.floor((System.currentTimeMillis() - lastRequestTimeAtomic.get())/500);

            if(availableCalls > 0)
            {
                remainingCreditsAtomic.set(availableCalls - 1);
                lastRequestTimeAtomic.set(System.currentTimeMillis());

                tryGetCredit = false;
                lock.unlock();
            }
            else
            {
                lock.unlock();

                try
                {
                    Thread.sleep(1000);
                }
                catch (InterruptedException e)
                {
                    System.out.println("Error while waiting for available Shopify call credit. " + e.getMessage());
                }
            }
        }
    }
}