This commit is contained in:
haoxr 2021-03-07 23:58:08 +08:00
commit c8ff953c1f
22 changed files with 1167 additions and 14 deletions

View File

@ -23,6 +23,17 @@
<artifactId>feign-okhttp</artifactId>
</dependency>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>ums-api</artifactId>
<version>${youlai.version}</version>
</dependency>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>pms-api</artifactId>
<version>${youlai.version}</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>

View File

@ -1,9 +1,11 @@
package com.youlai.mall.sms.pojo.domain;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* sms_seckill_session
@ -46,5 +48,8 @@ public class SmsSeckillSession implements Serializable {
*/
private Date gmtModified;
@TableField(exist = false)
private List<SmsSeckillSkuRelation> relations;
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,71 @@
package com.youlai.mall.sms.pojo.to;
import com.youlai.mall.pms.pojo.dto.SkuDTO;
import lombok.Data;
import lombok.ToString;
/**
* @author huawei
* @desc 秒杀商品Redis存储 TO
* @email huawei_code@163.com
* @date 2021/3/7
*/
@Data
@ToString
public class SeckillSkuRedisTO {
/**
* id
*/
private Long id;
/**
* 活动场次id
*/
private Long sessionId;
/**
* 商品id
*/
private Long skuId;
/**
* 秒杀随机码
*/
private String randomCode;
/**
* 秒杀价格
*/
private Long seckillPrice;
/**
* 秒杀总量
*/
private Integer seckillCount;
/**
* 每人限购数量
*/
private Integer seckillLimit;
/**
* 排序
*/
private Integer seckillSort;
/**
* 秒杀开始时间
*/
private Long startTime;
/**
* 秒杀结束时间
*/
private Long endTime;
/**
* 秒杀商品详情
*/
private SkuDTO skuInfo;
}

View File

@ -0,0 +1,73 @@
package com.youlai.mall.sms.pojo.vo;
import com.youlai.mall.pms.pojo.dto.SkuDTO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author huawei
* @desc 秒杀商品模型
* @email huawei_code@163.com
* @date 2021/3/7
*/
@ApiModel(value = "秒杀商品模型",description = "秒杀商品模型")
@Data
public class SmsSeckillSkuVO {
/**
* id
*/
@ApiModelProperty
private Long id;
/**
* 活动场次id
*/
private Long sessionId;
/**
* 商品id
*/
private Long skuId;
/**
* 秒杀随机码
*/
private String randomCode;
/**
* 秒杀价格
*/
private Long seckillPrice;
/**
* 秒杀总量
*/
private Integer seckillCount;
/**
* 每人限购数量
*/
private Integer seckillLimit;
/**
* 排序
*/
private Integer seckillSort;
/**
* 秒杀开始时间
*/
private Long startTime;
/**
* 秒杀结束时间
*/
private Long endTime;
/**
* 秒杀商品详情
*/
private SkuDTO skuInfo;
}

View File

@ -25,6 +25,18 @@
<version>${youlai.version}</version>
</dependency>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>oms-api</artifactId>
<version>${youlai.version}</version>
</dependency>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>pms-api</artifactId>
<version>${youlai.version}</version>
</dependency>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>common-mybatis</artifactId>
@ -70,6 +82,7 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,24 @@
package com.youlai.mall;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* 优惠营销系统
* 秒杀功能开发
* 管理员端
* 1管理员端开发秒杀活动管理界面创建秒杀活动场次建立秒杀活动场次与商品关联
* 2秒杀预热采用 异步+定时 将秒杀数据提前同步到redis中
*
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SmsApplication {
public static void main(String[] args) {
SpringApplication.run(SmsApplication.class);
}
}

View File

@ -1,13 +0,0 @@
package com.youlai.mall.sms;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class SmsApplication {
public static void main(String[] args) {
SpringApplication.run(SmsApplication.class);
}
}

View File

@ -0,0 +1,17 @@
package com.youlai.mall.sms.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @author huawei
* @desc 异步+定时 配置
* @email huawei_code@163.com
* @date 2021/3/5
*/
@EnableScheduling
@EnableAsync
@Configuration
public class ScheduledConfig {
}

View File

@ -0,0 +1,35 @@
package com.youlai.mall.sms.controller.app;
import com.youlai.common.result.Result;
import com.youlai.mall.sms.pojo.vo.SmsSeckillSkuVO;
import com.youlai.mall.sms.service.SeckillService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author huawei
* @desc 秒杀活动管理
* @email huawei_code@163.com
* @date 2021/3/7
*/
@Api(tags = "【移动端】秒杀活动管理")
@RestController
@RequestMapping("/api.app/v1/seckill")
@Slf4j
public class SeckillController {
@Autowired
private SeckillService seckillService;
@GetMapping
public Result getCurrentSeckillSession() {
List<SmsSeckillSkuVO> currentSeckills = seckillService.getCurrentSeckillSession();
return Result.success();
}
}

View File

@ -0,0 +1,46 @@
package com.youlai.mall.sms.scheduled;
import com.youlai.mall.sms.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* @author huawei
* @desc 秒杀商品的定时上架每天晚上3点上架最近三天需要秒杀的商品
* @email huawei_code@163.com·
* @date 2021/3/5
*/
@Component
@Slf4j
public class SeckillSkuScheduled {
private static final String SECKILL_SKU_LATEST_3_DAY= "seckillSkuLatest3Days";
@Autowired
private SeckillService seckillService;
@Autowired
private RedissonClient redissonClient;
@Scheduled(cron = "0 * * * * ?")
public void updateSeckillSkuLatest3Days() {
log.info("上架秒杀最近3天商品信息");
// 1重复上架无需处理
// 使用分布式锁在分布式场景下只允许一个服务启动上架流程
RLock lock = redissonClient.getLock(SECKILL_SKU_LATEST_3_DAY);
lock.lock(10, TimeUnit.SECONDS);
try {
seckillService.updateSeckillSkuLatest3Days();
} finally {
lock.unlock();
}
}
}

View File

@ -0,0 +1,23 @@
package com.youlai.mall.sms.service;
import com.youlai.mall.sms.pojo.vo.SmsSeckillSkuVO;
import java.util.List;
/**
* @author huawei
* @desc 秒杀模块业务接口
* @email huawei_code@163.com
* @date 2021/3/5
*/
public interface SeckillService {
void updateSeckillSkuLatest3Days();
/**
* 获取当前时间秒杀活动商品列表
* @return
*/
List<SmsSeckillSkuVO> getCurrentSeckillSession();
}

View File

@ -1,8 +1,11 @@
package com.youlai.mall.sms.service;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.service.IService;
import com.youlai.mall.sms.pojo.domain.SmsSeckillSession;
import java.util.List;
/**
* @author huawei
* @desc 秒杀活动场次业务接口
@ -10,4 +13,12 @@ import com.youlai.mall.sms.pojo.domain.SmsSeckillSession;
* @date 2021/3/5
*/
public interface SmsSeckillSessionService extends IService<SmsSeckillSession> {
/**
* 根据起始时间和结束时间查询秒杀活动列表
* @param startTime 起始时间
* @param endTime 结束时间
* @return 秒杀活动列表
*/
List<SmsSeckillSession> selectByTime(DateTime startTime, DateTime endTime);
}

View File

@ -3,6 +3,8 @@ package com.youlai.mall.sms.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.youlai.mall.sms.pojo.domain.SmsSeckillSkuRelation;
import java.util.List;
/**
* @author huawei
* @desc 秒杀活动场次商品关联业务接口
@ -10,4 +12,11 @@ import com.youlai.mall.sms.pojo.domain.SmsSeckillSkuRelation;
* @date 2021/3/5
*/
public interface SmsSeckillSkuRelationService extends IService<SmsSeckillSkuRelation> {
/**
* 根据秒杀活动ID获取关联商品
* @param sessionId 秒杀活动场次ID
* @return 关联商品列表
*/
List<SmsSeckillSkuRelation> selectBySessionId(Long sessionId);
}

View File

@ -0,0 +1,140 @@
package com.youlai.mall.sms.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.youlai.common.redis.utils.RedisUtils;
import com.youlai.common.web.util.BeanMapperUtils;
import com.youlai.mall.pms.api.ProductFeignService;
import com.youlai.mall.sms.pojo.domain.SmsSeckillSession;
import com.youlai.mall.sms.pojo.domain.SmsSeckillSkuRelation;
import com.youlai.mall.sms.pojo.to.SeckillSkuRedisTO;
import com.youlai.mall.sms.pojo.vo.SmsSeckillSkuVO;
import com.youlai.mall.sms.service.SeckillService;
import com.youlai.mall.sms.service.SmsSeckillSessionService;
import com.youlai.mall.sms.service.SmsSeckillSkuRelationService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author huawei
* @desc 秒杀模块业务实现类
* @email huawei_code@163.com
* @date 2021/3/5
*/
@Service
@Slf4j
public class SeckillServiceImpl implements SeckillService {
private static final String SECKILL_SESSION_CACHE_PREFIX = "seckill:sessions:";
private static final String SECKILL_SKU_CACHE_PREFIX = "seckill:skus";
private static final String SECKILL_SKU_SEMAPHORE = "seckill:stock:";
@Autowired
private SmsSeckillSessionService seckillSessionService;
@Autowired
private SmsSeckillSkuRelationService seckillSkuRelationService;
@Autowired
private RedisUtils redisUtils;
@Resource
private ProductFeignService productFeignService;
@Autowired
private RedissonClient redissonClient;
@Override
public void updateSeckillSkuLatest3Days() {
// 1获取需要参与秒杀的活动
DateTime startTime = DateUtil.beginOfDay(new Date());
DateTime endTime = DateUtil.endOfDay(DateUtil.offsetDay(new Date(), 3));
List<SmsSeckillSession> seckillSessions = seckillSessionService.selectByTime(startTime, endTime);
if (CollectionUtil.isEmpty(seckillSessions)) {
log.info("秒杀活动列表为空startTime={}endTime={}", startTime, endTime);
return;
}
seckillSessions = seckillSessions.stream().map(session -> {
List<SmsSeckillSkuRelation> relations = seckillSkuRelationService.selectBySessionId(session.getId());
session.setRelations(relations);
return session;
}).collect(Collectors.toList());
// 2将秒杀信息缓存到redis中
saveSessionInfos(seckillSessions);
saveSessionSkuInfos(seckillSessions);
}
@Override
public List<SmsSeckillSkuVO> getCurrentSeckillSession() {
long current = DateUtil.current();
return null;
}
/**
* 缓存活动信息
*
* @param seckillSessions
*/
private void saveSessionInfos(List<SmsSeckillSession> seckillSessions) {
seckillSessions.stream().forEach(session -> {
long startTime = session.getStartTime().getTime();
long endTime = session.getEndTime().getTime();
String key = SECKILL_SESSION_CACHE_PREFIX + session.getId() + "_" + startTime + "_" + endTime;
List<String> relations = session.getRelations().stream().map(sku -> sku.getSessionId() + "-" + sku.getSkuId()).collect(Collectors.toList());
if (!redisUtils.hasKey(key)) {
redisUtils.lSet(key, relations);
}
});
}
private void saveSessionSkuInfos(List<SmsSeckillSession> seckillSessions) {
seckillSessions.stream().forEach(session -> {
List<SmsSeckillSkuRelation> relations = session.getRelations();
if (CollectionUtil.isNotEmpty(relations)) {
relations.stream().forEach(sku -> {
String randomCode = RandomUtil.randomNumbers(8);
// 4保存商品信息到redis中
if (!redisUtils.hHasKey(SECKILL_SKU_CACHE_PREFIX, sku.getSessionId() + "-" + sku.getSkuId())) {
SeckillSkuRedisTO skuRedisTO = BeanMapperUtils.map(sku, SeckillSkuRedisTO.class);
//1sku基本信息
Long skuId = sku.getSkuId();
// Result<SkuDTO> skuInfo = productFeignService.getSkuById(skuId);
// if (skuInfo != null && skuInfo.getCode().equals(ResultCode.SUCCESS.getCode()) && skuInfo.getData() != null) {
// skuRedisTO.setSkuInfo(skuInfo.getData());
// } else {
// log.error("根据商品ID获取详情详情失败skuId={}data={}", skuId, skuInfo);
// }
skuRedisTO.setStartTime(session.getStartTime().getTime());
skuRedisTO.setEndTime(session.getEndTime().getTime());
//2sku秒杀信息
//3随机码
skuRedisTO.setRandomCode(randomCode);
redisUtils.hset(SECKILL_SKU_CACHE_PREFIX, sku.getSessionId() + "-" + sku.getSkuId(), skuRedisTO);
// 5设置秒杀库存信号量
RSemaphore semaphore = redissonClient.getSemaphore(SECKILL_SKU_SEMAPHORE + randomCode);
semaphore.trySetPermits(skuRedisTO.getSeckillCount());
}
});
}
});
}
}

View File

@ -1,5 +1,7 @@
package com.youlai.mall.sms.service.impl;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.mall.sms.mapper.SmsSeckillSessionMapper;
import com.youlai.mall.sms.pojo.domain.SmsSeckillSession;
@ -7,6 +9,8 @@ import com.youlai.mall.sms.service.SmsSeckillSessionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author huawei
* @desc 秒杀活动场次管理业务实现类
@ -16,4 +20,12 @@ import org.springframework.stereotype.Service;
@Service
@Slf4j
public class SmsSeckillSessionServiceImpl extends ServiceImpl<SmsSeckillSessionMapper, SmsSeckillSession> implements SmsSeckillSessionService {
@Override
public List<SmsSeckillSession> selectByTime(DateTime startTime, DateTime endTime) {
log.info("根据起始时间和结束时间查询秒杀活动列表, startTime={}, endTime={}", startTime, endTime);
QueryWrapper<SmsSeckillSession> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("status", 1).between("start_time", startTime, endTime).orderByAsc("start_time");
return this.list(queryWrapper);
}
}

View File

@ -1,5 +1,6 @@
package com.youlai.mall.sms.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.mall.sms.mapper.SmsSeckillSkuRelationMapper;
import com.youlai.mall.sms.pojo.domain.SmsSeckillSkuRelation;
@ -7,6 +8,8 @@ import com.youlai.mall.sms.service.SmsSeckillSkuRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author huawei
* @desc 秒杀活动场次商品关联业务实现类
@ -16,4 +19,11 @@ import org.springframework.stereotype.Service;
@Service
@Slf4j
public class SmsSeckillSkuRelationServiceImpl extends ServiceImpl<SmsSeckillSkuRelationMapper, SmsSeckillSkuRelation> implements SmsSeckillSkuRelationService {
@Override
public List<SmsSeckillSkuRelation> selectBySessionId(Long sessionId) {
log.info("根据秒杀活动场次ID查询关联商品列表sessionId={}",sessionId);
QueryWrapper<SmsSeckillSkuRelation> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("session_id",sessionId);
return this.list(queryWrapper);
}
}

View File

@ -48,6 +48,7 @@
<knife4j.version>2.0.8</knife4j.version>
<logstash-logback-encoder.version>6.6</logstash-logback-encoder.version>
<elasticsearch.version>7.10.1</elasticsearch.version>
<redisson.version>3.11.1</redisson.version>
</properties>
<dependencies>
@ -168,6 +169,12 @@
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>${redisson.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

View File

@ -26,6 +26,11 @@
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>

View File

@ -0,0 +1,37 @@
package com.youlai.common.redis.redisson;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
/**
* @author huawei
* @desc Redisson 配置
* @email huawei_code@163.com
* @date 2021/2/22
*/
@Component
@Configuration
public class RedissonConfig {
@Bean
public RedissonClient redissonClient(RedissonProperties properties) {
if (properties.getServerAddress() == null) {
return null;
}
Config config = new Config();
SingleServerConfig singleServerConfig = config.useSingleServer();
singleServerConfig
//可以用"rediss://"来启用SSL连接
.setAddress(properties.getServerAddress() + ":" + properties.getPort())
.setPassword(properties.getPassword());
RedissonClient redisson = Redisson.create(config);
return redisson;
}
}

View File

@ -0,0 +1,24 @@
package com.youlai.common.redis.redisson;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author huawei
* @desc redisson 连接配置类
* @email huawei_code@163.com
* @date 2021/2/22
*/
@Component
@ConfigurationProperties(prefix = "redisson")
@Data
public class RedissonProperties {
private String serverAddress;
private String port;
private String password;
}

View File

@ -0,0 +1,590 @@
package com.youlai.common.redis.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @author huawei
* @desc spring redis 通用工具类
* @email huawei_code@163.com
* @date 2021/2/9
*/
@Component
public class RedisUtils {
/**
* 注入redisTemplate bean
*/
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 指定缓存失效时间
*
* @param key
* @param time 时间()
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据key获取过期时间
*
* @param key 不能为null
* @return 时间() 返回0代表为永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判断key是否存在
*
* @param key
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除缓存
*
* @param key 可以传一个值 或多个
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete((List<String>) CollectionUtils.arrayToList(key));
}
}
}
// ============================String(字符串)=============================
/**
* 普通缓存获取
*
* @param key
* @return
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通缓存放入
*
* @param key
* @param value
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通缓存放入并设置时间
*
* @param key
* @param value
* @param time 时间() time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 递增
*
* @param key
* @param delta 要增加几(大于0)
* @return
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 递减
*
* @param key
* @param delta 要减少几(小于0)
* @return
*/
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
// ================================Hash(哈希)=================================
/**
* HashGet
*
* @param key 不能为null
* @param item 不能为null
* @return
*/
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* 获取hashKey对应的所有键值
*
* @param key
* @return 对应的多个键值
*/
public Map<Object, Object> hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* HashSet
*
* @param key
* @param map 对应多个键值
* @return true 成功 false 失败
*/
public boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* HashSet 并设置时间
*
* @param key
* @param map 对应多个键值
* @param time 时间()
* @return true成功 false失败
*/
public boolean hmset(String key, Map<String, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key
* @param item
* @param value
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key
* @param item
* @param value
* @param time 时间() 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除hash表中的值
*
* @param key 不能为null
* @param item 可以使多个 不能为null
*/
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/**
* 判断hash表中是否有该项的值
*
* @param key 不能为null
* @param item 不能为null
* @return true 存在 false不存在
*/
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
*
* @param key
* @param item
* @param by 要增加几(大于0)
* @return
*/
public double hincr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/**
* hash递减
*
* @param key
* @param item
* @param by 要减少记(小于0)
* @return
*/
public double hdecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
// ============================Set(集合)=============================
/**
* 根据key获取Set中的所有值
*
* @param key
* @return
*/
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 根据value从一个set中查询,是否存在
*
* @param key
* @param value
* @return true 存在 false不存在
*/
public boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将数据放入set缓存
*
* @param key
* @param values 可以是多个
* @return 成功个数
*/
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 将set数据放入缓存
*
* @param key
* @param time 时间()
* @param values 可以是多个
* @return 成功个数
*/
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0) {
expire(key, time);
}
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 获取set缓存的长度
*
* @param key
* @return
*/
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 移除值为value的
*
* @param key
* @param values 可以是多个
* @return 移除的个数
*/
public long setRemove(String key, Object... values) {
try {
Long count = redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
// ===============================List(列表)=================================
/**
* 获取list缓存的内容
*
* @param key
* @param start 开始
* @param end 结束 0 -1代表所有值
* @return
*/
public List<Object> lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取list缓存的长度
*
* @param key
* @return
*/
public long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 通过索引 获取list中的值
*
* @param key
* @param index 索引 index>=0时 0 表头1 第二个元素依次类推index<0时-1表尾-2倒数第二个元素依次类推
* @return
*/
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 将list放入缓存
*
* @param key
* @param value
* @return
*/
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将list放入缓存
*
* @param key
* @param value
* @param time 时间()
* @return
*/
public boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将list放入缓存
*
* @param key
* @param value
* @return
*/
public boolean lSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将list放入缓存
*
* @param key
* @param value
* @param time 时间()
* @return
*/
public boolean lSet(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据索引修改list中的某条数据
*
* @param key
* @param index 索引
* @param value
* @return
*/
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 移除N个值为value
*
* @param key
* @param count 移除多少个
* @param value
* @return 移除的个数
*/
public long lRemove(String key, long count, Object value) {
try {
Long remove = redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 根据正则表达式获取key列表
*
* @param patternKey 正则表达式
* @return 匹配key列表
*/
public Set<String> keys(String patternKey) {
try {
Set<String> keys = redisTemplate.keys(patternKey);
return keys;
} catch (Exception e) {
e.printStackTrace();
return new HashSet<>();
}
}
}

View File

@ -1,3 +1,6 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.youlai.common.redis.RedisConfig,\
com.youlai.common.redis.component.BusinessNoGenerator
com.youlai.common.redis.component.BusinessNoGenerator,\
com.youlai.common.redis.utils.RedisUtils,\
com.youlai.common.redis.redisson.RedissonConfig,\
com.youlai.common.redis.redisson.RedissonProperties