Spring Data操作Redis详解

时间:2022-11-02 22:00:47

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的配置和开发方式进行了详细的分析说明,配置部分给出了具体的配置方式,代码示例部分分三种情况给出了具体的解决方案,希望对大家有所帮助。

Spring Data操作Redis详解的更多相关文章

  1. Spring Boot 之 Redis详解

    Redis是目前业界使用最广泛的内存数据存储. Redis支持丰富的数据结构,同时支持数据持久化. Redis还提供一些类数据库的特性,比如事务,HA,主从库. REmote DIctionary S ...

  2. Spring Data操作Redis时,发现key值出现 &bsol;xac&bsol;xed&bsol;x00&bsol;x05t&bsol;x00&bsol;tb

    原文链接:http://blog.csdn.net/yunhaibin/article/details/9001198 最近在研究redis,以及spring data对redis的支持发现了一个奇怪 ...

  3. spring data jpa使用详解

    https://blog.csdn.net/liuchuanhong1/article/details/52042477 使用Spring data JPA开发已经有一段时间了,这期间学习了一些东西, ...

  4. Spring Data JPA实体详解

    1. Spring Data JPA实体概述 JPA提供了一种简单高效的方式来管理Java对象(POJO)到关系数据库的映射,此类Java对象称为JPA实体或简称实体.实体通常与底层数据库中的单个关系 ...

  5. redis入门到精通系列(四):Jedis--使用java操作redis详解

    (一)前言 如果不把数据库和后端语言联系起来,就起不到数据库应该要起到的作用.Java语言通过JDBC操作mysql,用Jedis操作redis.当然了,java操作redis的方式不止jedis一种 ...

  6. python操作redis详解

    https://www.cnblogs.com/koka24/p/5841826.html

  7. Spring Batch&lpar;4&rpar;&colon; Job详解

    Spring Batch(4): Job详解 2016-03-26 18:46 870人阅读 评论(1) 收藏 举报  分类: Spring(6)  版权声明:本文为博主原创文章,未经博主允许不得转载 ...

  8. Redis详解(二)——AOF

    Redis详解(二)--AOF 前言 RDB 持久化存在一个缺点是一定时间内做一次备份,如果redis意外down掉的话,就会丢失最后一次快照后的所有修改(数据有丢失).对于数据完整性要求很严格的需求 ...

  9. Redis详解(七)——集群

    Redis详解(七)--集群 ​Redis3.0版本之前,可以通过Redis Sentinel(哨兵)来实现高可用 ( HA ),从3.0版本之后,官方推出了Redis Cluster,它的主要用途是 ...

随机推荐

  1. JS中setInterval与setTimeout的区别

    JS里设定延时: 使用SetInterval和设定延时函数setTimeout 很类似.setTimeout 运用在延迟一段时间,再进行某项操作. setTimeout("function& ...

  2. ios打包出来为pkg的处理方法

    Add LSRequiresIPhoneOS YES to your Info.plist The key can be found as Application requires iPhone en ...

  3. piap&period;excel 微软 时间戳转换mssql sql server文件时间戳转换unix 导入mysql

    piap.excel 微软 时间戳转换mssql sql server文件时间戳转换unix 导入mysql 需要不个mssql的sql文件导入mysql.他们的时间戳格式不同..ms用的是自定义的时 ...

  4. cxx-generator JS绑定工具

    第一部分:配置安装环境 cxx-generator是由Zynga工程师贡献的C++代码绑定到js工具.用于将cocos2d-x 的c++代码,生成相应的js绑定代码(由c++写成),然后将这些函数注册 ...

  5. 【一天一道LeetCode】&num;83&period; Remove Duplicates from Sorted List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. 微信小程序开发之搞懂flex布局2——flex container

    容器的概念,是用来包含其它容器(container)和项目(item). flex container——flex容器 A flexbox layout is defined using the fl ...

  7. csharp&colon; Microsoft SqlHelper

    from: Microsoft Data Access Application Block for .NET  https://www.microsoft.com/en-us/download/con ...

  8. Ubuntu Server 13&period;10 安装配置图解教程

    一.Ubuntu Server 13.10系统安装 Ubuntu分为桌面版(desktop)和服务器版(Server),下面为大家介绍服务器版本Ubuntu Server 13.10的详细安装过程. ...

  9. 【剑指offer】顺时针打印矩阵,C&plus;&plus;实现

    原创文章,转载请注明出处! 博客文章索引地址 1.题目 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵,则依次打印出数字1,2,3,4,8,12,16,15,14 ...

  10. Loj 2028 随机序列

    Loj 2028 随机序列 连续的乘号会将序列分成若干个块,块与块之间用加减号连接: \[ (a_1*a_2*...a_i)\pm(a_{i+1}*a_{i+2}*...a_j)\pm... \] 除 ...