org.redisson.api.RAtomicLong Java Examples
The following examples show how to use
org.redisson.api.RAtomicLong.
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 |
/** * 重置已记录规则 * @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: RedissonAtomicLongTest.java From redisson with Apache License 2.0 | 6 votes |
@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 #3
Source File: AtomicLongExamples.java From redisson-examples with Apache License 2.0 | 6 votes |
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 | 6 votes |
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 #5
Source File: CacheSyncManager.java From j360-dubbo-app-all with Apache License 2.0 | 6 votes |
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 #6
Source File: CacheSyncManager.java From j360-dubbo-app-all with Apache License 2.0 | 6 votes |
/** * 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 #7
Source File: CacheSyncManager.java From j360-dubbo-app-all with Apache License 2.0 | 6 votes |
/** * 异步队列写入count * 无需修改cache的值 * @param cacheSyncEvent */ public void writeStepCount(CacheSyncEvent cacheSyncEvent) { log.info("writeStepCount:{}", cacheSyncEvent); RAtomicLong rCacheLong = redissonClient.getAtomicLong(cacheSyncEvent.getCacheKey()); //获取步长是否满足补偿条件 if (checkStepCondition(cacheSyncEvent, rCacheLong)) { //封装 CacheModelEnum cacheModelEnum = CacheModelEnum.lookup(cacheSyncEvent.getIndex()); if (retryCountToCache(cacheSyncEvent.getId(), 0, cacheModelEnum, rCacheLong)) { //回写DB if (rCacheLong.get() > 0) { writeValue(rCacheLong.get(), cacheSyncEvent.getId(), cacheModelEnum); } } } rCacheLong.expire(AppConfig.COMMON_COUNT_CACHE_DAYS, TimeUnit.DAYS); }
Example #8
Source File: CacheSyncUtil.java From j360-dubbo-app-all with Apache License 2.0 | 5 votes |
/** * 用于重试级别的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 #9
Source File: RedissonAtomicLongTest.java From redisson with Apache License 2.0 | 5 votes |
@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 #10
Source File: DelayedTask.java From redisson with Apache License 2.0 | 5 votes |
@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; }
Example #11
Source File: ShopifyJacksonDecoder.java From shopify-api-java-wrapper with Apache License 2.0 | 5 votes |
@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 #12
Source File: RedissonAtomicLongTest.java From redisson with Apache License 2.0 | 5 votes |
@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 #13
Source File: RedissonInstrumentationTest.java From apm-agent-java with Apache License 2.0 | 5 votes |
@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 #14
Source File: RedissonAtomicLongTest.java From redisson with Apache License 2.0 | 5 votes |
@Test public void testCompareAndSet() { RAtomicLong al = redisson.getAtomicLong("test"); Assert.assertFalse(al.compareAndSet(-1, 2)); Assert.assertEquals(0, al.get()); Assert.assertTrue(al.compareAndSet(0, 2)); Assert.assertEquals(2, al.get()); }
Example #15
Source File: RedissonAtomicLongTest.java From redisson with Apache License 2.0 | 5 votes |
@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: CacheSyncUtil.java From j360-dubbo-app-all with Apache License 2.0 | 5 votes |
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 #17
Source File: CacheSyncUtil.java From j360-dubbo-app-all with Apache License 2.0 | 5 votes |
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 #18
Source File: RedissonTest.java From game-server with MIT License | 5 votes |
/** * 测试过期 * @author JiangZhiYong * @QQ 359135103 * 2017年9月30日 上午10:46:43 */ @Test public void testExipreAsync() { //long RAtomicLong atomicLong = RedissonManager.getRedissonClient().getAtomicLong("longtest"); atomicLong.addAndGet(100); atomicLong.expireAt(new Date().getTime()+100000); }
Example #19
Source File: CacheSyncManager.java From j360-dubbo-app-all with Apache License 2.0 | 5 votes |
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 #20
Source File: CacheSyncManager.java From j360-dubbo-app-all with Apache License 2.0 | 5 votes |
/** * 用于重试级别的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 #21
Source File: RedissionUtilsTest.java From Redis_Learning with Apache License 2.0 | 5 votes |
/** * 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 #22
Source File: CacheSyncManager.java From j360-dubbo-app-all with Apache License 2.0 | 5 votes |
/** * 写入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 #23
Source File: CacheSyncManager.java From j360-dubbo-app-all with Apache License 2.0 | 5 votes |
/** * 读取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 #24
Source File: TracingRedissonTest.java From java-redis-client with Apache License 2.0 | 5 votes |
@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 #25
Source File: IpRule.java From kk-anti-reptile with Apache License 2.0 | 5 votes |
@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 #26
Source File: RedisAtomicIdentityFactory.java From jstarcraft-core with Apache License 2.0 | 4 votes |
public RedisAtomicIdentityFactory(RAtomicLong redisson, IdentityDefinition definition, int partition, long step) { super(definition, partition, step); this.redisson = redisson; }
Example #27
Source File: RedissonAtomicLongTest.java From redisson with Apache License 2.0 | 4 votes |
@Test public void testIncrementAndGet() { RAtomicLong al = redisson.getAtomicLong("test"); Assert.assertEquals(1, al.incrementAndGet()); Assert.assertEquals(1, al.get()); }
Example #28
Source File: RedissonAtomicLongTest.java From redisson with Apache License 2.0 | 4 votes |
@Test public void testGetAndIncrement() { RAtomicLong al = redisson.getAtomicLong("test"); Assert.assertEquals(0, al.getAndIncrement()); Assert.assertEquals(1, al.get()); }
Example #29
Source File: RedissonAtomicLongTest.java From redisson with Apache License 2.0 | 4 votes |
@Test public void testGetZero() { RAtomicLong ad2 = redisson.getAtomicLong("test"); assertThat(ad2.get()).isZero(); }
Example #30
Source File: RedissonAtomicLongTest.java From redisson with Apache License 2.0 | 4 votes |
@Test public void testGetAndSet() { RAtomicLong al = redisson.getAtomicLong("test"); Assert.assertEquals(0, al.getAndSet(12)); }