From c6103ca00f685056416fb22bfa942184a59b9381 Mon Sep 17 00:00:00 2001 From: wangw <1594593906@qq.com> Date: Tue, 5 Mar 2024 09:41:41 +0800 Subject: [PATCH] =?UTF-8?q?redis=20key=E8=BF=87=E6=9C=9F=20=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/ysk/cashier/config/RedisConfig.java | 11 ---- .../config/RedisKeyExpirationListener.java | 17 ------ .../java/cn/ysk/cashier/utils/CacheKey.java | 23 +++++--- .../config/RedisKeyExpirationListener.java | 55 +++++++++++++++++++ .../repository/shop/TbShopInfoRepository.java | 40 ++++++++------ .../impl/shopimpl/TbShopInfoServiceImpl.java | 6 ++ .../service/shop/TbShopInfoService.java | 2 + .../system/repository/UserRepository.java | 4 ++ .../cashier/system/service/UserService.java | 2 + .../system/service/impl/UserServiceImpl.java | 7 +++ 10 files changed, 112 insertions(+), 55 deletions(-) delete mode 100644 eladmin-common/src/main/java/cn/ysk/cashier/config/RedisKeyExpirationListener.java create mode 100644 eladmin-system/src/main/java/cn/ysk/cashier/config/RedisKeyExpirationListener.java diff --git a/eladmin-common/src/main/java/cn/ysk/cashier/config/RedisConfig.java b/eladmin-common/src/main/java/cn/ysk/cashier/config/RedisConfig.java index f60b92ec..d9fb969c 100644 --- a/eladmin-common/src/main/java/cn/ysk/cashier/config/RedisConfig.java +++ b/eladmin-common/src/main/java/cn/ysk/cashier/config/RedisConfig.java @@ -60,8 +60,6 @@ import java.util.Map; @EnableConfigurationProperties(RedisProperties.class) public class RedisConfig extends CachingConfigurerSupport { - @Autowired - private RedisKeyExpirationListener redisKeyExpirationListener; /** * 设置 redis 数据默认过期时间,默认2小时 @@ -100,15 +98,6 @@ public class RedisConfig extends CachingConfigurerSupport { return template; } - //redis key失效监听 - @Bean - public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) { - RedisMessageListenerContainer container = new RedisMessageListenerContainer(); - container.setConnectionFactory(connectionFactory); - // 监听特定键的过期事件 - container.addMessageListener(redisKeyExpirationListener, new PatternTopic("__keyevent@0__:expired")); - return container; - } /** * 自定义缓存key生成策略,默认将使用该策略 diff --git a/eladmin-common/src/main/java/cn/ysk/cashier/config/RedisKeyExpirationListener.java b/eladmin-common/src/main/java/cn/ysk/cashier/config/RedisKeyExpirationListener.java deleted file mode 100644 index 95b6e090..00000000 --- a/eladmin-common/src/main/java/cn/ysk/cashier/config/RedisKeyExpirationListener.java +++ /dev/null @@ -1,17 +0,0 @@ -package cn.ysk.cashier.config; - -import org.springframework.context.annotation.Configuration; -import org.springframework.data.redis.connection.Message; -import org.springframework.data.redis.connection.MessageListener; - -@Configuration -public class RedisKeyExpirationListener implements MessageListener { - - @Override - public void onMessage(Message message, byte[] pattern) { - String expiredKey = new String(message.getBody()); - System.out.println("过期的Key为: " + expiredKey); - // 在这里添加处理键过期事件的逻辑 - } - -} \ No newline at end of file diff --git a/eladmin-common/src/main/java/cn/ysk/cashier/utils/CacheKey.java b/eladmin-common/src/main/java/cn/ysk/cashier/utils/CacheKey.java index 1134afd1..0f6d7b21 100644 --- a/eladmin-common/src/main/java/cn/ysk/cashier/utils/CacheKey.java +++ b/eladmin-common/src/main/java/cn/ysk/cashier/utils/CacheKey.java @@ -25,34 +25,39 @@ public interface CacheKey { /** * 用户 */ - String USER_ID = "user::id:"; + String USER_ID = "user:id:"; + + /** + * 激活码 + */ + String ACT_CODE = "act_code:"; /** * 数据 */ - String DATA_USER = "data::user:"; + String DATA_USER = "data:user:"; /** * 菜单 */ - String MENU_ID = "menu::id:"; - String MENU_USER = "menu::user:"; + String MENU_ID = "menu:id:"; + String MENU_USER = "menu:user:"; /** * 角色授权 */ - String ROLE_AUTH = "role::auth:"; + String ROLE_AUTH = "role:auth:"; /** * 角色信息 */ - String ROLE_ID = "role::id:"; + String ROLE_ID = "role:id:"; /** * 部门 */ - String DEPT_ID = "dept::id:"; + String DEPT_ID = "dept:id:"; /** * 岗位 */ - String JOB_ID = "job::id:"; + String JOB_ID = "job:id:"; /** * 数据字典 */ - String DICT_NAME = "dict::name:"; + String DICT_NAME = "dict:name:"; } diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/config/RedisKeyExpirationListener.java b/eladmin-system/src/main/java/cn/ysk/cashier/config/RedisKeyExpirationListener.java new file mode 100644 index 00000000..9569bb17 --- /dev/null +++ b/eladmin-system/src/main/java/cn/ysk/cashier/config/RedisKeyExpirationListener.java @@ -0,0 +1,55 @@ +package cn.ysk.cashier.config; + +import cn.ysk.cashier.service.shop.TbShopInfoService; +import cn.ysk.cashier.system.service.UserService; +import cn.ysk.cashier.utils.CacheKey; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.data.redis.connection.Message; +import org.springframework.data.redis.connection.MessageListener; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.listener.PatternTopic; +import org.springframework.data.redis.listener.RedisMessageListenerContainer; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; + +@Slf4j +@Configuration +public class RedisKeyExpirationListener implements MessageListener { + + @Lazy + @Autowired + private TbShopInfoService shopInfoService; + + @Lazy + @Autowired + private UserService userServiceu; + + //redis key失效监听 + @Bean + public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) { + RedisMessageListenerContainer container = new RedisMessageListenerContainer(); + container.setConnectionFactory(connectionFactory); + // 监听特定键的过期事件 + container.addMessageListener(this, new PatternTopic("__keyevent@0__:expired")); + return container; + } + + @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) + public void onMessage(Message message, byte[] pattern) { + String expiredKey = new String(message.getBody()); + + // 检查过期的键是否以特定前缀开头 + if(expiredKey.startsWith(CacheKey.ACT_CODE)) { + String account = expiredKey.substring("act_code:".length()); + log.info("商户到期 账户名为:{}",account); + shopInfoService.update(account); + userServiceu.upEnableByusername(account); + } + } +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/repository/shop/TbShopInfoRepository.java b/eladmin-system/src/main/java/cn/ysk/cashier/repository/shop/TbShopInfoRepository.java index d20c3f82..e23e8670 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/repository/shop/TbShopInfoRepository.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/repository/shop/TbShopInfoRepository.java @@ -1,35 +1,39 @@ /* -* Copyright 2019-2020 Zheng Jie -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package cn.ysk.cashier.repository.shop; import cn.ysk.cashier.pojo.shop.TbShopInfo; import org.apache.ibatis.annotations.Param; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; /** -* @website https://eladmin.vip -* @author lyf -* @date 2023-11-07 -**/ + * @author lyf + * @website https://eladmin.vip + * @date 2023-11-07 + **/ public interface TbShopInfoRepository extends JpaRepository, JpaSpecificationExecutor { @Query("select info from TbShopInfo info where info.account = :account") TbShopInfo findByAccount(@Param("account") String account); + @Modifying + @Query("update TbShopInfo info set info.status=0 where info.account = :account") + void updateByAcc(@Param("account") String account); } \ No newline at end of file diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/TbShopInfoServiceImpl.java b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/TbShopInfoServiceImpl.java index 901d73ac..6a42524e 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/TbShopInfoServiceImpl.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/TbShopInfoServiceImpl.java @@ -207,6 +207,12 @@ public class TbShopInfoServiceImpl implements TbShopInfoService { tbShopInfoRepository.save(tbShopInfo); } + @Override + @Transactional(rollbackFor = Exception.class) + public void update(String account) { + tbShopInfoRepository.updateByAcc(account); + } + @Override public void deleteAll(Integer[] ids) { for (Integer id : ids) { diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/service/shop/TbShopInfoService.java b/eladmin-system/src/main/java/cn/ysk/cashier/service/shop/TbShopInfoService.java index 3d6014b5..42fb066f 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/service/shop/TbShopInfoService.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/service/shop/TbShopInfoService.java @@ -69,6 +69,8 @@ public interface TbShopInfoService { */ void update(TbShopInfo resources); + void update(String account); + /** * 多选删除 * @param ids / diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/system/repository/UserRepository.java b/eladmin-system/src/main/java/cn/ysk/cashier/system/repository/UserRepository.java index 159daff8..5d5afe4b 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/system/repository/UserRepository.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/system/repository/UserRepository.java @@ -61,6 +61,10 @@ public interface UserRepository extends JpaRepository, JpaSpecificat @Query(value = "update sys_user set password = ?2 , pwd_reset_time = ?3 where username = ?1",nativeQuery = true) void updatePass(String username, String pass, Date lastPasswordResetTime); + @Modifying + @Query(value = "update sys_user set enabled = 0 where username = ?1",nativeQuery = true) + void upEnableByusername(String username); + /** * 修改邮箱 * @param username 用户名 diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/system/service/UserService.java b/eladmin-system/src/main/java/cn/ysk/cashier/system/service/UserService.java index 9c8d7548..e7e46af2 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/system/service/UserService.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/system/service/UserService.java @@ -80,6 +80,8 @@ public interface UserService { */ void updatePass(String username, String encryptPassword); + void upEnableByusername(String username); + /** * 修改头像 * @param file 文件 diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/system/service/impl/UserServiceImpl.java b/eladmin-system/src/main/java/cn/ysk/cashier/system/service/impl/UserServiceImpl.java index 3e3d150b..46b3cde6 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/system/service/impl/UserServiceImpl.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/system/service/impl/UserServiceImpl.java @@ -196,6 +196,13 @@ public class UserServiceImpl implements UserService { } } + @Override + @Transactional(rollbackFor = Exception.class) + public void upEnableByusername(String username) { + userRepository.upEnableByusername(username); + flushCache(username); + } + @Override @Transactional(rollbackFor = Exception.class) public void updatePass(String username, String pass) {