From d99b538fd065debb6afe20c0223c721a9b4480b9 Mon Sep 17 00:00:00 2001
From: GYJ <1157756119@qq.com>
Date: Sat, 8 Feb 2025 18:26:36 +0800
Subject: [PATCH] redis
---
cash-api/account-server/pom.xml | 1 +
.../main/java/com/czg/AccountApplication.java | 2 +
.../controller/AuthorizationController.java | 4 +-
.../com/czg/controller/FeignController.java | 8 +-
.../czg/controller/ShopInfoController.java | 2 +-
.../src/main/resources/application-dev.yml | 8 +
cash-api/api-config/pom.xml | 26 +
.../com/czg/controller/FeignController.java | 2 +-
.../src/main/resources/application-dev.yml | 13 +
cash-common/cash-common-redis/pom.xml | 23 +
.../main/java/com/czg/config/RedisConfig.java | 41 ++
.../java/com/czg/service/RedisService.java | 557 ++++++++++++++++++
cash-dependencies/pom.xml | 12 +
.../com/czg/service/AuthorizationService.java | 11 -
.../account}/dto/SysLoginDTO.java | 2 +-
.../account}/dto/shopuser/ShopUserAddDTO.java | 2 +-
.../account}/feign/FeignSystemService.java | 4 +-
.../account/service/AuthorizationService.java | 13 +
.../service}/ShopInfoService.java | 2 +-
.../impl/AuthorizationServiceImpl.java | 6 +-
.../service}/impl/ShopInfoServiceImpl.java | 4 +-
cash-service/pom.xml | 6 +
.../czg/service/system/dto/SysParamsDTO.java | 2 +-
.../czg/service/system/entity/SysParams.java | 2 +
.../system/service/SysParamsService.java | 2 +-
.../service/impl/SysParamsServiceImpl.java | 43 +-
pom.xml | 2 +
27 files changed, 769 insertions(+), 31 deletions(-)
create mode 100644 cash-api/api-config/pom.xml
create mode 100644 cash-common/cash-common-redis/pom.xml
create mode 100644 cash-common/cash-common-redis/src/main/java/com/czg/config/RedisConfig.java
create mode 100644 cash-common/cash-common-redis/src/main/java/com/czg/service/RedisService.java
delete mode 100644 cash-service/account-service/src/main/java/com/czg/service/AuthorizationService.java
rename cash-service/account-service/src/main/java/com/czg/{ => service/account}/dto/SysLoginDTO.java (91%)
rename cash-service/account-service/src/main/java/com/czg/{ => service/account}/dto/shopuser/ShopUserAddDTO.java (96%)
rename cash-service/account-service/src/main/java/com/czg/{ => service/account}/feign/FeignSystemService.java (68%)
create mode 100644 cash-service/account-service/src/main/java/com/czg/service/account/service/AuthorizationService.java
rename cash-service/account-service/src/main/java/com/czg/service/{ => account/service}/ShopInfoService.java (63%)
rename cash-service/account-service/src/main/java/com/czg/service/{ => account/service}/impl/AuthorizationServiceImpl.java (85%)
rename cash-service/account-service/src/main/java/com/czg/service/{ => account/service}/impl/ShopInfoServiceImpl.java (60%)
diff --git a/cash-api/account-server/pom.xml b/cash-api/account-server/pom.xml
index 3c8c04b0..e12b2d66 100644
--- a/cash-api/account-server/pom.xml
+++ b/cash-api/account-server/pom.xml
@@ -30,6 +30,7 @@
+
diff --git a/cash-api/account-server/src/main/java/com/czg/AccountApplication.java b/cash-api/account-server/src/main/java/com/czg/AccountApplication.java
index 6c9ddcaf..7907e59b 100644
--- a/cash-api/account-server/src/main/java/com/czg/AccountApplication.java
+++ b/cash-api/account-server/src/main/java/com/czg/AccountApplication.java
@@ -1,5 +1,6 @@
package com.czg;
+import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@@ -11,6 +12,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
+@MapperScan("com.czg.service.account.mapper")
public class AccountApplication {
public static void main(String[] args) {
SpringApplication.run(AccountApplication.class, args);
diff --git a/cash-api/account-server/src/main/java/com/czg/controller/AuthorizationController.java b/cash-api/account-server/src/main/java/com/czg/controller/AuthorizationController.java
index 92ef7a7a..8ae2af82 100644
--- a/cash-api/account-server/src/main/java/com/czg/controller/AuthorizationController.java
+++ b/cash-api/account-server/src/main/java/com/czg/controller/AuthorizationController.java
@@ -1,9 +1,9 @@
package com.czg.controller;
-import com.czg.dto.SysLoginDTO;
+import com.czg.service.account.dto.SysLoginDTO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
-import com.czg.service.AuthorizationService;
+import com.czg.service.account.service.AuthorizationService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
diff --git a/cash-api/account-server/src/main/java/com/czg/controller/FeignController.java b/cash-api/account-server/src/main/java/com/czg/controller/FeignController.java
index d28396a3..b3725238 100644
--- a/cash-api/account-server/src/main/java/com/czg/controller/FeignController.java
+++ b/cash-api/account-server/src/main/java/com/czg/controller/FeignController.java
@@ -1,7 +1,9 @@
package com.czg.controller;
-import com.czg.feign.FeignSystemService;
+import com.czg.service.account.feign.FeignSystemService;
+import com.czg.service.RedisService;
import jakarta.annotation.Resource;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -15,10 +17,14 @@ public class FeignController {
@Resource
private FeignSystemService feignSystemService;
+ @Autowired
+ private RedisService redisService;
+
@RequestMapping("/test")
public String test() {
String string = feignSystemService.testCall("sssss");
System.out.println(string);
+ redisService.set("sssss", string);
return "test";
}
diff --git a/cash-api/account-server/src/main/java/com/czg/controller/ShopInfoController.java b/cash-api/account-server/src/main/java/com/czg/controller/ShopInfoController.java
index e71b94ee..e6aa6c0a 100644
--- a/cash-api/account-server/src/main/java/com/czg/controller/ShopInfoController.java
+++ b/cash-api/account-server/src/main/java/com/czg/controller/ShopInfoController.java
@@ -1,7 +1,7 @@
package com.czg.controller;
import com.czg.resp.CzgResult;
-import com.czg.service.ShopInfoService;
+import com.czg.service.account.service.ShopInfoService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
diff --git a/cash-api/account-server/src/main/resources/application-dev.yml b/cash-api/account-server/src/main/resources/application-dev.yml
index e423cf78..509763d8 100644
--- a/cash-api/account-server/src/main/resources/application-dev.yml
+++ b/cash-api/account-server/src/main/resources/application-dev.yml
@@ -6,6 +6,13 @@ spring:
username: cashier
password: Cashier@1@
+ data:
+ redis:
+ host: 121.40.128.145
+ port: 6379
+ password: 111111
+ timeout: 1000
+
cloud:
nacos:
config:
@@ -22,3 +29,4 @@ spring:
namespace: 237e1905-0a66-4375-9bb6-a51c3c034aca
+
diff --git a/cash-api/api-config/pom.xml b/cash-api/api-config/pom.xml
new file mode 100644
index 00000000..46d17c12
--- /dev/null
+++ b/cash-api/api-config/pom.xml
@@ -0,0 +1,26 @@
+
+ 4.0.0
+
+ com.czg
+ cash-api
+ 1.0.0
+
+
+ api-config
+ maven-plugin
+
+ api-config Maven Plugin
+
+ https://maven.apache.org
+
+
+ UTF-8
+
+
+
+
+
+
+
+
diff --git a/cash-api/system-server/src/main/java/com/czg/controller/FeignController.java b/cash-api/system-server/src/main/java/com/czg/controller/FeignController.java
index 87ce8e31..7b642506 100644
--- a/cash-api/system-server/src/main/java/com/czg/controller/FeignController.java
+++ b/cash-api/system-server/src/main/java/com/czg/controller/FeignController.java
@@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.RestController;
* @author GYJoker
*/
@RestController
-@RequestMapping("/feign")
+@RequestMapping("/admin/feign")
public class FeignController {
@RequestMapping("/testCall/{name}")
diff --git a/cash-api/system-server/src/main/resources/application-dev.yml b/cash-api/system-server/src/main/resources/application-dev.yml
index 619267ee..4ec5d1d3 100644
--- a/cash-api/system-server/src/main/resources/application-dev.yml
+++ b/cash-api/system-server/src/main/resources/application-dev.yml
@@ -6,6 +6,19 @@ spring:
username: cashier
password: Cashier@1@
+ data:
+ redis:
+ host: 121.40.128.145
+ port: 6379
+ password: 111111
+ timeout: 1000
+ lettuce:
+ pool:
+ min-idle: 0
+ max-idle: 8
+ max-wait: -1ms
+ max-active: 16
+
cloud:
nacos:
discovery:
diff --git a/cash-common/cash-common-redis/pom.xml b/cash-common/cash-common-redis/pom.xml
new file mode 100644
index 00000000..eb378626
--- /dev/null
+++ b/cash-common/cash-common-redis/pom.xml
@@ -0,0 +1,23 @@
+
+ 4.0.0
+
+ com.czg
+ cash-common
+ 1.0.0
+
+ cash-common-redis
+ cash-common-redis
+ https://maven.apache.org
+
+
+
+ redis.clients
+ jedis
+
+
+ org.springframework.data
+ spring-data-redis
+
+
+
diff --git a/cash-common/cash-common-redis/src/main/java/com/czg/config/RedisConfig.java b/cash-common/cash-common-redis/src/main/java/com/czg/config/RedisConfig.java
new file mode 100644
index 00000000..bfd0ab37
--- /dev/null
+++ b/cash-common/cash-common-redis/src/main/java/com/czg/config/RedisConfig.java
@@ -0,0 +1,41 @@
+package com.czg.config;
+
+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.RedisTemplate;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+/**
+ * @author GYJoker
+ */
+@Configuration
+public class RedisConfig {
+
+ /**
+ * redisTemplate相关配置
+ * [@Role(BeanDefinition.ROLE_INFRASTRUCTURE)] 表明这个bean是完全后台模式,不需要被代理。
+ *
+ * @param factory Redis连接工厂类
+ * @return 返回配置项对象
+ */
+ @Bean
+ public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
+
+ RedisTemplate template = new RedisTemplate<>();
+ // 配置连接工厂
+ template.setConnectionFactory(factory);
+
+ // 使用StringRedisSerializer来序列化和反序列化redis的key值
+ template.setKeySerializer(new StringRedisSerializer());
+ // 设置hash key 和value序列化模式
+ template.setHashKeySerializer(new StringRedisSerializer());
+ // template.setHashValueSerializer(jacksonSerial);
+ template.setHashValueSerializer(new StringRedisSerializer());
+ template.setValueSerializer(new StringRedisSerializer());
+ template.afterPropertiesSet();
+
+ return template;
+ }
+
+}
diff --git a/cash-common/cash-common-redis/src/main/java/com/czg/service/RedisService.java b/cash-common/cash-common-redis/src/main/java/com/czg/service/RedisService.java
new file mode 100644
index 00000000..566802fb
--- /dev/null
+++ b/cash-common/cash-common-redis/src/main/java/com/czg/service/RedisService.java
@@ -0,0 +1,557 @@
+package com.czg.service;
+
+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.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author GYJoker
+ */
+@Component
+public class RedisService {
+
+ @Autowired
+ private final RedisTemplate redisTemplate;
+
+ public RedisService(RedisTemplate redisTemplate) {
+ this.redisTemplate = redisTemplate;
+ }
+
+ /**
+ * 删除当前库所有key
+ *
+ * @return 返回删除的key数
+ */
+ public Long deleteAllKeys() {
+ Set keys = redisTemplate.keys("*");
+ assert keys != null;
+ return redisTemplate.delete(keys);
+ }
+
+ /**
+ * 指定缓存失效时间
+ *
+ * @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) {
+ 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) {
+ 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((Collection) 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) {
+ 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) {
+ 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