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: 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.stringredistemplate; 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 key, 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); } }
例如
redistemplate.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 redisconnectionfactory) { 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操作内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论