fix:redis测试

上级 b7d5cea9
...@@ -249,6 +249,20 @@ ...@@ -249,6 +249,20 @@
<artifactId>poi</artifactId> <artifactId>poi</artifactId>
<version>4.1.2</version> <version>4.1.2</version>
</dependency> </dependency>
<!--引入redis依赖-->
<!-- Redisson Core 包 -->
<!-- <dependency>-->
<!-- <groupId>org.redisson</groupId>-->
<!-- <artifactId>redisson-core</artifactId>-->
<!-- <version>3.17.2</version> &lt;!&ndash; 请替换为最新版本 &ndash;&gt;-->
<!-- </dependency>-->
<!-- Redisson 客户端包 -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.17.2</version> <!-- 请替换为最新版本 -->
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
......
package com.kwan.springbootkwan.config; //package com.kwan.springbootkwan.config;
//
import com.fasterxml.jackson.annotation.JsonAutoDetect; //import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor; //import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper; //import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j; //import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean; //import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; //import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory; //import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate; //import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; //import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer; //import org.springframework.data.redis.serializer.StringRedisSerializer;
//
@Slf4j //@Slf4j
@Configuration //@Configuration
public class RedisConfig { //public class RedisConfig {
//
@Bean // @Bean
@SuppressWarnings("all") // @SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { // public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
log.info("==================自定义的redisTemplate实例化了==================="); // log.info("==================自定义的redisTemplate实例化了===================");
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); // RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory); // template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); // Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper(); // ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); // om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om); // jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式 // // key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer); // template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式 // // hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer); // template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson // // value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer); // template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson // // hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer); // template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet(); // template.afterPropertiesSet();
return template; // return template;
} // }
} //}
//
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
...@@ -4,13 +4,14 @@ import com.kwan.springbootkwan.entity.Result; ...@@ -4,13 +4,14 @@ import com.kwan.springbootkwan.entity.Result;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; 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.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
import static com.kwan.springbootkwan.constant.CommonConstant.PREFIX_REDIS_KEY; import static com.kwan.springbootkwan.constant.CommonConstant.PREFIX_REDIS_KEY;
...@@ -30,14 +31,30 @@ public class RedisController { ...@@ -30,14 +31,30 @@ public class RedisController {
@Resource @Resource
private RedisTemplate redisTemplate; private RedisTemplate redisTemplate;
@Resource
private RedissonClient redissonClient;
@ApiOperation(value = "redis添加数据", notes = "redis添加数据") @ApiOperation(value = "redis添加数据", notes = "redis添加数据")
@GetMapping("/add") @GetMapping("/add")
public Result add(String key, String value) { public Result add(String key, String value) {
//向对应的key值添加数据 // //向对应的key值添加数据
redisTemplate.opsForValue().set(PREFIX_REDIS_KEY + key, value); // redisTemplate.opsForValue().set(PREFIX_REDIS_KEY + key, value);
//设置过期时间 // //设置过期时间
redisTemplate.expire(PREFIX_REDIS_KEY, 2000000, TimeUnit.SECONDS); // 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(); return Result.ok();
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册