这里有新鲜出炉的 Redis 官方指南, 程序狗速度看过来!
Redis Key-Value 数据库
Redis 是一个开源的使用 ANSI C 语言编写, 支持网络, 可基于内存亦可持久化的日志型, Key-Value 数据库, 并提供多种语言的 API.
Redis 是一种 NOSQL 数据库, Key-Value 形式对数据进行存储, 其中数据可以以内存形式存在, 也可以持久化到文件系统. Spring data 对 Redis 进行了很好的封装, 用起来也是十分的得心应手, 接下来通过本文给大家分享 Spring Data 操作 Redis 数据库, 需要的朋友参考下
Redis 是一种 NOSQL 数据库, Key-Value 形式对数据进行存储, 其中数据可以以内存形式存在, 也可以持久化到文件系统. Spring data 对 Redis 进行了很好的封装, 用起来也是十分的得心应手. Redis 是一个开源 (BSD 许可) 的, 内存中的数据结构存储系统, 它可以用作数据库, 缓存和消息中间件. 它支持多种类型的数据结构, 如 字符串 (strings), 散列 (hashes), 列表 (lists), 集合 (sets), 有序集合 (sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间 (geospatial) 索引半径查询. Redis 内置了 复制 (replication),LUA 脚本 (Lua scripting), LRU 驱动事件 (LRU eviction), 事务 (transactions) 和不同级别的 磁盘持久化 (persistence), 并通过 Redis 哨兵 (Sentinel) 和自动 分区 (Cluster) 提供高可用性 (high availability).
1. 系统配置, 如果使用 Maven 进行开发, 只需要在 pom.xml 文件中添加如下配置.
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.1.RELEASE</version>
</dependency>
</dependencies>
为了方面起见可以将 Spring Data 模板配置成 bean 方便在直接使用的地方直接注入.
<bean id="jedisConnFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:use-pool="true"/>
<bean id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnFactory"/>
2. Redis Template 针对不同的需求分类封装了如下操作.
opsForValue() - Operations for working with entries having simple values
opsForList() - Operations for working with entries having list values
opsForSet() - Operations for working with entries having set values
opsForZSet() - Operations for working with entries having ZSet (sorted set) values
opsForHash() - Operations for working with entries having hash values
boundValueOps(K) - Operations for working with simple values bound to a given key
boundListOps(K) - Operations for working with list values bound to a given key
boundSetOps(K) - Operations for working with set values bound to a given key
boundZSet(K) - Operations for working with ZSet (sorted set) values bound to a given key
boundHashOps(K) - Operations for working with hash values bound to a given key
3. 典型操作示例
3.1 Redis Template 注入, 可以直接模板注入, 也可以以 ops 形式注入, 如下示例中对两种方式都进行了说明.
public class Example {
// inject the actual template
@Autowired private RedisTemplate < String,
String > template;
// inject the template as ListOperations
// can also inject as Value, Set, ZSet, and HashOperations
@Resource(name = "redisTemplate") private ListOperations < String,
String > listOps;
public void addLink(String userId, URL url) {
listOps.leftPush(userId, url.toExternalForm());
// or use template directly
template.boundListOps(userId).leftPush(url.toExternalForm());
}
}
3.2 Bound 系列操作示例, Bound 系列操作的优势在于只需要绑定一次, 然后可以进行一个系列的操作, 代码十分精炼.
BoundListOperations<String, Product> mangoOps = redis.boundListOps("solidmango");
Product popped = mangoOps.rightPop();
mangoOps.rightPush(product1);
mangoOps.rightPush(product2);
mangoOps.rightPush(product3);
3.3 Serializer 配置示例, 通常情况下 Key 和 Value 都采用不同的方式进行持久化, 如下示例中 Key 使用 String 进行持久化, Value 使用 Jackson 格式进行持久化.
@Bean public RedisTemplate < String,
Cart > redisTemplate(RedisConnectionFactory rcf) {
RedisTemplate < String,
Cart > redis = new RedisTemplate < String,
Cart > ();
redis.setConnectionFactory(rcf);
redis.setKeySerializer(new StringRedisSerializer());
redis.setValueSerializer(new Jackson2JsonRedisSerializer < Product > (Product.class));
return redis;
}
总结
本文对 Spring Data 操作 Redis 的配置和开发方式进行了详细的分析说明, 配置部分给出了具体的配置方式, 代码示例部分分三种情况给出了具体的解决方案, 希望对大家有所帮助.
来源: http://www.phperz.com/article/18/0128/357926.html