Java Code Examples for org.redisson.api.RedissonClient#getAtomicLong()

The following examples show how to use org.redisson.api.RedissonClient#getAtomicLong() . 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: NFFTRedisStorage.java    From Panako with GNU Affero General Public License v3.0 6 votes vote down vote up
public NFFTRedisStorage() {
	Config config = new Config();
	config.useSingleServer().setAddress("127.0.0.1:6379");
	//config.useSingleServer().setAddress("157.193.92.74:6379");

	final RedissonClient redisson = Redisson.create(config);
	fingerprintMap = redisson.getMap("integerMap");
	metaDataMap = redisson.getMap("descriptionMap");
	
	secondsStored = redisson.getAtomicLong("secondsStoredAtomicLong");
			
	Runtime.getRuntime().addShutdownHook(new Thread(new Runnable(){
		@Override
		public void run() {
			redisson.shutdown();
		}}));
	
	//rnd = new Random();
}
 
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: CacheSyncUtil.java    From j360-dubbo-app-all with Apache License 2.0 5 votes vote down vote up
public static Long readCount(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)) {
        return 0L;
    }
    return rResultLong.get();
}
 
Example 5
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 6
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());
                }
            }
        }
    }
}
 
Example 7
Source File: RedissonLongAdder.java    From redisson with Apache License 2.0 4 votes vote down vote up
public RedissonLongAdder(CommandAsyncExecutor connectionManager, String name, RedissonClient redisson) {
    super(connectionManager, name, redisson);
    
    atomicLong = redisson.getAtomicLong(getName());
}
 
Example 8
Source File: CacheSyncUtil.java    From j360-dubbo-app-all with Apache License 2.0 3 votes vote down vote up
/**
 * 回写补偿数据
 * @param id
 * @param column
 * @param key
 * @param redissonClient
 * @param baseDao
 */
public static void writeCompensateCount(Long id, String column,String key, RedissonClient redissonClient,BaseDao baseDao) {
    RAtomicLong rCacheLong = redissonClient.getAtomicLong(key);
    if (retryCountToCache(id, column, key, 0, rCacheLong, redissonClient, baseDao)) {
        //回写DB加减1
        //baseDao.updateCountValue(column, id);
    }
}
 
Example 9
Source File: RedissionUtils.java    From Redis_Learning with Apache License 2.0 2 votes vote down vote up
/** 
 * ��ȡԭ���� 
 * @param redisson 
 * @param objectName 
 * @return 
 */  
public RAtomicLong getRAtomicLong(RedissonClient redisson,String objectName){  
    RAtomicLong rAtomicLong=redisson.getAtomicLong(objectName);  
    return rAtomicLong;  
}