RedisSettingMap.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package com.YuyeTech.TPlat.utils;
  2. import cn.hutool.crypto.asymmetric.RSA;
  3. import cn.hutool.json.JSONUtil;
  4. import com.YuyeTech.TPlat.constant.RankingConstant;
  5. import com.YuyeTech.TPlat.constant.RedisConstant;
  6. import com.YuyeTech.TPlat.enums.RedisType;
  7. import com.alibaba.fastjson.JSON;
  8. import com.alibaba.fastjson.TypeReference;
  9. import com.YuyeTech.TPlat.sys.syssetting.service.SysSettingService;
  10. import com.YuyeTech.TPlat.sys.syssetting.vo.SysSettingVo;
  11. import com.YuyeTech.TPlat.sys.syssettinglimit.service.SysSettingLimitService;
  12. import com.YuyeTech.TPlat.sys.syssettinglimit.vo.SysSettingLimitVo;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Qualifier;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.data.redis.core.StringRedisTemplate;
  17. import org.springframework.stereotype.Component;
  18. import javax.annotation.Resource;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import java.util.UUID;
  22. import java.util.concurrent.TimeUnit;
  23. /**
  24. *
  25. * 存储对应的配置
  26. * @author:slambb
  27. * @date:2022/4/19
  28. */
  29. @Component
  30. public class RedisSettingMap {
  31. @Value("${spring.application.name}")
  32. private String applicationName;
  33. @Autowired
  34. private StringRedisTemplate redisTemplate;
  35. @Autowired
  36. private SysSettingService sysSettingService;
  37. @Autowired
  38. private SysSettingLimitService sysSettingLimitService;
  39. //动态设置一个RSA key保存
  40. private String rsaPublicKey = ":rsa:public";
  41. private String rsaPrivateKey = ":rsa:private";
  42. //update 一次rsa数据
  43. public void updateRSAMap(){
  44. RSA rsa = new RSA();
  45. String privateKeyBase64 = rsa.getPrivateKeyBase64();
  46. String publicKeyBase64 = rsa.getPublicKeyBase64();
  47. redisTemplate.opsForValue().set(applicationName.concat(rsaPublicKey), JSONUtil.toJsonStr(publicKeyBase64));
  48. redisTemplate.opsForValue().set(applicationName.concat(rsaPrivateKey), JSONUtil.toJsonStr(privateKeyBase64));
  49. }
  50. //从redis 获取系统设置 rsa 设置
  51. public String getRSAPublicKeyBase64(){
  52. if (redisTemplate.hasKey(applicationName.concat(rsaPublicKey))) {
  53. return redisTemplate.opsForValue().get(applicationName.concat(rsaPublicKey));
  54. } else {
  55. RSA rsa = new RSA();
  56. String privateKeyBase64 = rsa.getPrivateKeyBase64();
  57. String publicKeyBase64 = rsa.getPublicKeyBase64();
  58. redisTemplate.opsForValue().set(applicationName.concat(rsaPublicKey), JSONUtil.toJsonStr(publicKeyBase64));
  59. redisTemplate.opsForValue().set(applicationName.concat(rsaPrivateKey), JSONUtil.toJsonStr(privateKeyBase64));
  60. return publicKeyBase64;
  61. }
  62. }
  63. public String getRSAPrivateKeyBase64(){
  64. if (redisTemplate.hasKey(applicationName.concat(rsaPrivateKey))) {
  65. return redisTemplate.opsForValue().get(applicationName.concat(rsaPrivateKey));
  66. } else {
  67. RSA rsa = new RSA();
  68. String privateKeyBase64 = rsa.getPrivateKeyBase64();
  69. String publicKeyBase64 = rsa.getPublicKeyBase64();
  70. redisTemplate.opsForValue().set(applicationName.concat(rsaPublicKey), JSONUtil.toJsonStr(publicKeyBase64));
  71. redisTemplate.opsForValue().set(applicationName.concat(rsaPrivateKey), JSONUtil.toJsonStr(privateKeyBase64));
  72. return privateKeyBase64;
  73. }
  74. }
  75. //系统设置key
  76. private String sysSettingKey = ":sys:setting";
  77. //update 一次redis数据
  78. public void updateSysSettingMap(){
  79. SysSettingVo sysSettingVo = sysSettingService.get("1").getData();
  80. redisTemplate.opsForValue().set(applicationName.concat(sysSettingKey), JSONUtil.toJsonStr(sysSettingVo));
  81. }
  82. //从redis 获取系统设置 sysSettingMap
  83. public SysSettingVo getSysSettingMap(){
  84. SysSettingVo sysSettingVo;
  85. if (redisTemplate.hasKey(applicationName.concat(sysSettingKey))) {
  86. sysSettingVo = JSON.parseObject(redisTemplate.opsForValue().get(applicationName.concat(sysSettingKey)),new TypeReference<SysSettingVo>(){});
  87. } else {
  88. sysSettingVo = sysSettingService.get("1").getData();
  89. redisTemplate.opsForValue().set(applicationName.concat(sysSettingKey), JSONUtil.toJsonStr(sysSettingVo));
  90. }
  91. return sysSettingVo;
  92. }
  93. //redis 设置 sysSettingMap
  94. public void setSysSettingMap(SysSettingVo sysSettingVo){
  95. redisTemplate.opsForValue().set(applicationName.concat(sysSettingKey), JSONUtil.toJsonStr(sysSettingVo));
  96. }
  97. //限制登录ip key
  98. private String sysSettingLimitKey = ":sys:settingLimit";
  99. //update 一次redis数据
  100. public void updateSysSettingLimitMap(){
  101. SysSettingLimitVo sysSettingLimitVo = sysSettingLimitService.get("1").getData();
  102. redisTemplate.opsForValue().set(applicationName.concat(sysSettingLimitKey), JSONUtil.toJsonStr(sysSettingLimitVo));;
  103. }
  104. //从redis 获取系统设置 sysSettingLimitMap
  105. public SysSettingLimitVo getSysSettingLimitMap(){
  106. SysSettingLimitVo sysSettingLimitVo;
  107. if (redisTemplate.hasKey(applicationName.concat(sysSettingLimitKey))) {
  108. sysSettingLimitVo = JSON.parseObject(redisTemplate.opsForValue().get(applicationName.concat(sysSettingLimitKey)),new TypeReference<SysSettingLimitVo>(){});
  109. } else {
  110. sysSettingLimitVo = sysSettingLimitService.get("1").getData();
  111. redisTemplate.opsForValue().set(applicationName.concat(sysSettingLimitKey), JSONUtil.toJsonStr(sysSettingLimitVo));
  112. }
  113. return sysSettingLimitVo;
  114. }
  115. //设置SysSettingTask到Map
  116. public void setSysSettingLimitMap(SysSettingLimitVo sysSettingLimitVo){
  117. redisTemplate.opsForValue().set(applicationName.concat(sysSettingLimitKey), JSONUtil.toJsonStr(sysSettingLimitVo));
  118. }
  119. /**
  120. * ranking 排行榜相关操作
  121. */
  122. /**
  123. * 获取城市key
  124. * @param cityCode
  125. * @param gameId
  126. * @return
  127. */
  128. public String getCityKey(String cityCode,String gameId){
  129. return applicationName.concat(":city:").concat(String.format(RankingConstant.cityKey, cityCode.substring(0, 4), gameId));
  130. }
  131. /**
  132. * 获取全国key
  133. * @param gameId
  134. * @return
  135. */
  136. public String getChinaKey(String gameId){
  137. return applicationName.concat(":china:").concat(String.format(RankingConstant.chinaKey, gameId));
  138. }
  139. /**
  140. * token相关操作
  141. */
  142. /**
  143. * 根据token生成对应的key
  144. * @param token
  145. * @return
  146. */
  147. public String getTokenKey(String token){
  148. return applicationName.concat(":").concat(String.format(RedisConstant.TOKEN_PREFIX, token));
  149. }
  150. /**
  151. * 对应token存放 map 信息
  152. * @param token
  153. * @param tokenMap
  154. */
  155. public void putToken(String token, Map<String, String> tokenMap){
  156. Integer expire = RedisConstant.EXPIRE;//过期时间
  157. redisTemplate.opsForHash().putAll(getTokenKey(token), tokenMap);
  158. redisTemplate.expire(getTokenKey(token), expire, TimeUnit.SECONDS);
  159. }
  160. /**
  161. * SMS相关操作
  162. */
  163. /**
  164. * 短信key
  165. * @param key
  166. * @return
  167. */
  168. public String getSMSKey(String key){
  169. return applicationName.concat(":").concat(String.format(RedisConstant.SMS_PREFIX, key));
  170. }
  171. /**
  172. * 设置短信缓存
  173. * @param key
  174. * @param code
  175. */
  176. public void setSMS(String key,String code){
  177. Integer expire = RedisConstant.CODE_EXPIRE;//过期时间
  178. redisTemplate.opsForValue().set(getSMSKey(key), code, expire, TimeUnit.SECONDS);
  179. }
  180. /**
  181. * 根据类型获取缓存值
  182. * @param redisType
  183. * @param key
  184. * @return
  185. */
  186. public String getValue(RedisType redisType,String key){
  187. switch (redisType){
  188. case SMS:
  189. /**
  190. * 获取短信key对应的值
  191. */
  192. return redisTemplate.opsForValue().get(getSMSKey(key));
  193. case TOKEN:
  194. /**
  195. * 获取token的key对应userId
  196. */
  197. return redisTemplate.opsForHash().get(getTokenKey(key), "userId").toString();
  198. default: return null;
  199. }
  200. }
  201. /**
  202. * 根据类型删除缓存值
  203. * @param redisType
  204. * @param key
  205. * @return
  206. */
  207. public Boolean deleteValue(RedisType redisType,String key){
  208. switch (redisType){
  209. case SMS:
  210. /**
  211. * 删除短信key的值
  212. */
  213. return redisTemplate.delete(getSMSKey(key));
  214. case TOKEN:
  215. /**
  216. * 删除token的值
  217. */
  218. return redisTemplate.delete(getTokenKey(key));
  219. default: return null;
  220. }
  221. }
  222. }