1、引入jar
<!--redisson-->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.13.4</version>
</dependency>
<!--alibabaJson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!--commons-pool-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2、编写application.yml当中redis的配置
spring:
redis:
open: true
database: 0
host: 127.0.0.1 ##这里是我自己的服务器
port: 6379
password: 123456
lettuce:
pool:
max-idle: 8
min-idle: 0
max-active: 8
max-wait: -1ms
shutdown-timeout: 100ms
3、编写RedissonConfig
@Configuration
public class RedissonConfig {
@Autowired
private RedissionService redissionService;
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private String port;
@Value("${spring.redis.password}")
private String password;
@Bean
public RedissonClient getRedisson() {
Config config = new Config();
config.useSingleServer().setAddress("redis://" + host + ":" + port).setPassword(password);
//解决乱码问题 不使用的话 那么存入value的时候 前面一段value会存在乱码
config.setCodec(new StringCodec());
RedissonClient redissonClient = Redisson.create(config);
try {
redissionService.setRedissonClient(redissonClient);
} catch (Exception e) {
e.printStackTrace();
}
return Redisson.create(config);
}
}
4、编写redisson接口
public interface RedissionService {
RedissonClient getRedissonClient();
void setRedissonClient(RedissonClient redissonClient);
void lock(String lockName);
void lock(String lockName, long lockTime);
boolean tryLock(String lockName, long lockTime);
boolean tryLock(String lockName, long leaseTime, long waitTime);
void unlock(String lockName);
boolean isLock(String lockName);
boolean isHeldByCurrentThread(String lockName);
String getStr(String key);
void setStr(String key, String value);
<T> T get(String key);
<T> T get(String key, Class<T> t);
<T> List<T> getList(String key, Class<T> t);
void set(String key, Object value);
void set(String key, Object value, long time);
void expire(String key, Duration expireTimeDuration);
long incr(String key);
long incrBy(String key, long value);
long decr(String key);
boolean delete(String key);
Integer putSortedSet(String key, Double score, Object value, Duration expireTime);
Integer putSortedSet(String key, Double score, Object value);
Integer addScore(String key, Double addScore, Object value, Duration expireTime);
List<Object> rankRange(String key, Integer top);
<T> List<T> rankRange(String key, Long minScore, Long maxScore, Integer maxCount, Class<T> t);
RScoredSortedSet<Object> rankRangeAll(String key);
Integer getRank(String key, Object value);
void lPush(String key, String... values);
boolean zadd(String key, Long score, String userId);
Integer revrank(String key, String value);
Collection<String> zrevrange(String key, int start, int end);
}
5、redisson接口 实现类
@Service
public class RedissonServiceImpl implements RedissionService {
private static final Logger log = LoggerFactory.getLogger(RedissonServiceImpl.class);
protected RedissonClient redissonClient;
public RedissonServiceImpl() {
}
public RedissonClient getRedissonClient() {
return this.redissonClient;
}
public void setRedissonClient(RedissonClient redissonClient) {
this.redissonClient = redissonClient;
}
public void lock(String lockName) {
RLock rLock = this.redissonClient.getLock(lockName);
rLock.lock();
}
public void lock(String lockName, long lockTime) {
RLock rLock = this.redissonClient.getLock(lockName);
rLock.lock(lockTime, TimeUnit.MILLISECONDS);
}
public boolean tryLock(String lockName, long lockTime) {
try {
RLock rLock = this.redissonClient.getLock(lockName);
return rLock.tryLock(lockTime, TimeUnit.MILLISECONDS);
} catch (Exception var5) {
var5.printStackTrace();
return false;
}
}
public boolean tryLock(String lockName, long leaseTime, long waitTime) {
try {
RLock rLock = this.redissonClient.getLock(lockName);
return rLock.tryLock(waitTime, leaseTime, TimeUnit.MILLISECONDS);
} catch (Exception var7) {
var7.printStackTrace();
return false;
}
}
public void unlock(String lockName) {
this.redissonClient.getLock(lockName).unlock();
}
public boolean isLock(String lockName) {
return this.redissonClient.getLock(lockName).isLocked();
}
public boolean isHeldByCurrentThread(String lockName) {
return this.redissonClient.getLock(lockName).isHeldByCurrentThread();
}
public String getStr(String key) {
RBucket<String> bucket = this.redissonClient.getBucket(key);
return (String) bucket.get();
}
public void setStr(String key, String value) {
RBucket bucket = this.redissonClient.getBucket(key);
bucket.set(value);
}
public <T> T get(String key) {
RBucket<T> bucket = this.redissonClient.getBucket(key);
return bucket.get();
}
public <T> T get(String key, Class<T> t) {
RBucket<String> bucket = this.redissonClient.getBucket(key);
return JSON.parseObject((String) bucket.get(), t);
}
public <T> List<T> getList(String key, Class<T> t) {
RBucket<String> bucket = this.redissonClient.getBucket(key);
return JSON.parseArray((String) bucket.get(), t);
}
public void set(String key, Object value) {
RBucket bucket = this.redissonClient.getBucket(key);
bucket.set(JSON.toJSONString(value));
}
public void set(String key, Object value, long time) {
RBucket bucket = this.redissonClient.getBucket(key);
bucket.set(JSON.toJSONString(value), time, TimeUnit.MILLISECONDS);
}
public void expire(String key, Duration expireTimeDuration) {
RBucket bucket = this.redissonClient.getBucket(key);
bucket.expire(expireTimeDuration.getSeconds(), TimeUnit.SECONDS);
}
public long incr(String key) {
RAtomicLong atomicLong = this.redissonClient.getAtomicLong(key);
return atomicLong.incrementAndGet();
}
public long incrBy(String key, long value) {
RAtomicLong atomicLong = this.redissonClient.getAtomicLong(key);
return atomicLong.addAndGet(value);
}
public long decr(String key) {
RAtomicLong atomicLong = this.redissonClient.getAtomicLong(key);
return atomicLong.decrementAndGet();
}
public boolean delete(String key) {
RBucket bucket = this.redissonClient.getBucket(key);
return bucket.delete();
}
public Integer putSortedSet(String key, Double score, Object value, Duration expireTime) {
RScoredSortedSet<Object> scoredSortedSet = this.redissonClient.getScoredSortedSet(key);
Integer rank = scoredSortedSet.addAndGetRevRank(score, value);
scoredSortedSet.expire(expireTime.getSeconds(), TimeUnit.SECONDS);
return rank;
}
public Integer putSortedSet(String key, Double score, Object value) {
RScoredSortedSet<Object> scoredSortedSet = this.redissonClient.getScoredSortedSet(key);
Integer rank = scoredSortedSet.addAndGetRevRank(score, value);
return rank + 1;
}
public List<Object> rankRange(String key, Integer top) {
RScoredSortedSet<Object> scoredSortedSet = this.redissonClient.getScoredSortedSet(key);
LinkedHashSet<Object> linkedHashSet = (LinkedHashSet) scoredSortedSet.valueRangeReversed(0.0D, false, scoredSortedSet.lastScore(), true, 0, top);
return new ArrayList(linkedHashSet);
}
public RScoredSortedSet<Object> rankRangeAll(String key) {
RScoredSortedSet<Object> scoredSortedSet = this.redissonClient.getScoredSortedSet(key);
return scoredSortedSet;
}
public <T> List<T> rankRange(String key, Long minScore, Long maxScore, Integer maxCount, Class<T> t) {
RScoredSortedSet<T> scoredSortedSet = this.redissonClient.getScoredSortedSet(key);
ArrayList<T> value = (ArrayList) scoredSortedSet.valueRange(Double.valueOf((double) minScore), true, (double) maxScore, false, 0, maxCount);
return value;
}
public Integer getRank(String key, Object value) {
RScoredSortedSet<Object> scoredSortedSet = this.redissonClient.getScoredSortedSet(key);
return scoredSortedSet.revRank(value) + 1;
}
public Integer addScore(String key, Double addScore, Object value, Duration expireTime) {
RScoredSortedSet<Object> scoredSortedSet = this.redissonClient.getScoredSortedSet(key);
Integer rank = scoredSortedSet.addScoreAndGetRank(value, addScore);
scoredSortedSet.expire(expireTime.getSeconds(), TimeUnit.SECONDS);
return rank;
}
public void lPush(String key, String... values) {
}
public boolean zadd(String key, Long score, String value) {
RScoredSortedSet rScoredSortedSet = this.redissonClient.getScoredSortedSet(key);
return rScoredSortedSet.add((double) score, value);
}
public Integer revrank(String key, String value) {
RScoredSortedSet rScoredSortedSet = this.redissonClient.getScoredSortedSet(key);
return rScoredSortedSet.revRank(value);
}
public Collection<String> zrevrange(String key, int start, int end) {
RScoredSortedSet rScoredSortedSet = this.redissonClient.getScoredSortedSet(key);
return rScoredSortedSet.valueRangeReversed(start, end);
}
}