【分布式】Redis分布式之AtomicLong
说明使用package com.demo.redis.string;import org.redisson.api.RAtomicLong;import org.redisson.api.RedissonClient;import org.springframework.util.Assert;import javax.annotation.Resource;/*** RedisAtomicLon
·
说明
原理同Jdk的AtomicLong,
配置文件参考:https://blog.csdn.net/qq_38428623/article/details/123217001?utm_source=app&app_version=5.1.1&code=app_1562916241&uLinkId=usr1mkqgl919blen
使用
package com.demo.redis.string;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RedissonClient;
import org.springframework.util.Assert;
import javax.annotation.Resource;
/**
* RedisAtomicLong
*
* @author 王思勤
*/
public class RedisAtomicLong {
@Resource
private RedissonClient redissonClient;
/**
* 获取 字符串 的 RAtomicLong
*
* @param name 名称
* @return 返回 值
*/
public RAtomicLong getAtomicLong(String name) {
RAtomicLong atomicLong = redissonClient.getAtomicLong(name);
Assert.notNull(atomicLong, "atomicLong is null");
return atomicLong;
}
/**
* 获取 RAtomicLong 的 值
*
* @param name 名称
* @return 返回 值
*/
public long get(String name){
return this.getAtomicLong(name).get();
}
/**
* 获取然后自减 1
*
* @param name 名称
* @return 返回 值
*/
public long getAndDecrement(String name){
return this.getAtomicLong(name).getAndDecrement();
}
/**
* 获取然后自增 1
*
* @param name 名称
* @return 返回 值
*/
public long getAndIncrement(String name){
return this.getAtomicLong(name).getAndIncrement();
}
/**
* 自减 1 然后获取
*
* @param name 名称
* @return 返回 值
*/
public long decrementAndGet(String name){
return this.getAtomicLong(name).decrementAndGet();
}
/**
* 自增 1 然后获取
*
* @param name 名称
* @return 返回 值
*/
public long incrementAndGet(String name){
return this.getAtomicLong(name).incrementAndGet();
}
/**
* 新增然后获取 RAtomicLong 的 值
*
* @param name 名称
* @return 返回 值
*/
public long addAndGet(String name, long delta){
return this.getAtomicLong(name).addAndGet(delta);
}
}
更多推荐
已为社区贡献3条内容
所有评论(0)