redis key过期 业务处理

This commit is contained in:
2024-03-05 09:41:41 +08:00
parent 48f43f7c4d
commit c6103ca00f
10 changed files with 112 additions and 55 deletions

View File

@@ -60,8 +60,6 @@ import java.util.Map;
@EnableConfigurationProperties(RedisProperties.class) @EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig extends CachingConfigurerSupport { public class RedisConfig extends CachingConfigurerSupport {
@Autowired
private RedisKeyExpirationListener redisKeyExpirationListener;
/** /**
* 设置 redis 数据默认过期时间默认2小时 * 设置 redis 数据默认过期时间默认2小时
@@ -100,15 +98,6 @@ public class RedisConfig extends CachingConfigurerSupport {
return template; 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生成策略默认将使用该策略 * 自定义缓存key生成策略默认将使用该策略

View File

@@ -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);
// 在这里添加处理键过期事件的逻辑
}
}

View File

@@ -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_ID = "menu:id:";
String MENU_USER = "menu::user:"; 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:";
} }

View File

@@ -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);
}
}
}

View File

@@ -1,35 +1,39 @@
/* /*
* Copyright 2019-2020 Zheng Jie * Copyright 2019-2020 Zheng Jie
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package cn.ysk.cashier.repository.shop; package cn.ysk.cashier.repository.shop;
import cn.ysk.cashier.pojo.shop.TbShopInfo; import cn.ysk.cashier.pojo.shop.TbShopInfo;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
/** /**
* @website https://eladmin.vip * @author lyf
* @author lyf * @website https://eladmin.vip
* @date 2023-11-07 * @date 2023-11-07
**/ **/
public interface TbShopInfoRepository extends JpaRepository<TbShopInfo, Integer>, JpaSpecificationExecutor<TbShopInfo> { public interface TbShopInfoRepository extends JpaRepository<TbShopInfo, Integer>, JpaSpecificationExecutor<TbShopInfo> {
@Query("select info from TbShopInfo info where info.account = :account") @Query("select info from TbShopInfo info where info.account = :account")
TbShopInfo findByAccount(@Param("account") String 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);
} }

View File

@@ -207,6 +207,12 @@ public class TbShopInfoServiceImpl implements TbShopInfoService {
tbShopInfoRepository.save(tbShopInfo); tbShopInfoRepository.save(tbShopInfo);
} }
@Override
@Transactional(rollbackFor = Exception.class)
public void update(String account) {
tbShopInfoRepository.updateByAcc(account);
}
@Override @Override
public void deleteAll(Integer[] ids) { public void deleteAll(Integer[] ids) {
for (Integer id : ids) { for (Integer id : ids) {

View File

@@ -69,6 +69,8 @@ public interface TbShopInfoService {
*/ */
void update(TbShopInfo resources); void update(TbShopInfo resources);
void update(String account);
/** /**
* 多选删除 * 多选删除
* @param ids / * @param ids /

View File

@@ -61,6 +61,10 @@ public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificat
@Query(value = "update sys_user set password = ?2 , pwd_reset_time = ?3 where username = ?1",nativeQuery = true) @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); 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 用户名 * @param username 用户名

View File

@@ -80,6 +80,8 @@ public interface UserService {
*/ */
void updatePass(String username, String encryptPassword); void updatePass(String username, String encryptPassword);
void upEnableByusername(String username);
/** /**
* 修改头像 * 修改头像
* @param file 文件 * @param file 文件

View File

@@ -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 @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void updatePass(String username, String pass) { public void updatePass(String username, String pass) {