Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
SpringBoot-kwan
提交
319121f1
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看板
提交
319121f1
编写于
12月 12, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:redis测试
上级
b7d5cea9
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
112 addition
and
47 deletion
+112
-47
pom.xml
pom.xml
+14
-0
src/main/java/com/kwan/springbootkwan/config/RedisConfig.java
...main/java/com/kwan/springbootkwan/config/RedisConfig.java
+42
-42
src/main/java/com/kwan/springbootkwan/config/RedisTestConfig.java
.../java/com/kwan/springbootkwan/config/RedisTestConfig.java
+34
-0
src/main/java/com/kwan/springbootkwan/controller/RedisController.java
...a/com/kwan/springbootkwan/controller/RedisController.java
+22
-5
未找到文件。
pom.xml
浏览文件 @
319121f1
...
...
@@ -249,6 +249,20 @@
<artifactId>
poi
</artifactId>
<version>
4.1.2
</version>
</dependency>
<!--引入redis依赖-->
<!-- Redisson Core 包 -->
<!-- <dependency>-->
<!-- <groupId>org.redisson</groupId>-->
<!-- <artifactId>redisson-core</artifactId>-->
<!-- <version>3.17.2</version> <!– 请替换为最新版本 –>-->
<!-- </dependency>-->
<!-- Redisson 客户端包 -->
<dependency>
<groupId>
org.redisson
</groupId>
<artifactId>
redisson
</artifactId>
<version>
3.17.2
</version>
<!-- 请替换为最新版本 -->
</dependency>
</dependencies>
<build>
<plugins>
...
...
src/main/java/com/kwan/springbootkwan/config/RedisConfig.java
浏览文件 @
319121f1
package
com.kwan.springbootkwan.config
;
import
com.fasterxml.jackson.annotation.JsonAutoDetect
;
import
com.fasterxml.jackson.annotation.PropertyAccessor
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
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.StringRedisSerializer
;
@Slf4j
@Configuration
public
class
RedisConfig
{
@Bean
@SuppressWarnings
(
"all"
)
public
RedisTemplate
<
String
,
Object
>
redisTemplate
(
RedisConnectionFactory
factory
)
{
log
.
info
(
"==================自定义的redisTemplate实例化了==================="
);
RedisTemplate
<
String
,
Object
>
template
=
new
RedisTemplate
<
String
,
Object
>();
template
.
setConnectionFactory
(
factory
);
Jackson2JsonRedisSerializer
jackson2JsonRedisSerializer
=
new
Jackson2JsonRedisSerializer
(
Object
.
class
);
ObjectMapper
om
=
new
ObjectMapper
();
om
.
setVisibility
(
PropertyAccessor
.
ALL
,
JsonAutoDetect
.
Visibility
.
ANY
);
om
.
enableDefaultTyping
(
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
;
}
}
//
package com.kwan.springbootkwan.config;
//
//
import com.fasterxml.jackson.annotation.JsonAutoDetect;
//
import com.fasterxml.jackson.annotation.PropertyAccessor;
//
import com.fasterxml.jackson.databind.ObjectMapper;
//
import lombok.extern.slf4j.Slf4j;
//
import org.springframework.context.annotation.Bean;
//
import org.springframework.context.annotation.Configuration;
//
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.StringRedisSerializer;
//
//
@Slf4j
//
@Configuration
//
public class RedisConfig {
//
//
@Bean
//
@SuppressWarnings("all")
//
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
//
log.info("==================自定义的redisTemplate实例化了===================");
//
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
//
template.setConnectionFactory(factory);
//
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//
ObjectMapper om = new ObjectMapper();
//
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//
om.enableDefaultTyping(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/java/com/kwan/springbootkwan/config/RedisTestConfig.java
0 → 100644
浏览文件 @
319121f1
package
com.kwan.springbootkwan.config
;
import
lombok.Data
;
import
org.redisson.Redisson
;
import
org.redisson.api.RedissonClient
;
import
org.redisson.config.Config
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
@Configuration
@ConfigurationProperties
(
prefix
=
"spring.redis"
)
@Data
public
class
RedisTestConfig
{
private
String
host
;
private
String
port
;
private
String
password
;
@Bean
public
RedissonClient
redissonClient
()
{
Config
config
=
new
Config
();
config
.
useSingleServer
()
.
setAddress
(
String
.
format
(
"redis://%s:%s"
,
host
,
port
))
.
setPassword
(
password
)
.
setConnectionPoolSize
(
50
)
.
setIdleConnectionTimeout
(
10000
)
.
setConnectTimeout
(
3000
)
.
setTimeout
(
3000
)
.
setDatabase
(
5
);
return
Redisson
.
create
(
config
);
}
}
\ No newline at end of file
src/main/java/com/kwan/springbootkwan/controller/RedisController.java
浏览文件 @
319121f1
...
...
@@ -4,13 +4,14 @@ import com.kwan.springbootkwan.entity.Result;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
lombok.extern.slf4j.Slf4j
;
import
org.redisson.api.RMap
;
import
org.redisson.api.RedissonClient
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.annotation.Resource
;
import
java.util.concurrent.TimeUnit
;
import
static
com
.
kwan
.
springbootkwan
.
constant
.
CommonConstant
.
PREFIX_REDIS_KEY
;
...
...
@@ -30,14 +31,30 @@ public class RedisController {
@Resource
private
RedisTemplate
redisTemplate
;
@Resource
private
RedissonClient
redissonClient
;
@ApiOperation
(
value
=
"redis添加数据"
,
notes
=
"redis添加数据"
)
@GetMapping
(
"/add"
)
public
Result
add
(
String
key
,
String
value
)
{
//向对应的key值添加数据
redisTemplate
.
opsForValue
().
set
(
PREFIX_REDIS_KEY
+
key
,
value
);
//设置过期时间
redisTemplate
.
expire
(
PREFIX_REDIS_KEY
,
2000000
,
TimeUnit
.
SECONDS
);
// //向对应的key值添加数据
// redisTemplate.opsForValue().set(PREFIX_REDIS_KEY + key, value);
// //设置过期时间
// redisTemplate.expire(PREFIX_REDIS_KEY, 2000000, TimeUnit.SECONDS);
// 获取或创建一个 Redisson Map 对象,它类似于 Java 的 Map
RMap
<
String
,
String
>
map
=
redissonClient
.
getMap
(
"myMap"
);
// 存储值
map
.
put
(
"key1"
,
"value1"
);
map
.
put
(
"key2"
,
"value2"
);
// 检索值
String
value1
=
map
.
get
(
"key1"
);
String
value2
=
map
.
get
(
"key2"
);
System
.
out
.
println
(
"Value of key1: "
+
value1
);
System
.
out
.
println
(
"Value of key2: "
+
value2
);
return
Result
.
ok
();
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录