Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
SpringBoot-kwan
提交
ab106e39
S
SpringBoot-kwan
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
SpringBoot-kwan
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringBoot-kwan
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
ab106e39
编写于
8月 29, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:添加分布式锁,并用paifox测试
上级
40b20e87
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
100 addition
and
5 deletion
+100
-5
src/main/java/com/kwan/springbootkwan/annotation/RedisLock.java
...in/java/com/kwan/springbootkwan/annotation/RedisLock.java
+26
-0
src/main/java/com/kwan/springbootkwan/aop/RedisLockAspect.java
...ain/java/com/kwan/springbootkwan/aop/RedisLockAspect.java
+62
-0
src/main/java/com/kwan/springbootkwan/controller/ChatbotController.java
...com/kwan/springbootkwan/controller/ChatbotController.java
+2
-3
src/main/java/com/kwan/springbootkwan/controller/DepartmentController.java
.../kwan/springbootkwan/controller/DepartmentController.java
+10
-1
src/main/java/com/kwan/springbootkwan/filter/TimeFilter.java
src/main/java/com/kwan/springbootkwan/filter/TimeFilter.java
+0
-1
未找到文件。
src/main/java/com/kwan/springbootkwan/annotation/RedisLock.java
0 → 100644
浏览文件 @
ab106e39
package
com.kwan.springbootkwan.annotation
;
import
org.springframework.core.annotation.AliasFor
;
import
org.springframework.stereotype.Component
;
import
java.lang.annotation.*
;
@Target
({
ElementType
.
METHOD
})
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Documented
@Component
public
@interface
RedisLock
{
String
value
()
default
""
;
@AliasFor
(
"key"
)
String
name
()
default
""
;
@AliasFor
(
"name"
)
String
key
()
default
""
;
long
expire
()
default
30000
;
// 默认锁的过期时间,单位毫秒
long
timeout
()
default
0
;
// 默认获取锁的超时时间,单位毫秒
boolean
fair
()
default
false
;
// 是否使用公平锁
}
src/main/java/com/kwan/springbootkwan/aop/RedisLockAspect.java
0 → 100644
浏览文件 @
ab106e39
package
com.kwan.springbootkwan.aop
;
import
com.kwan.springbootkwan.annotation.RedisLock
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.StringUtils
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.locks.Lock
;
import
java.util.concurrent.locks.ReentrantLock
;
@Aspect
@Component
@Order
(
1
)
// 设置切面优先级
public
class
RedisLockAspect
{
private
final
StringRedisTemplate
redisTemplate
;
public
RedisLockAspect
(
StringRedisTemplate
redisTemplate
)
{
this
.
redisTemplate
=
redisTemplate
;
}
@Around
(
"@annotation(redisLock)"
)
public
Object
doWithLock
(
ProceedingJoinPoint
joinPoint
,
RedisLock
redisLock
)
throws
Throwable
{
String
lockKey
=
getLockKey
(
redisLock
);
Lock
lock
=
new
ReentrantLock
(
redisLock
.
fair
());
boolean
locked
=
false
;
try
{
locked
=
lock
.
tryLock
(
redisLock
.
timeout
(),
TimeUnit
.
MILLISECONDS
);
if
(
locked
)
{
if
(
StringUtils
.
hasText
(
lockKey
))
{
redisTemplate
.
opsForValue
().
set
(
lockKey
,
"locked"
,
redisLock
.
expire
(),
TimeUnit
.
MILLISECONDS
);
}
return
joinPoint
.
proceed
();
}
else
{
throw
new
RuntimeException
(
"Failed to acquire lock."
);
}
}
finally
{
if
(
locked
)
{
lock
.
unlock
();
if
(
StringUtils
.
hasText
(
lockKey
))
{
redisTemplate
.
delete
(
lockKey
);
}
}
}
}
private
String
getLockKey
(
RedisLock
redisLock
)
{
String
lockKey
=
redisLock
.
key
();
if
(!
StringUtils
.
hasText
(
lockKey
))
{
lockKey
=
redisLock
.
name
();
}
return
lockKey
;
}
}
src/main/java/com/kwan/springbootkwan/controller/ChatbotController.java
浏览文件 @
ab106e39
...
@@ -8,7 +8,6 @@ import com.kwan.springbootkwan.entity.Result;
...
@@ -8,7 +8,6 @@ import com.kwan.springbootkwan.entity.Result;
import
com.kwan.springbootkwan.entity.dto.ChatbotDTO
;
import
com.kwan.springbootkwan.entity.dto.ChatbotDTO
;
import
com.kwan.springbootkwan.service.ChatbotService
;
import
com.kwan.springbootkwan.service.ChatbotService
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.cache.annotation.Cacheable
;
import
org.springframework.web.bind.annotation.DeleteMapping
;
import
org.springframework.web.bind.annotation.DeleteMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PathVariable
;
...
@@ -57,11 +56,11 @@ public class ChatbotController {
...
@@ -57,11 +56,11 @@ public class ChatbotController {
/**
/**
* 分页查询所有数据
* 分页查询所有数据
,本地缓存的使用
*
*
* @return 所有数据
* @return 所有数据
*/
*/
@Cacheable
(
"chatbot-cache"
)
//
@Cacheable("chatbot-cache")
@GetMapping
(
"/page"
)
@GetMapping
(
"/page"
)
public
Result
selectAll
(
@RequestParam
Integer
page
public
Result
selectAll
(
@RequestParam
Integer
page
,
@RequestParam
Integer
pageSize
,
@RequestParam
Integer
pageSize
...
...
src/main/java/com/kwan/springbootkwan/controller/DepartmentController.java
浏览文件 @
ab106e39
package
com.kwan.springbootkwan.controller
;
package
com.kwan.springbootkwan.controller
;
import
com.kwan.springbootkwan.annotation.RedisLock
;
import
com.kwan.springbootkwan.entity.Department
;
import
com.kwan.springbootkwan.entity.Department
;
import
com.kwan.springbootkwan.service.DepartmentService
;
import
com.kwan.springbootkwan.service.DepartmentService
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.PageRequest
;
import
org.springframework.data.domain.PageRequest
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.DeleteMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.PutMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
import
java.util.List
;
import
java.util.List
;
...
@@ -63,6 +70,8 @@ public class DepartmentController {
...
@@ -63,6 +70,8 @@ public class DepartmentController {
@GetMapping
(
"/all"
)
@GetMapping
(
"/all"
)
@RedisLock
(
key
=
"myLock_queryTreeAll"
,
expire
=
5000
,
timeout
=
3000
)
// @RedisLock(key = "myLock_queryTreeAll", timeout = 10000)
public
ResponseEntity
<
List
<
Department
>>
queryTreeAll
()
{
public
ResponseEntity
<
List
<
Department
>>
queryTreeAll
()
{
return
ResponseEntity
.
ok
(
this
.
departmentService
.
queryTreeAll
());
return
ResponseEntity
.
ok
(
this
.
departmentService
.
queryTreeAll
());
}
}
...
...
src/main/java/com/kwan/springbootkwan/filter/TimeFilter.java
浏览文件 @
ab106e39
...
@@ -18,7 +18,6 @@ public class TimeFilter implements Filter {
...
@@ -18,7 +18,6 @@ public class TimeFilter implements Filter {
@Override
@Override
public
void
doFilter
(
ServletRequest
request
,
ServletResponse
response
,
FilterChain
chain
)
throws
IOException
,
ServletException
{
public
void
doFilter
(
ServletRequest
request
,
ServletResponse
response
,
FilterChain
chain
)
throws
IOException
,
ServletException
{
// log.info("先执行:接口执行时间");
chain
.
doFilter
(
request
,
response
);
chain
.
doFilter
(
request
,
response
);
}
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录