From a91e9ad74af4b9decad78c0184e8721f48f2c457 Mon Sep 17 00:00:00 2001 From: Administrator Date: Tue, 21 Dec 2021 13:57:08 +0800 Subject: [PATCH] redis vod_play_history新加字段 --- pom.xml | 13 +++++++++++++ src/main/java/com/subsidy/common/configure/RedisConfig.java | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/com/subsidy/controller/AdministerController.java | 9 +++++++-- src/main/java/com/subsidy/controller/MemberController.java | 8 +++++--- src/main/java/com/subsidy/controller/SignInRecordController.java | 5 +++++ src/main/java/com/subsidy/model/VodPlayHistoryDO.java | 5 +++++ src/main/java/com/subsidy/service/AdministerService.java | 1 + src/main/java/com/subsidy/service/MemberService.java | 6 ++++-- src/main/java/com/subsidy/service/SignInRecordService.java | 2 ++ src/main/java/com/subsidy/service/impl/AdministerServiceImpl.java | 32 ++++++++++++++++++++++++++++---- src/main/java/com/subsidy/service/impl/ClassHourDictServiceImpl.java | 2 +- src/main/java/com/subsidy/service/impl/MemberServiceImpl.java | 77 ++++++++++++++++++++++++++++++++++++++++++----------------------------------- src/main/java/com/subsidy/service/impl/SignInRecordServiceImpl.java | 23 +++++++++++++++++++---- src/main/java/com/subsidy/service/impl/SmsVerifyCodeServiceImpl.java | 17 +++++++++++++---- src/main/java/com/subsidy/util/RedisUtil.java | 597 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/resources/application-dev.properties | 9 ++++++++- src/main/resources/application-prod.properties | 6 ++++++ src/main/resources/application.properties | 16 ++++++++++++++++ 18 files changed, 891 insertions(+), 56 deletions(-) create mode 100644 src/main/java/com/subsidy/common/configure/RedisConfig.java create mode 100644 src/main/java/com/subsidy/util/RedisUtil.java diff --git a/pom.xml b/pom.xml index 03da5bf..84e0b4a 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,7 @@ 5.2.0 5.4.1 3.10.3 + 3.3.0 @@ -190,6 +191,18 @@ jlatexmath 1.0.7 + + + org.springframework.boot + spring-boot-starter-data-redis + + + + + redis.clients + jedis + ${jedis.version} + diff --git a/src/main/java/com/subsidy/common/configure/RedisConfig.java b/src/main/java/com/subsidy/common/configure/RedisConfig.java new file mode 100644 index 0000000..dffdaeb --- /dev/null +++ b/src/main/java/com/subsidy/common/configure/RedisConfig.java @@ -0,0 +1,119 @@ +package com.subsidy.common.configure; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.cache.annotation.CachingConfigurerSupport; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.*; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +@EnableCaching + +/** + *

+ * Redis + *

+ * + * @author DengMin + * @since 2020/12/14 + */ +@Configuration +public class RedisConfig extends CachingConfigurerSupport { + + /** + * retemplate相关配置 + * @param factory + * @return + */ + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + + RedisTemplate template = new RedisTemplate<>(); + // 配置连接工厂 + template.setConnectionFactory(factory); + + //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式) + Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class); + + ObjectMapper om = new ObjectMapper(); + // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public + om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常 + om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); + jacksonSeial.setObjectMapper(om); + + // 值采用json序列化 + template.setValueSerializer(jacksonSeial); + //使用StringRedisSerializer来序列化和反序列化redis的key值 + template.setKeySerializer(new StringRedisSerializer()); + + // 设置hash key 和value序列化模式 + template.setHashKeySerializer(new StringRedisSerializer()); + template.setHashValueSerializer(jacksonSeial); + template.afterPropertiesSet(); + + return template; + } + + /** + * 对hash类型的数据操作 + * + * @param redisTemplate + * @return + */ + @Bean + public HashOperations hashOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForHash(); + } + + /** + * 对redis字符串类型数据操作 + * + * @param redisTemplate + * @return + */ + @Bean + public ValueOperations valueOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForValue(); + } + + /** + * 对链表类型的数据操作 + * + * @param redisTemplate + * @return + */ + @Bean + public ListOperations listOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForList(); + } + + /** + * 对无序集合类型的数据操作 + * + * @param redisTemplate + * @return + */ + @Bean + public SetOperations setOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForSet(); + } + + /** + * 对有序集合类型的数据操作 + * + * @param redisTemplate + * @return + */ + @Bean + public ZSetOperations zSetOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForZSet(); + } + + +} \ No newline at end of file diff --git a/src/main/java/com/subsidy/controller/AdministerController.java b/src/main/java/com/subsidy/controller/AdministerController.java index 11322d7..9dd8824 100644 --- a/src/main/java/com/subsidy/controller/AdministerController.java +++ b/src/main/java/com/subsidy/controller/AdministerController.java @@ -103,14 +103,14 @@ public class AdministerController { @PostMapping("classDetail") @ApiOperation("** 班级管理--班级成员 id 班级id userName") - //@LoginRequired(value = {ConstantUtils.ADMINISTER_TERMINATE}) + @LoginRequired(value = {ConstantUtils.ADMINISTER_TERMINATE}) public ResponseVO classDetail(@RequestBody ClassDetailDTO classDetailDTO){ return ResponseData.generateCreatedResponse(0,administerService.classDetail(classDetailDTO)); } @PostMapping("exportClassDetail") @ApiOperation("** 班级管理--班级成员--导出 id 班级id userName") - //@LoginRequired(value = {ConstantUtils.ADMINISTER_TERMINATE}) + @LoginRequired(value = {ConstantUtils.ADMINISTER_TERMINATE}) public void exportClassDetail(@RequestBody ClassDetailDTO classDetailDTO)throws Exception{ classDetailDTO.setFlag(true); administerService.exportClassDetail(classDetailDTO); @@ -200,6 +200,11 @@ public class AdministerController { return ResponseData.generateCreatedResponse(0,administerService.importMember(companyId,file)); } + @PostMapping("importRedis") + @ApiOperation("导入老师账号到redis") + public void importRedis(){ + administerService.importRedis(); + } } diff --git a/src/main/java/com/subsidy/controller/MemberController.java b/src/main/java/com/subsidy/controller/MemberController.java index 1441aea..58f469b 100644 --- a/src/main/java/com/subsidy/controller/MemberController.java +++ b/src/main/java/com/subsidy/controller/MemberController.java @@ -137,9 +137,11 @@ public class MemberController { return ResponseData.generateCreatedResponse(0,memberService.checkTimes(exerciseDoneResultDO)); } - @PostMapping("replace") - public void replace(){ - memberService.replace(); + @PostMapping("importRedis") + @ApiOperation("导入学生账号到redis") + public void importRedis(){ + memberService.importRedis(); } + } diff --git a/src/main/java/com/subsidy/controller/SignInRecordController.java b/src/main/java/com/subsidy/controller/SignInRecordController.java index f600090..cd535dc 100644 --- a/src/main/java/com/subsidy/controller/SignInRecordController.java +++ b/src/main/java/com/subsidy/controller/SignInRecordController.java @@ -61,4 +61,9 @@ public class SignInRecordController { return ResponseData.generateCreatedResponse(0,signInRecordService.signInStatus(signInRecordDO)); } + @PostMapping("getRedisData") + public ResponseVO test(){ + return ResponseData.generateCreatedResponse(0,signInRecordService.test()); + } + } diff --git a/src/main/java/com/subsidy/model/VodPlayHistoryDO.java b/src/main/java/com/subsidy/model/VodPlayHistoryDO.java index 1635b64..900a7bc 100644 --- a/src/main/java/com/subsidy/model/VodPlayHistoryDO.java +++ b/src/main/java/com/subsidy/model/VodPlayHistoryDO.java @@ -50,5 +50,10 @@ public class VodPlayHistoryDO extends BaseModel { */ private Integer playRecord; + /** + * 聚合 + */ + private Integer playCount; + } diff --git a/src/main/java/com/subsidy/service/AdministerService.java b/src/main/java/com/subsidy/service/AdministerService.java index d05dcc1..1196e48 100644 --- a/src/main/java/com/subsidy/service/AdministerService.java +++ b/src/main/java/com/subsidy/service/AdministerService.java @@ -68,4 +68,5 @@ public interface AdministerService extends IService { String importMember(Long companyId, MultipartFile multipartFile); + void importRedis(); } diff --git a/src/main/java/com/subsidy/service/MemberService.java b/src/main/java/com/subsidy/service/MemberService.java index 1ddfb6b..f53d7e5 100644 --- a/src/main/java/com/subsidy/service/MemberService.java +++ b/src/main/java/com/subsidy/service/MemberService.java @@ -11,6 +11,7 @@ import com.subsidy.vo.administer.UserRoleVO; import com.subsidy.vo.member.*; import com.subsidy.vo.paper.QueryPapersVO; +import java.lang.reflect.Member; import java.util.List; /** @@ -33,7 +34,7 @@ public interface MemberService extends IService { UserRoleVO login(VerifyCodeDTO verifyCodeDTO); - UserRoleVO passwordLogin(PasswordLoginDTO passwordLoginDTO); + MemberDO passwordLogin(PasswordLoginDTO passwordLoginDTO); String updatePassword(MemberDO memberDO); @@ -55,5 +56,6 @@ public interface MemberService extends IService { String checkTimes(ExerciseDoneResultDO exerciseDoneResultDO); - void replace(); + void importRedis(); + } diff --git a/src/main/java/com/subsidy/service/SignInRecordService.java b/src/main/java/com/subsidy/service/SignInRecordService.java index f285364..fd7b878 100644 --- a/src/main/java/com/subsidy/service/SignInRecordService.java +++ b/src/main/java/com/subsidy/service/SignInRecordService.java @@ -28,4 +28,6 @@ public interface SignInRecordService extends IService { IPage classSignInfo(ClassSignInfoDTO classSignInfoDTO); SignInStatusVO signInStatus(SignInRecordDO signInRecordDO); + + Object test(); } diff --git a/src/main/java/com/subsidy/service/impl/AdministerServiceImpl.java b/src/main/java/com/subsidy/service/impl/AdministerServiceImpl.java index 56d92ad..e66e482 100644 --- a/src/main/java/com/subsidy/service/impl/AdministerServiceImpl.java +++ b/src/main/java/com/subsidy/service/impl/AdministerServiceImpl.java @@ -39,6 +39,7 @@ import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.request.RequestContextHolder; @@ -110,16 +111,29 @@ public class AdministerServiceImpl extends ServiceImpl() - .lambda() - .eq(AdministerDO::getAccountName, administerDO.getAccountName())); + /** + * 先从redis里拿 + */ + AdministerDO administerDO1 = (AdministerDO)redisUtil.get("subsidyAdminister_"+administerDO.getAccountName()); + if (null==administerDO1){ + /** + * 查表,并将数据写入到redis + */ + administerDO1 = this.baseMapper.selectOne(new QueryWrapper() + .lambda() + .eq(AdministerDO::getAccountName, administerDO.getAccountName())); + redisUtil.set("subsidyAdminister_"+administerDO.getAccountName(),administerDO1); + } /** - * 最近5次都输入失败,5分钟后再验证 + * 最近5次都输入失败,5分钟后再验证 待优化 */ List oprAdmDictDOS = oprAdmDictMapper.getLoginInfo(administerDO1.getId()); @@ -268,6 +282,9 @@ public class AdministerServiceImpl extends ServiceImpl administerDOS = this.baseMapper.selectList(null); + for (AdministerDO administerDO: administerDOS){ + redisUtil.set("subsidyAdmin_"+administerDO.getAccountName(),administerDO); + } + } + public String userName(String originName, int i, Long companyId) { i++; List memberDOS = memberMapper.selectList(new QueryWrapper() diff --git a/src/main/java/com/subsidy/service/impl/ClassHourDictServiceImpl.java b/src/main/java/com/subsidy/service/impl/ClassHourDictServiceImpl.java index 61bb337..d844dc5 100644 --- a/src/main/java/com/subsidy/service/impl/ClassHourDictServiceImpl.java +++ b/src/main/java/com/subsidy/service/impl/ClassHourDictServiceImpl.java @@ -74,7 +74,7 @@ public class ClassHourDictServiceImpl extends ServiceImpl() .lambda() .eq(ClassHourDictDO::getStatus,1) - .eq(ClassHourDictDO::getCompanyId,memberDO.getCompanyId())); + .gt(ClassHourDictDO::getCompanyId,memberDO.getCompanyId())); if (null == classHourDictDO){ pollingGetVO.setBool(false); return pollingGetVO; diff --git a/src/main/java/com/subsidy/service/impl/MemberServiceImpl.java b/src/main/java/com/subsidy/service/impl/MemberServiceImpl.java index 8ec6a5c..ff1d859 100644 --- a/src/main/java/com/subsidy/service/impl/MemberServiceImpl.java +++ b/src/main/java/com/subsidy/service/impl/MemberServiceImpl.java @@ -13,6 +13,7 @@ import com.subsidy.service.MemberService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.subsidy.util.ConstantUtils; import com.subsidy.util.MathUtil; +import com.subsidy.util.RedisUtil; import com.subsidy.vo.administer.UserRoleVO; import com.subsidy.vo.course.QueryCoursesVO; import com.subsidy.vo.member.*; @@ -83,6 +84,9 @@ public class MemberServiceImpl extends ServiceImpl imple @Autowired private OprMemDictMapper oprMemDictMapper; + @Autowired + private RedisUtil redisUtil; + public IPage getAll(GetAllDTO getAllDTO) { Page pager = new Page(getAllDTO.getPageNum(), getAllDTO.getPageSize()); IPage iPage = this.baseMapper.getMembers(pager, getAllDTO.getDepartmentId(), getAllDTO.getUserName(), getAllDTO.getStatus()); @@ -223,12 +227,14 @@ public class MemberServiceImpl extends ServiceImpl imple } BeanUtils.copyProperties(memberDO, userRoleVO); - SmsVerifyCodeDO smsCodeDO = smsVerifyCodeMapper.selectOne(new QueryWrapper() - .lambda() - .eq(SmsVerifyCodeDO::getTelephone, verifyCodeDTO.getTelephone()) - .eq(SmsVerifyCodeDO::getVerifyCode, verifyCodeDTO.getVerifyCode()) - .gt(SmsVerifyCodeDO::getCreateDate, LocalDateTime.now().minusMinutes(30L))); - if (smsCodeDO == null) { + String code = redisUtil.get("subsidySmsCode_"+verifyCodeDTO.getTelephone()).toString(); + + //SmsVerifyCodeDO smsCodeDO = smsVerifyCodeMapper.selectOne(new QueryWrapper() + // .lambda() + // .eq(SmsVerifyCodeDO::getTelephone, verifyCodeDTO.getTelephone()) + // .eq(SmsVerifyCodeDO::getVerifyCode, verifyCodeDTO.getVerifyCode()) + // .gt(SmsVerifyCodeDO::getCreateDate, LocalDateTime.now().minusMinutes(30L))); + if (code == null || !verifyCodeDTO.getVerifyCode().equals(code) ) { OprMemDictDO oprMemDictDO = new OprMemDictDO(); oprMemDictDO.setUserId(memberDO.getId()); oprMemDictDO.setOprType("登录"); @@ -242,46 +248,63 @@ public class MemberServiceImpl extends ServiceImpl imple oprMemDictDO.setResult(1); oprMemDictMapper.insert(oprMemDictDO); + //将学员信息写到redis + redisUtil.set("subsidyMember_"+memberDO.getCompanyId()+":"+memberDO.getAccountName(),memberDO); return userRoleVO; } - public UserRoleVO passwordLogin(PasswordLoginDTO passwordLoginDTO) { - UserRoleVO userRoleVO = this.baseMapper.passwordLogin(passwordLoginDTO); - if (userRoleVO == null) { + public MemberDO passwordLogin(PasswordLoginDTO passwordLoginDTO) { + //UserRoleVO userRoleVO = this.baseMapper.passwordLogin(passwordLoginDTO); + /** + * 先从redis里查 + */ + MemberDO memberDO = (MemberDO) redisUtil.get("subsidyMember_"+passwordLoginDTO.getCompanyId()+":"+passwordLoginDTO.getAccountName()); + if (null==memberDO){ + /** + * redis里没有,从数据库里查 + */ + memberDO = this.baseMapper.selectOne(new QueryWrapper() + .lambda() + .eq(MemberDO::getCompanyId,passwordLoginDTO.getCompanyId()) + .eq(MemberDO::getAccountName,passwordLoginDTO.getAccountName())); + } + if (memberDO == null) { throw new HttpException(10017); } - if (!userRoleVO.getPassword().equals(passwordLoginDTO.getPassword())){ + if (!memberDO.getPassword().equals(passwordLoginDTO.getPassword())) { OprMemDictDO oprMemDictDO = new OprMemDictDO(); - oprMemDictDO.setUserId(userRoleVO.getId()); + oprMemDictDO.setUserId(memberDO.getId()); oprMemDictDO.setOprType("登录"); oprMemDictDO.setResult(0); oprMemDictMapper.insert(oprMemDictDO); throw new HttpException(10016); } - if (0==userRoleVO.getFirstLogin()){ + if (0 == memberDO.getFirstLogin()) { OprMemDictDO oprMemDictDO = new OprMemDictDO(); - oprMemDictDO.setUserId(userRoleVO.getId()); + oprMemDictDO.setUserId(memberDO.getId()); oprMemDictDO.setOprType("登录"); oprMemDictDO.setResult(0); oprMemDictMapper.insert(oprMemDictDO); throw new HttpException(10015); } - if ("冻结".equals(userRoleVO.getStatus())) { + if ("冻结".equals(memberDO.getStatus())) { throw new HttpException(10014); } OprMemDictDO oprMemDictDO = new OprMemDictDO(); - oprMemDictDO.setUserId(userRoleVO.getId()); + oprMemDictDO.setUserId(memberDO.getId()); oprMemDictDO.setOprType("登录"); oprMemDictDO.setResult(1); oprMemDictMapper.insert(oprMemDictDO); - return userRoleVO; + return memberDO; } public String updatePassword(MemberDO memberDO) { this.baseMapper.updateById(memberDO); + MemberDO memberDO1 = this.baseMapper.selectById(memberDO.getId()); + redisUtil.set("subsidyMember_"+memberDO1.getCompanyId()+":"+memberDO.getAccountName(),memberDO); return ConstantUtils.SET_SUCCESS; } @@ -600,27 +623,11 @@ public class MemberServiceImpl extends ServiceImpl imple return ConstantUtils.CHECK_STATUS; } - public void replace(){ - + public void importRedis() { List memberDOS = this.baseMapper.selectList(null); - - HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); - //拼音小写 - format.setCaseType(HanyuPinyinCaseType.LOWERCASE); - //不带声调 - format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); - - try { - for (MemberDO memberDO : memberDOS) { - - String accountName = PinyinHelper.toHanYuPinyinString(memberDO.getUserName(), format, "", true); - memberDO.setAccountName(accountName); - this.baseMapper.updateById(memberDO); - } - }catch (Exception e){ - + for (MemberDO memberDO : memberDOS) { + redisUtil.set("subsidyMember_"+memberDO.getCompanyId()+":"+memberDO.getAccountName(),memberDO); } } - } diff --git a/src/main/java/com/subsidy/service/impl/SignInRecordServiceImpl.java b/src/main/java/com/subsidy/service/impl/SignInRecordServiceImpl.java index 76cc13b..2ff5291 100644 --- a/src/main/java/com/subsidy/service/impl/SignInRecordServiceImpl.java +++ b/src/main/java/com/subsidy/service/impl/SignInRecordServiceImpl.java @@ -12,6 +12,7 @@ import com.subsidy.service.SignInRecordService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.subsidy.util.ConstantUtils; import com.subsidy.util.MathUtil; +import com.subsidy.util.RedisUtil; import com.subsidy.vo.member.GetMemberSignInfoVO; import com.subsidy.vo.sign.ClassSignInfoVO; import com.subsidy.vo.sign.SignInStatusVO; @@ -21,6 +22,7 @@ import org.springframework.stereotype.Service; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; +import java.util.Set; /** *

@@ -48,6 +50,9 @@ public class SignInRecordServiceImpl extends ServiceImpl getMemberSignInfo(GetMemberSignInfoDTO getMemberSignInfoDTO) { Page pager = new Page(getMemberSignInfoDTO.getPageNum(), getMemberSignInfoDTO.getPageSize()); @@ -74,6 +79,8 @@ public class SignInRecordServiceImpl extends ServiceImpl() .lambda() @@ -45,10 +49,15 @@ public class SmsVerifyCodeServiceImpl extends ServiceImpl + * redisTemplate封装 + *

+ * + * @author DengMin + * @since 2020/12/17 + */ + +@Component +public class RedisUtil { + + + @Autowired + private RedisTemplate redisTemplate; + + public RedisUtil(RedisTemplate redisTemplate) { + this.redisTemplate = 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(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); + } + + //================================Map================================= + /** + * 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 hmget(String key){ + return redisTemplate.opsForHash().entries(key); + } + + /** + * HashSet + * @param key 键 + * @param map 对应多个键值 + * @return true 成功 false 失败 + */ + public boolean hmset(String key, Map 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 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 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 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 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 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 pattern + * @return + */ + public Set keys(String pattern){ + return redisTemplate.keys(pattern); + } + + /** + * 使用Redis的消息队列 + * @param channel + * @param message 消息内容 + */ + public void convertAndSend(String channel, Object message){ + redisTemplate.convertAndSend(channel,message); + } + + + //=========BoundListOperations 用法 start============ + + + /** + * 根据起始结束序号遍历Redis中的list + * @param listKey + * @param start 起始序号 + * @param end 结束序号 + * @return + */ + public List rangeList(String listKey, long start, long end) { + //绑定操作 + BoundListOperations boundValueOperations = redisTemplate.boundListOps(listKey); + //查询数据 + return boundValueOperations.range(start, end); + } + /** + * 弹出右边的值 --- 并且移除这个值 + * @param listKey + */ + public Object rifhtPop(String listKey){ + //绑定操作 + BoundListOperations boundValueOperations = redisTemplate.boundListOps(listKey); + return boundValueOperations.rightPop(); + } + + //=========BoundListOperations 用法 End============ + +} \ No newline at end of file diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index 6ff748a..3392cf0 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -24,4 +24,11 @@ spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.druid.useGlobalDataSourceStat=true # 控制台日志打印 -mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl \ No newline at end of file +mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl + +spring.redis.host=47.97.19.66 +spring.redis.password=Ykhl@208 +spring.redis.port=6389 +spring.redis.lettuce.pool.max-idle=16 +spring.redis.lettuce.pool.max-active=32 +spring.redis.lettuce.pool.min-idle=8 \ No newline at end of file diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties index 2f61ade..0164ddc 100644 --- a/src/main/resources/application-prod.properties +++ b/src/main/resources/application-prod.properties @@ -31,3 +31,9 @@ spring.servlet.multipart.max-request-size=4098KB #wechat.appSecret= 25d57cad61fc1b45b3afa46d4c35e8f6 #wechat.msgUrl= https://teachai.youkehulian.com/login #wechat.templateId= Z1HNPLOrKiHEjfR1KMP-cC-uH3BWqDJIHv365Ev20yQ + +spring.redis.cluster.nodes=47.97.19.66:6377,47.97.19.66:6378,47.97.16.66:6379 +spring.redis.password=Ykhl@208 +spring.redis.lettuce.pool.max-idle=16 +spring.redis.lettuce.pool.max-active=32 +spring.redis.lettuce.pool.min-idle=8 \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f65f337..cbb7380 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -63,3 +63,19 @@ spring.quartz.overwrite-existing-jobs=false # 自动建表,如果已经存在表请勿使用,会覆盖数据表 #spring.quartz.jdbc.initialize-schema=always #spring.datasource.initialization-mode=embedded + + +#spring.redis.cluster.nodes=47.97.19.66:6377,47.97.19.66:6378,47.97.19.66:6379 +##spring.redis.port=6379 +#spring.redis.password=Ykhl@208 +## spring.redis.cluster.max-redirects=3 +#spring.redis.lettuce.pool.max-idle=16 +#spring.redis.lettuce.pool.max-active=32 +#spring.redis.lettuce.pool.min-idle=8 + +#spring.redis.host=47.97.19.66 +#spring.redis.password=Ykhl@208 +#spring.redis.port=6389 +#spring.redis.lettuce.pool.max-idle=16 +#spring.redis.lettuce.pool.max-active=32 +#spring.redis.lettuce.pool.min-idle=8 \ No newline at end of file -- libgit2 0.25.0