From cb734129b410c02275c0f2fc5e820d6ccc0e17c0 Mon Sep 17 00:00:00 2001
From: linwe <872216996@qq.com>
Date: Tue, 18 Jun 2024 18:12:20 +0800
Subject: [PATCH] 出租屋查询房东信息
---
src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java | 46 ++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 46 insertions(+), 0 deletions(-)
diff --git a/src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java b/src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java
index 04d6539..7ed7f80 100644
--- a/src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java
+++ b/src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java
@@ -39,6 +39,7 @@
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
+import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.BladeTenantProperties;
import org.springblade.core.tool.constant.BladeConstant;
@@ -65,10 +66,15 @@
import org.springblade.modules.system.vo.UserDetailVO;
import org.springblade.modules.system.vo.UserVO;
import org.springblade.modules.system.wrapper.UserWrapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.connection.ReturnType;
+import org.springframework.data.redis.core.RedisCallback;
+import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
+import java.util.concurrent.TimeUnit;
import static org.springblade.common.constant.CommonConstant.DEFAULT_PARAM_PASSWORD;
@@ -88,6 +94,10 @@
private final IDeptService deptService;
private final BladeTenantProperties tenantProperties;
private final IPoliceAffairsGridService policeAffairsGridService;
+
+ @Autowired
+ private BladeRedis redisTemplate;
+
@Override
@Transactional(rollbackFor = Exception.class)
@@ -215,6 +225,42 @@
return b;
}
+ /**
+ * 尝试获取锁
+ * @param timeout 超时时间(毫秒)
+ * @return 是否成功获取锁
+ */
+ public boolean lock(long timeout,String lockKey) {
+ long endTime = System.currentTimeMillis() + timeout;
+ ValueOperations<String, Object> ops = redisTemplate.getValueOps();
+ while (System.currentTimeMillis() < endTime) {
+ // 使用setIfAbsent命令尝试设置值,仅当key不存在时设置,类似于SETNX
+ Boolean result = ops.setIfAbsent(lockKey, String.valueOf(System.currentTimeMillis() + 5000), 5, TimeUnit.SECONDS);
+ if (result != null && result) {
+ return true;
+ }
+ try {
+ // 短暂休眠,防止CPU过度占用
+ Thread.sleep(10);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new RuntimeException(e);
+ }
+ }
+ return false;
+ }
+
+ /**
+ * 释放锁
+ */
+ public void unlock(String lockKey) {
+ // 使用lua脚本保证操作的原子性,避免删除非本客户端创建的锁
+ String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
+ redisTemplate.getRedisTemplate().execute((RedisCallback<Object>) (connection) -> connection.eval(script.getBytes(), ReturnType.INTEGER, 1, lockKey.getBytes(), String.valueOf(redisTemplate.getValueOps().get(lockKey)).getBytes()));
+ }
+
+
+
@Override
public IPage<User> selectUserPage(IPage<User> page, User user, String deptId, String tenantId) {
if (Strings.isBlank(deptId) && !AuthUtil.isAdministrator() && !AuthUtil.isAdmin()) {
--
Gitblit v1.9.3