Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
HYH.APSJ
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
佟礼
HYH.APSJ
Commits
ed06e909
Commit
ed06e909
authored
Dec 19, 2025
by
DESKTOP-VKRD9QF\Administration
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
redis 设置
parent
2a8e9ba8
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
1264 additions
and
3 deletions
+1264
-3
pom.xml
pom.xml
+9
-2
RedisLockUtil.java
src/main/java/com/aps/common/util/redis/RedisLockUtil.java
+53
-0
RedisUtils.java
src/main/java/com/aps/common/util/redis/RedisUtils.java
+817
-0
RedisConfiguration.java
src/main/java/com/aps/config/RedisConfiguration.java
+64
-0
application.yml
src/main/resources/application.yml
+6
-1
RedisUtilsTest.java
src/test/java/com/aps/demo/RedisUtilsTest.java
+315
-0
No files found.
pom.xml
View file @
ed06e909
...
...
@@ -41,14 +41,21 @@
<artifactId>
dynamic-datasource-spring-boot-starter
</artifactId>
<version>
${dynamic-datasource.version}
</version>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
2.0.33
</version>
</dependency>
<!-- 代码生成器 -->
<dependency>
<groupId>
com.baomidou
</groupId>
<artifactId>
mybatis-plus-generator
</artifactId>
<version>
${mybatis-plus.version}
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-redis
</artifactId>
</dependency>
<!-- 模板引擎 -->
<dependency>
<groupId>
org.apache.velocity
</groupId>
...
...
src/main/java/com/aps/common/util/redis/RedisLockUtil.java
0 → 100644
View file @
ed06e909
package
com
.
aps
.
common
.
util
.
redis
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.data.redis.connection.RedisStringCommands
;
import
org.springframework.data.redis.connection.ReturnType
;
import
org.springframework.data.redis.core.RedisCallback
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.types.Expiration
;
import
org.springframework.stereotype.Component
;
import
java.util.concurrent.TimeUnit
;
/**
* Redis分布式锁
*
* @author Administrator
*/
@Slf4j
@Component
@Import
(
RedisUtils
.
class
)
public
class
RedisLockUtil
{
public
RedisLockUtil
(
RedisTemplate
<
String
,
Object
>
redisTemplate
)
{
this
.
redisTemplate
=
redisTemplate
;
}
private
RedisTemplate
<
String
,
Object
>
redisTemplate
;
private
static
final
byte
[]
SCRIPT_RELEASE_LOCK
=
"if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"
.
getBytes
();
/**
* 尝试获取分布式锁
*
* @param key 键
* @param requestId 请求ID
* @param expire 锁的有效时间(秒)
*/
public
Boolean
tryLock
(
String
key
,
String
requestId
,
long
expire
)
{
return
redisTemplate
.
execute
((
RedisCallback
<
Boolean
>)
redisConnection
->
redisConnection
.
set
(
key
.
getBytes
(),
requestId
.
getBytes
(),
Expiration
.
from
(
expire
,
TimeUnit
.
SECONDS
),
RedisStringCommands
.
SetOption
.
SET_IF_ABSENT
));
}
/**
* 释放分布式锁
*
* @param key 键
* @param requestId 请求ID
*/
public
Boolean
releaseLock
(
String
key
,
String
requestId
)
{
return
redisTemplate
.
execute
((
RedisCallback
<
Boolean
>)
redisConnection
->
redisConnection
.
eval
(
SCRIPT_RELEASE_LOCK
,
ReturnType
.
BOOLEAN
,
1
,
key
.
getBytes
(),
requestId
.
getBytes
()));
}
}
\ No newline at end of file
src/main/java/com/aps/common/util/redis/RedisUtils.java
0 → 100644
View file @
ed06e909
This diff is collapsed.
Click to expand it.
src/main/java/com/aps/config/RedisConfiguration.java
0 → 100644
View file @
ed06e909
package
com
.
aps
.
config
;
import
com.fasterxml.jackson.annotation.JsonAutoDetect
;
import
com.fasterxml.jackson.annotation.PropertyAccessor
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.cache.annotation.EnableCaching
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Lazy
;
import
org.springframework.data.redis.connection.RedisConnectionFactory
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer
;
import
org.springframework.data.redis.serializer.RedisSerializer
;
import
org.springframework.data.redis.serializer.StringRedisSerializer
;
/**
* Redis基础配置类
*
* @author pangu
*/
@Configuration
@Lazy
(
value
=
false
)
@EnableCaching
public
class
RedisConfiguration
{
@Bean
@ConditionalOnMissingBean
public
RedisSerializer
<
String
>
redisKeySerializer
()
{
return
new
Jackson2JsonRedisSerializer
(
String
.
class
);
}
@Bean
@ConditionalOnMissingBean
public
RedisSerializer
<
Object
>
redisValueSerializer
()
{
return
new
Jackson2JsonRedisSerializer
(
Object
.
class
);
}
@Bean
public
RedisTemplate
redisTemplate
(
RedisConnectionFactory
factory
)
{
RedisTemplate
template
=
new
RedisTemplate
();
//LettuceConnectionFactory
template
.
setConnectionFactory
(
factory
);
Jackson2JsonRedisSerializer
<
Object
>
jackson2JsonRedisSerializer
=
new
Jackson2JsonRedisSerializer
<
Object
>(
Object
.
class
);
ObjectMapper
om
=
new
ObjectMapper
();
om
.
setVisibility
(
PropertyAccessor
.
ALL
,
JsonAutoDetect
.
Visibility
.
ANY
);
om
.
activateDefaultTyping
(
om
.
getPolymorphicTypeValidator
(),
ObjectMapper
.
DefaultTyping
.
NON_FINAL
);
jackson2JsonRedisSerializer
.
setObjectMapper
(
om
);
StringRedisSerializer
stringRedisSerializer
=
new
StringRedisSerializer
();
// key采用String的序列化方式
template
.
setKeySerializer
(
stringRedisSerializer
);
// hash的key也采用String的序列化方式
template
.
setHashKeySerializer
(
stringRedisSerializer
);
// value序列化方式采用jackson
template
.
setValueSerializer
(
jackson2JsonRedisSerializer
);
// hash的value序列化方式采用jackson
template
.
setHashValueSerializer
(
jackson2JsonRedisSerializer
);
template
.
afterPropertiesSet
();
return
template
;
}
}
src/main/resources/application.yml
View file @
ed06e909
...
...
@@ -9,7 +9,12 @@ spring:
mvc
:
pathmatch
:
matching-strategy
:
ant_path_matcher
# Spring Boot 2.6+ 需要这个配置
redis
:
host
:
192.168.0.181
port
:
6380
timeout
:
10000
database
:
10
password
:
redis@228!
# Swagger 配置
doc
:
swagger-ui
:
...
...
src/test/java/com/aps/demo/RedisUtilsTest.java
0 → 100644
View file @
ed06e909
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment