Spring模版类 Class RedisTemplate<K,V>

文档地址 https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/RedisTemplate.html

Class RedisTemplate<K,V>

Type Parameters:

  • K - the Redis key type against which the template works (usually a String)
  • V - the Redis value type against which the template works

文档介绍

public class RedisTemplate<K,V> extends RedisAccessor implements RedisOperations<K,V>, BeanClassLoaderAware

简化 Redis 数据访问代码的辅助类。
Helper class that simplifies Redis data access code.

在给定对象和 Redis 存储中的底层二进制数据之间执行自动序列化/反序列化。默认情况下,它使用 Java 序列化方法(通过 JdkSerializationRedisSerializer )。对于字符串密集型操作,请考虑使用专用的 StringRedisTemplate。
Performs automatic serialization/deserialization between the given objects and the underlying binary data in the Redis store. By default, it uses Java serialization for its objects (through JdkSerializationRedisSerializer ). For String intensive operations consider the dedicated StringRedisTemplate.

核心方法是 execute(RedisCallback),支持实现 RedisCallback 接口的 Redis 访问代码。它提供 RedisConnection 处理,使得 RedisCallback 实现和调用代码都无需明确关心检索/关闭 Redis 连接,或处理 Connection 生命周期异常。对于典型的单步操作,有各种便捷方法。
The central method is execute(RedisCallback), supporting Redis access code implementing the RedisCallback interface. It provides RedisConnection handling such that neither the RedisCallback implementation nor the calling code needs to explicitly care about retrieving/closing Redis connections, or handling Connection lifecycle exceptions. For typical single step actions, there are various convenience methods.

配置完成后,此类是线程安全的。
Once configured, this class is thread-safe.

请注意,虽然模板是泛型的,但序列化器/反序列化器仍负责将给定的对象正确地转换为二进制数据。
Note that while the template is generified, it is up to the serializers/deserializers to properly convert the given Objects to and from binary data.

这是 Redis 支持的核心类。
This is the central class in Redis support.

posted @ 2025-04-11 14:46  kuki'  阅读(18)  评论(0)    收藏  举报