java org.springframework.boot 对redis操作方法
目录
- Java org.springframework.boot 对Redis操作
- 1. 添加依赖
- 2. 配置Redis
- 3. 使用RedisTemplate操作Redis
- 4. 使用Jackson2jsonRedisSerializer(可选)自定义序列化方式(存储对象)
java org.springframework.boot 对redis操作
在Spring Boot项目中操作Redis,你可以使用Spring Data Redis。Spring Data Redis是Spring提供的一个用于简化Redis数据访问的模块,它提供了一个易于使用的编程模型来与Redis交互。
1. 添加依赖
首先,你需要在你的pom.XML
文件中添加Spring Boot的Redis starter依赖:
<dependencies> <!-- Spring Boot Redis Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- 连接池管理 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> </dependencies>
2. 配置Redis
在application.properties
或application.yml
中配置Redis的连接信息:
# application.properties 示例 spring.redis.host=localhost spring.redis.port=6379 spring.redis.password=yourpassword # 如果你的Redis设置了密码
或者使用YAML格式:
# application.yml 示例 spring: redis: www.devze.com host: localhost port: 6379 password: yourpassword # 如果你的Redis设置了密码
3. 使用RedisTemplate操作Redis
RedisTemplate
是Spring Data Redis中用于操作Redis的一个模板类,它提供了丰富的API来操作Redis。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemphpplate; import org.springframework.data.redis.core.ValueOperations; @Service public class RedisService { @Autowired private StringRedisTemplate stringRedisTemplate; // 用于操作String类型的数据 @Autowired private RedisTemplate<Object, Object> redisTemplate; // 用于操作Object类型的数据,可以存储任何类型的数据,需要手动转换类型等操作。 public void setKeyValue(String key, String value) { ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue(); opsForValue.set(key, value); } public String getKeyValue(String key) { return stringRedisTemplate.opsForValue().get(key); } }
增删改查操作
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service public class RedisService { @Autowired private RedisTemplate<String, Object> redisTemplate; // 存储数据 public void set(String kjavascriptey, Object value) { redisTemplate.opsForValue().set(key, value); } // 存储数据并设置过期时间 public void setWithExpire(String key, Object value, long timeout, TimeUnit unit) { redisTemplate.opsForValue().set(key, value, timeout, unit); } // 获取数据 public Object get(String key) { return redisTemplate.opsForValue().get(key); } // 删除数据 public void delete(String key) { redisTemplate.delete(key); } }
创建临时数据并自动删除,通过设置过期时间,可以创建临时数据。当数据过期时,Redis 会自动删除它们。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service public class RedisService { @Autowired private RedisTemplate<String, Object> redisTemplate; // 创建临时数据并设置过期时间 public void createTemporaryData(String key, Object value, long timeout, TimeUnit unit) { redisTemplate.opsForValue().set(key, value, timeout, unit); } }
例如
rediswww.devze.comTemplate.opsForValue().set("exampleKey", "exampleValue", 10, TimeUnit.SECONDS);
在这个示例中,“exampleKey"将被设置为"exampleValue”,并且这个键值对将在10秒后过期。
4. 使用Jackson2JsonRedisSerializer(可选)自定义序列化方式(存储对象)
如果你需要存储Java对象而不是简单的字符串,你可以使用Jackson2JsonRedisSerializer
来序列化和反序列化对象。
import com.fasterxml.jackson.databind.ObjectMapper; 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; @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConn编程客栈ectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(com.fasterxml.jackson.annotation.PropertyAccessor.ALL, com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY); // 所有字段全部可见,包括私有字段等。根据实际情况调整。 serializer.setObjectMapper(om); // 设置序列化工具类。也可以自定义序列化工具类。例如:new CustomObjectMapper()。 具体根据需要选择合适的序列化工具类。例如:new CustomObjectMapper()。具体根据需要选择合适的序列化工具类。例如:new CustomObjectMapper()。具体根据需要选择合适的序列化工具类。例如:new CustomObjectMapper()。具体根据需要选择合适的序列化工具类。例如:new CustomObjectMapper()。具体根据需要选择合适的序列化工具类。例如:new CustomObjectMapper()。具体根据需要选择合适的序列化工具类。例如:new CustomObjectMapper()。具体根据需要选择合适的序列化工具类
到此这篇关于java org.springframework.boot 对redis操作方法的文章就介绍到这了,更多相关java org.springframework.boot redis操作内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论