package com.hfw.basesystem.config;
import javax.annotation.Resource;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.data.redis.domain.geo.GeoReference;
import org.springframework.data.redis.domain.geo.GeoShape;
import org.springframework.scripting.support.StaticScriptSource;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* Redis工具类
* 官方命令文档:https://redis.io/commands/
* @author farkle
* @create 2020-04-25
*/
@Component
public class RedisUtil {
@Resource
private RedisTemplate redisTemplate;
//@Resource
//private StringRedisTemplate stringRedisTemplate;
/*************************string操作*****************************/
/*public void setStr(String key, String value){
stringRedisTemplate.opsForValue().set(key,value);
}
public String getStr(String key){
return stringRedisTemplate.opsForValue().get(key);
}*/
public T get(String key) {
return (T)redisTemplate.opsForValue().get(key);
}
/**
* 获取并设置过期时间
* @param key
* @param expire
* @param
* @return
*/
public T getEx(String key, long expire){
return (T)redisTemplate.opsForValue().getAndExpire(key, expire, TimeUnit.SECONDS);
}
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 设置值和超时事件
* @param key
* @param value
* @param expire
*/
public void setEx(String key, Object value, long expire) {
redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
}
/**
* key不存在时设置
* @param key
* @param value
* @return
*/
public Boolean setNx(String key, Object value){
return redisTemplate.opsForValue().setIfAbsent(key,value);
}
/**
* key存在时设置
* @param key
* @param value
* @return
*/
public Boolean setXx(String key, Object value){
return redisTemplate.opsForValue().setIfPresent(key,value);
}
public Boolean setNxEx(String key, Object value, long timeout){
return redisTemplate.opsForValue().setIfAbsent(key,value, timeout, TimeUnit.SECONDS);
}
/*public Boolean setNxEx(String key, Object value, long expire) {
return redisTemplate.execute((RedisConnection redisConnection) ->
redisConnection.set(key.getBytes(), value.getBytes(), Expiration.from(expire, TimeUnit.SECONDS), RedisStringCommands.SetOption.SET_IF_ABSENT)
);
}*/
public T getSet(String key, Object value){
return (T)redisTemplate.opsForValue().getAndSet(key, value);
}
public Boolean expire(String key, long expire) {
return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
public Boolean exists(String key){
return redisTemplate.hasKey(key);
}
public Boolean del(String key) {
return redisTemplate.delete(key);
}
public Long del(Collection keys) {
return redisTemplate.delete(keys);
}
public Long incr(String key) {
return redisTemplate.opsForValue().increment(key);
}
public Long decr(String key){
return redisTemplate.opsForValue().decrement(key);
}
/**
*************************list操作*****************************
* 有序,可重复
* 1、作为栈或队列使用
* 2、可用于各种列表,比如用户列表、商品列表、评论列表等。
*/
/**
* 从列表头部插入
* @param key
* @param value
* @return 返回列表的长度
*/
public Long lPush(String key, Object value){
return redisTemplate.opsForList().leftPush(key, value);
}
public T lPop(String key){
return (T) redisTemplate.opsForList().leftPop(key);
}
public Long rPush(String key, Object value){
return redisTemplate.opsForList().rightPush(key, value);
}
public T rPop(String key){
return (T) redisTemplate.opsForList().rightPop(key);
}
/**
* 当列表为空时阻塞
* @param key
* @param timeout 设置最大阻塞时间
* @param
* @return
*/
public T bLPop(String key, long timeout){
return (T) redisTemplate.opsForList().leftPop(key, timeout, TimeUnit.SECONDS);
}
public T bRPop(String key, long timeout){
return (T) redisTemplate.opsForList().rightPop(key, timeout, TimeUnit.SECONDS);
}
public Long lLen(String key){
return redisTemplate.opsForList().size(key);
}
/**
* 获取指定索引的元素
* @param key
* @param index 索引,从0开始
* @param
* @return
*/
public T lIndex(String key, long index){
return (T) redisTemplate.opsForList().index(key, index);
}
public List lRange(String key, long start, long end){
return (List) redisTemplate.opsForList().range(key, start, end);
}
/**
* 从列表中删除元素
* @param key
* @param value 要删除的元素
* @return
*/
public Long lRem(String key, Object value){
/**
* count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT
* count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值
* count = 0 : 移除表中所有与 VALUE 相等的值
*/
return redisTemplate.opsForList().remove(key,0, value);
}
/**
* 设置列表中指定索引的值
* @param key
* @param index 索引
* @param value 值
*/
public void lSet(String key, long index, Object value){
redisTemplate.opsForList().set(key,index,value);
}
/**
* 对列表进行修剪,只保留start到end区间
* @param key
* @param start
* @param end
*/
public void lTrim(String key, long start, long end){
redisTemplate.opsForList().trim(key,start,end);
}
public T rPopLPush(String sourceKey, String destinationKey){
return (T) redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey);
}
//会阻塞
public T bRPopLPush(String sourceKey, String destinationKey, long timeout){
return (T) redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey, timeout,TimeUnit.SECONDS);
}
/**
* 将value插入到列表,且位于值pivot之前
* @param key
* @param pivot
* @param value
* @return
*/
public Long lInsert(String key, Object pivot, Object value){
return redisTemplate.opsForList().leftPush(key,pivot,value);
}
/**
**************************set操作*****************************
* 无序,不重复
* 适用于不能重复的且不需要顺序的数据结构, 比如:关注的用户,还可以通过spop进行随机抽奖
*/
public Long sAdd(String key, Object ... values){
return redisTemplate.opsForSet().add(key, values);
}
public Long sRem(String key, Object ... values){
return redisTemplate.opsForSet().remove(key, values);
}
public Set sMembers(String key){
return (Set) redisTemplate.opsForSet().members(key);
}
/**
* 随机弹出一个元素,删除
* @param key
* @param
* @return
*/
public T sPop(String key){
return (T) redisTemplate.opsForSet().pop(key);
}
/**
* 随机获取一个元素,不删除
* @param key
* @param
* @return
*/
public T sRandMember(String key){
return (T) redisTemplate.opsForSet().randomMember(key);
}
/**
* 获取数量
* @param key
* @return
*/
public Long sCard(String key){
return redisTemplate.opsForSet().size(key);
}
/**
* 是否在集合内
* @param key
* @param member
* @return
*/
public Boolean sIsMember(String key, Object member){
return redisTemplate.opsForSet().isMember(key, member);
}
/**
* 求两个集合的交集
* @param key
* @param anotherKey
* @param
* @return
*/
public Set sInter(String key, String anotherKey){
return (Set) redisTemplate.opsForSet().intersect(key, anotherKey);
}
/**
* 求两个集合的差集
* @param key
* @param anotherKey
* @param
* @return
*/
public Set sDiff(String key, String anotherKey){
return (Set) redisTemplate.opsForSet().difference(key, anotherKey);
}
/**
* 求两个集合的并集
* @param key
* @param anotherKey
* @param
* @return
*/
public Set sUnion(String key, String anotherKey){
return (Set) redisTemplate.opsForSet().union(key, anotherKey);
}
/**
**************************zset操作*****************************
* 有序,不重复,且每一个元素关联一个score
* 由于可以按照分值排序,所以适用于各种排行榜。比如:点击排行榜、销量排行榜、关注排行榜等
*/
public Boolean zAdd(String key, Object value, double score){
return redisTemplate.opsForZSet().add(key,value,score);
}
public Long zRem(String key, Object... values){
return redisTemplate.opsForZSet().remove(key, values);
}
/**
* 获取集合数量
* @param key
* @return
*/
public Long zCard(String key){
return redisTemplate.opsForZSet().zCard(key);
}
/**
* 返回集合中score值在[min,max]区间
* 的元素数量
* @param key
* @param min
* @param max
* @return
*/
public Long zCount(String key, double min, double max){
return redisTemplate.opsForZSet().count(key, min, max);
}
/**
* 集合的元素分值增加
* @param key
* @param value
* @param delta
* @return
*/
public Double zIncrBy(String key, Object value, double delta){
return redisTemplate.opsForZSet().incrementScore(key,value,delta);
}
/**
* 获取指定元素的分值
* @param key
* @param value
* @return
*/
public Double zScore(String key, Object value){
return redisTemplate.opsForZSet().score(key,value);
}
/**
* 获取指定元素的分值排行榜(升序,分值从小到大)
* @param key
* @param value
* @return
*/
public Long zrank(String key, Object value){
return redisTemplate.opsForZSet().rank(key, value);
}
/**
* 获取指定元素的分值排行榜(降序,分值从大到小)
* @param key
* @param value
* @return
*/
public Long zRevRank(String key, Object value){
return redisTemplate.opsForZSet().reverseRank(key,value);
}
/**
* 按索引获取集合中的数据(升序)
* @param key
* @param start
* @param end
* @param
* @return
*/
public Set zRange(String key, long start, long end){
return (Set) redisTemplate.opsForZSet().range(key, start, end);
}
public Set zRevRange(String key, long start, long end){
return (Set) redisTemplate.opsForZSet().reverseRange(key, start, end);
}
/**
* 按索引获取集合中的数据及分数(升序)
* @param key
* @param start
* @param end
* @return
*/
public Set> zRangeWithScores(String key, long start, long end){
return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
}
/**
* 根据分值获取集合中的数据(升序)
* @param key
* @param min
* @param max
* @param
* @return
*/
public Set zRangeByScore(String key, double min, double max){
return (Set) redisTemplate.opsForZSet().rangeByScore(key, min, max);
}
/**
*************************hash操作*****************************
* 应用场景: 对象的存储 ,表数据的映射
*/
/**
* 设置对象的 一个字段
* @param key 对象主键
* @param hashKey 对象字段
* @param hashValue 对象主键值
*/
public void hSet(String key, String hashKey, Object hashValue){
redisTemplate.opsForHash().put(key, hashKey, hashValue);
}
public void hMSet(String key, Map data){
redisTemplate.opsForHash().putAll(key,data);
}
/**
* hashKey不存在时设置 hashKey的值
* @param key
* @param hashKey
* @param hashValue
* @return
*/
public Boolean hSetNX(String key, String hashKey, Object hashValue){
return redisTemplate.opsForHash().putIfAbsent(key, hashKey,hashValue);
}
/**
* 查看对象的 某个字段是否存在
* @param key 对象主键
* @param hashKey 对象字段
* @return
*/
public Boolean hExists(String key, String hashKey){
return redisTemplate.opsForHash().hasKey(key,hashKey);
}
/**
* 获取对象的某个字段值
* @param key 对象主键
* @param hashKey 对象字段
* @param
* @return
*/
public T hGet(String key, String hashKey){
return (T) redisTemplate.opsForHash().get(key, hashKey);
}
public List hMGet(String key, Collection hashKeys){
return redisTemplate.opsForHash().multiGet(key, hashKeys);
}
/**
* 删除对象的某个字段
* @param key 对象主键
* @param value 对象字段
*/
public Long hDel(String key, Object... value){
return redisTemplate.opsForHash().delete(key, value);
}
/**
* 获取对象的字段个数
* @param key
* @return
*/
public Long hLen(String key){
return redisTemplate.opsForHash().size(key);
}
/**
**************************bitmap操作*****************************
* 应用:用户每月签到,用户id为key , 日期作为偏移量 1表示签到
*/
public Boolean setBit(String key, long offset, boolean value){
return redisTemplate.opsForValue().setBit(key,offset,value);
}
/*public void setBit(String key, long offset, boolean value) {
stringRedisTemplate.execute((RedisConnection redisConnection) -> {
try {
return redisConnection.setBit((key).getBytes("UTF-8"), offset, value);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
});
}*/
public Boolean getBit(String key, long offset) {
return redisTemplate.opsForValue().getBit(key,offset);
}
/**
**************************GEO地理位置操作*****************************
* 应用:附件的人,计算距离
*/
/**
* 添加坐标点
* @param key
* @param point
* @param value
* @return
*/
public Long geoAdd(String key, Point point, Object value){
return redisTemplate.boundGeoOps(key).add(point, value);
}
/**
* 获取geo成员的坐标
* @param key
* @param members
* @return
*/
public List geoPos(String key, Object... members){
return redisTemplate.boundGeoOps(key).position(members);
}
/**
* 计算两个点的距离
* @param key
* @param o1 点1的值
* @param o2 点2的值
* @return
*/
public Distance geoDist(String key,Object o1,Object o2){
return redisTemplate.boundGeoOps(key).distance(o1,o2);
}
/**
* 中心点搜索
* @param key
* @param point 中心点
* @param distance 半径
* @param count 数量
* @return
*/
public GeoResults> geoRadius(String key, Point point, Distance distance, long count){
Circle circle = new Circle(point,distance);
RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().sortAscending().limit(count);
return redisTemplate.boundGeoOps(key).radius(circle, args);
}
/**
* 成员做中心点搜索
* @param key
* @param member 成员
* @param distance 半径
* @param count 数量
* @return
*/
public GeoResults> geoRadiusByMember(String key, Object member,Distance distance, long count){
RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().sortAscending().limit(count);
return redisTemplate.boundGeoOps(key).radius(member, distance, args);
}
/**
* 根据值删除坐标点
* @param key
* @param value
* @return
*/
public Long geoRemove(String key, Object value){
return redisTemplate.boundGeoOps(key).remove(value);
//return this.zrem(key,value); //GEO数据是保存在zset中的,所以可以通过zset的命令删除
}
/**
* 中心点搜索并存储成zset
* @param key
* @param zsetKey 存储的zset_key
* @param point 中心点
* @param distance 半径
* @param count 数量
* @return
*/
public Long geoSearchStore(String key, String zsetKey, Point point, Distance distance, long count){
RedisGeoCommands.GeoSearchStoreCommandArgs args = RedisGeoCommands.GeoSearchStoreCommandArgs.newGeoSearchStoreArgs().storeDistance().sortAscending().limit(count);
return redisTemplate.boundGeoOps(key).searchAndStore(zsetKey, GeoReference.fromCoordinate(point), GeoShape.byRadius(distance), args);
}
/*************************布隆过滤器操作*****************************/
/**
* 布隆过滤器-添加
* 可用于把消息置为已读
*
* @param key
* @param value
* @return
*/
public Long bfAdd(String key, String value) {
DefaultRedisScript script = new DefaultRedisScript();
script.setResultType(Long.class);
script.setScriptSource(new StaticScriptSource("return redis.call('bf.add', KEYS[1], KEYS[2])"));
return redisTemplate.execute(script, Arrays.asList(key, value));
}
/**
* 布隆过滤器-判断
* 可用于获取消息的状态
* @param key
* @param value
* @return
*/
public Boolean bfExists(String key, String value) {
DefaultRedisScript script = new DefaultRedisScript();
script.setResultType(Long.class);
script.setScriptSource(new StaticScriptSource("return redis.call('bf.exists', KEYS[1], KEYS[2])"));
Long res = redisTemplate.execute(script, Arrays.asList(key, value));
return 1 == res;
}
/**
* 布隆过滤器-设置存储空间和错误率
* error_rate, initial_size,错误率越低,需要的空间越大,error_rate表示预计错误率,initial_size参数表示预计放入的元素数量,当实际数量超过这个值时,误判率会上升,所以需要提前设置一个较大的数值来避免超出
* @param key
* @param count 空间大小
* @return
*/
public Boolean bfReserve(String key, Long count) {
String cnt = count > 100 ? count + "" : "100";
String errorRate = new BigDecimal(cnt).pide(new BigDecimal(10000)).toString();
DefaultRedisScript script = new DefaultRedisScript();
script.setResultType(String.class);
script.setScriptSource(new StaticScriptSource("return redis.call('bf.reserve', KEYS[1], KEYS[2], KEYS[3])"));
String res = redisTemplate.execute(script, Arrays.asList(key, errorRate, cnt));
return "OK".equals(res);
}
}
redis数据结构设计
sysuser_token:token string类型存储用户登录信息
sysuser_token_list:userId list结构存储用户的所有token
1. 用户登录
生成token, 并存储 sysuser_token:token 用户登录信息
向sysuser_token_list:userId入栈生成的token, 如果超出同一用户最大登录限制, 则出栈一条token
出栈的token判断是否有效, 有效则删除对应的sysuser_token:token(被挤下线)
2. 根据token校验用户信息
直接获取sysuser_token:token存储的用户信息, 存在且充值超时时间
3. 用户登出
删除sysuser_token_list:userId
删除sysuser_token_list:userId 中存储的所有token
4. 禁用用户
-->用户登出. 重新登录时判断是否被禁用
-->或者修改sysuser_token_list:userId 中存储的所有token对应用户的禁用字段标识
代码实现
package com.hfw.basesystem.service.impl;
import com.hfw.basesystem.config.RedisUtil;
import com.hfw.basesystem.service.RedisAuthService;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* redis认证服务
* @author farkle
* @date 2023-02-02
*/
//@Service("redisAuthService")
public class RedisAuthServiceImpl implements RedisAuthService {
/**
* 限制同一账号最大登录人数
*/
private int max_login = 2;
/**
* token前缀
*/
private String token_key_prefix;
/**
* 存储所有登录的token前缀
*/
private String user_token_list_prefix;
/**
* 失效时间
* 30分钟(单位秒)
*/
private long expire = 30*60;
private RedisUtil redisUtil;
public void setMax_login(int max_login) {
this.max_login = max_login;
}
public void setToken_key_prefix(String token_key_prefix) {
this.token_key_prefix = token_key_prefix;
}
public void setUser_token_list_prefix(String user_token_list_prefix) {
this.user_token_list_prefix = user_token_list_prefix;
}
public void setExpire(long expire) {
this.expire = expire;
}
public void setRedisUtil(RedisUtil redisUtil) {
this.redisUtil = redisUtil;
}
public String genToken(){
return UUID.randomUUID().toString().replaceAll("-","");
}
//用户登录时存储登录信息
@Override
public String store(Long userId, String token, Object obj){
String userTokenListKey = user_token_list_prefix + userId;
Long userTokenSize = redisUtil.lPush(userTokenListKey, token);
redisUtil.setEx(token_key_prefix+ token, obj, expire);
if(userTokenSize > max_login){
String rpop = redisUtil.rPop(userTokenListKey);
if(this.exists(rpop)){
this.logout(rpop);
return rpop;
}
}
return null;
}
//更新登录信息
@Override
public int update(Long userId, Object obj){
List tokenList = redisUtil.lRange(user_token_list_prefix + userId, 0, max_login);
if(CollectionUtils.isEmpty(tokenList)){
return 0;
}
int cnt = 0;
for(String token : tokenList){
if( redisUtil.setXx(token_key_prefix+token, obj) ){
cnt++;
}
}
return cnt;
}
/**
* 判断token是否存在
* @param token
* @return
*/
@Override
public Boolean exists(String token){
return redisUtil.exists(token_key_prefix+token);
}
/**
* 校验token
* @param token
* @return
*/
@Override
public T validToken(String token){
return redisUtil.getEx(token_key_prefix+ token, expire);
}
@Override
public List getValidToken(Long userId){
List tokenList = redisUtil.lRange(user_token_list_prefix + userId, 0, max_login);
List validList = tokenList.stream().filter(token -> this.exists(token)).collect(Collectors.toList());
return validList;
}
/**
* 用户登出
* @param userId
*/
@Override
public Boolean logout(Long userId){
List tokenList = redisUtil.lRange(user_token_list_prefix + userId, 0, max_login);
if(CollectionUtils.isEmpty(tokenList)){
return false;
}
List keys = tokenList.stream().map(token -> token_key_prefix + token).collect(Collectors.toList());
Long dels = redisUtil.del(keys);
redisUtil.del(user_token_list_prefix+userId);
return dels>0;
}
/**
* 当前token登出
* @param token
*/
@Override
public Boolean logout(String token){
return redisUtil.del(token_key_prefix+ token);
}
/**
* 禁用用户
* @param userId
* @return
*/
@Override
public Boolean disableUser(Long userId){
return this.logout(userId);
}
}
实现思路
实现代码
package com.hfw.basesystem.service.impl;
import com.hfw.basesystem.config.RedisUtil;
import com.hfw.basesystem.service.RedisGeoService;
import com.hfw.common.entity.PageResult;
import com.hfw.common.util.NumberUtil;
import javax.annotation.Resource;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.domain.geo.Metrics;
import org.springframework.stereotype.Service;
import java.util.Set;
/**
* @author farkle
* @date 2022-11-27
*/
@Service("redisGeoService")
public class RedisGeoServiceImpl implements RedisGeoService {
/**
* 附近的点最多获取多少个
*/
private static final Integer max_count = 10000;
private static final Integer min_count = 100;
/**
* 附近的点最大半径距离,单位米
*/
private static final Integer max_distance = 1000*1000;
/**
* 附近的点实现zset key
*/
private static final String near_zset_key = "near_zset";
public static final String near_geo_key = "near_geo";
@Resource
private RedisUtil redisUtil;
@Override
public Set> near(double lng, double lat, Integer pageNumber, Integer pageSize){
PageResult page = new PageResult(pageNumber, pageSize);
Integer requestCount = page.getEnd()+1;
if(pageNumber>1 && requestCount>max_count){
return null;
}
Integer searchCount = min_count;
int i = 0;
while (searchCountmax_count){
searchCount = min_count;
}
//System.out.println(searchCount+":end");
Long size = redisUtil.zCard(near_zset_key);
if(searchCount>size){
Integer distance = 10*1000;
Long cnt = redisUtil.geoSearchStore(near_geo_key, near_zset_key, new Point(lng, lat), new Distance(distance, Metrics.METERS), searchCount);
//System.out.println(distance);
while (cnt < requestCount && distance
设计思路
推荐用spring-data-redis实现
//实例对象
@RedisHash("persons")
@Data
public class Person {
@Id
private Long id;
@Indexed
private String name;
private String address;
}
//查询服务, spring-data-redis自动扫描并生成实现bean
public interface PersonRepository extends CrudRepository {
List findByName(String name);
}
//操作实例
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
private PersonRepository personRepository;
@RequestMapping("/add")
@ResponseBody
public String add(){
Person p = new Person();
p.setId(1L);
p.setName("张三");
p.setAddress("北京");
personRepository.save(p);
p = new Person();
p.setId(2L);
p.setName("李四");
p.setAddress("昆明");
personRepository.save(p);
return "ok";
}
@RequestMapping("/query")
@ResponseBody
public List query(String name){
List list = personRepository.findByName(name);
return list;
}
}
观察生成的数据结构
依赖
org.redisson
redisson
2.7.0
配置
public class RedissonManager {
private static Config config = new Config();
//声明redisso对象
private static Redisson redisson = null;
//实例化redisson
static{
config.useClusterServers()
// 集群状态扫描间隔时间,单位是毫秒
.setScanInterval(2000)
//cluster方式至少6个节点(3主3从,3主做sharding,3从用来保证主宕机后可以高可用)
.addNodeAddress("redis://127.0.0.1:6379" )
.addNodeAddress("redis://127.0.0.1:6380")
;
//得到redisson对象
redisson = (Redisson) Redisson.create(config); } public static Redisson getRedisson(){ return redisson; }
}
锁的获取和释放
public class DistributedRedisLock {
//从配置类中获取redisson对象
private static Redisson redisson = RedissonManager.getRedisson();
private static final String LOCK_TITLE = "redisLock_";
//加锁
public static boolean acquire(String lockName){
//声明key对象
String key = LOCK_TITLE + lockName;
//获取锁对象
RLock mylock = redisson.getLock(key);
//加锁,并且设置锁过期时间3秒,防止死锁的产生 uuid+threadId
mylock.lock(2,3,TimeUtil.SECOND);
//加锁成功
return true;
}
//锁的释放
public static void release(String lockName){
//必须是和加锁时的同一个key
String key = LOCK_TITLE + lockName;
//获取所对象
RLock mylock = redisson.getLock(key);
//释放锁(解锁)
mylock.unlock();
}
}
https://gitee.com/hongy123/znew-admin
一个简单通用的springboot+vue3后台管理系统
页面更新:2024-03-27
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号