redis key过期 业务处理
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<TbShopInfo, Integer>, JpaSpecificationExecutor<TbShopInfo> {
|
||||
|
||||
@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);
|
||||
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -69,6 +69,8 @@ public interface TbShopInfoService {
|
||||
*/
|
||||
void update(TbShopInfo resources);
|
||||
|
||||
void update(String account);
|
||||
|
||||
/**
|
||||
* 多选删除
|
||||
* @param ids /
|
||||
|
||||
@@ -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)
|
||||
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 用户名
|
||||
|
||||
@@ -80,6 +80,8 @@ public interface UserService {
|
||||
*/
|
||||
void updatePass(String username, String encryptPassword);
|
||||
|
||||
void upEnableByusername(String username);
|
||||
|
||||
/**
|
||||
* 修改头像
|
||||
* @param 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
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void updatePass(String username, String pass) {
|
||||
|
||||
Reference in New Issue
Block a user