一、SpringDataRedis简介

  Spring-data-redis是spring大家族的一部分,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis,  JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。

  spring-data-redis针对jedis提供了如下功能:

  1. 连接池自动管理,提供了一个高度封装的“RedisTemplate”类
  2. 针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
    ValueOperations:简单K-V操作
    SetOperations:set类型数据操作
    ZSetOperations:zset类型数据操作
    HashOperations:针对map类型的数据操作
    ListOperations:针对list类型的数据操作

二、SpringDataRedis入门案例

2.1 准备工作

  (1)构建Maven工程——SpringDataRedisDemo

  (2)引入Spring相关依赖、引入JUnit依赖

  (3)引入Jedis和SpringDataRedis依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.pinyougou</groupId>
<artifactId>SpringDataRedisDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 集中定义依赖版本号 -->
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
</properties> <dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
<!-- 缓存 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
</dependencies>
</project>

  (4)在src/main/resources下创建properties文件夹,建立redis-config.properties

redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true

  (5)在src/main/resources下创建spring文件夹 ,创建applicationContext-redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath*:properties/redis-config.properties"/> <!--redis相关配置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxWaitMillis" value="${redis.maxWait}"/>
<!--suppress SpringXmlModelInspection -->
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean> <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
</bean>
</beans>

  maxIdle :最大空闲数

  maxWaitMillis:连接时的最大等待毫秒数

  testOnBorrow:在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的;

2.2 值类型操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestValue { @Autowired
private RedisTemplate redisTemplate; @Test
public void setValue() {
redisTemplate.boundValueOps("name").set("zhangsan");
} @Test
public void getValue() {
String str = (String) redisTemplate.boundValueOps("name").get();
System.out.println(str);
} @Test
public void deleteValue() {
redisTemplate.delete("name");
}
}

2.3 set类型操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestSet { @Autowired
private RedisTemplate redisTemplate; /**
* 存入值
*/
@Test
public void setValue() {
redisTemplate.boundSetOps("nameset").add("曹操");
redisTemplate.boundSetOps("nameset").add("刘备");
redisTemplate.boundSetOps("nameset").add("孙权");
} /**
* 提取值
*/
@Test
public void getValue() {
Set members = redisTemplate.boundSetOps(("nameset")).members();
System.out.println(members);//[孙权, 刘备, 曹操]
} /**
* 删除集合中的某一个值
*/
@Test
public void deleteValue() {
redisTemplate.boundSetOps("nameset").remove("孙权");
} /**
* 删除整个集合
*/
@Test
public void deleteAllValue() {
redisTemplate.delete("nameset");
}
}

2.4 List类型操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestList { @Autowired
private RedisTemplate redisTemplate; /**
* 右压栈:后添加的对象排在后边
*/
@Test
public void testSetValue1() {
redisTemplate.boundListOps("nameList1").rightPush("刘备");
redisTemplate.boundListOps("nameList1").rightPush("关羽");
redisTemplate.boundListOps("nameList1").rightPush("张飞");
} /**
* 显示右压栈集合
*/
@Test
public void testGetValue1() {
List list = redisTemplate.boundListOps("nameList1").range(0, 10);
System.out.println(list);//[刘备, 关羽, 张飞]
} /**
* 左压栈:后添加的对象排在前边
*/
@Test
public void testSetValue2() {
redisTemplate.boundListOps("nameList2").leftPush("刘备");
redisTemplate.boundListOps("nameList2").leftPush("关羽");
redisTemplate.boundListOps("nameList2").leftPush("张飞");
} /**
* 显示左压栈集合
*/
@Test
public void testGetValue2() {
List list = redisTemplate.boundListOps("nameList2").range(0, 10);
System.out.println(list);//[张飞, 关羽, 刘备]
} /**
* 查询集合某个元素
*/
@Test
public void testSearchByIndex() {
String str = (String) redisTemplate.boundListOps("nameList1").index(1);
System.out.println(str);//关羽
} /**
* 移除集合某个元素
*/
@Test
public void testRemoveByIndex() {
//1代表移除的个数
redisTemplate.boundListOps("nameList1").remove(1, "关羽");
}
}

2.5 Hash类型操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class TestHash { @Autowired
private RedisTemplate redisTemplate; /**
* 存入值
*/
@Test
public void testSetValue() {
redisTemplate.boundHashOps("namehash").put("a", "唐僧");
redisTemplate.boundHashOps("namehash").put("b", "悟空");
redisTemplate.boundHashOps("namehash").put("c", "八戒");
redisTemplate.boundHashOps("namehash").put("d", "沙僧");
} /**
* 提取所有的KEY
*/
@Test
public void testGetKeys() {
Set keys = redisTemplate.boundHashOps("namehash").keys();
System.out.println(keys);//[a, b, c, d]
} /**
* 提取所有的值
*/
@Test
public void testGetValues() {
List values = redisTemplate.boundHashOps("namehash").values();
System.out.println(values);//[唐僧, 悟空, 八戒, 沙僧]
} /**
* 根据KEY提取值
*/
@Test
public void testGetValueByKey() {
Object o = redisTemplate.boundHashOps("namehash").get("b");
System.out.println(o);//悟空
} /**
* 根据KEY移除值
*/
@Test
public void testRemoveValueByKey() {
redisTemplate.boundHashOps("namehash").delete("c");
}
}

Redis学习笔记(6)——SpringDataRedis入门的更多相关文章

  1. (转)redis 学习笔记(1)-编译、启动、停止

    redis 学习笔记(1)-编译.启动.停止   一.下载.编译 redis是以源码方式发行的,先下载源码,然后在linux下编译 1.1 http://www.redis.io/download 先 ...

  2. Redis学习笔记(二)Redis支持的5种数据类型的总结之String和Hash

    引言 在Redis学习笔记(一)中我们已经会安装并且简单使用Redis了,接下来我们一起来学习下Redis支持的5大数据类型. 简介 Redis是REmote DIctionary Server(远程 ...

  3. redis 学习笔记(6)-cluster集群搭建

    上次写redis的学习笔记还是2014年,一转眼已经快2年过去了,在段时间里,redis最大的变化之一就是cluster功能的正式发布,以前要搞redis集群,得借助一致性hash来自己搞shardi ...

  4. Redis学习笔记~目录

    回到占占推荐博客索引 百度百科 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合). ...

  5. Redis学习笔记4-Redis配置详解

    在Redis中直接启动redis-server服务时, 采用的是默认的配置文件.采用redis-server   xxx.conf 这样的方式可以按照指定的配置文件来运行Redis服务.按照本Redi ...

  6. Redis学习笔记7--Redis管道(pipeline)

    redis是一个cs模式的tcp server,使用和http类似的请求响应协议.一个client可以通过一个socket连接发起多个请求命令.每个请求命令发出后client通常会阻塞并等待redis ...

  7. Redis学习笔记一:数据结构与对象

    1. String(SDS) Redis使用自定义的一种字符串结构SDS来作为字符串的表示. 127.0.0.1:6379> set name liushijie OK 在如上操作中,name( ...

  8. Redis学习笔记之ABC

    Redis学习笔记之ABC Redis命令速查 官方帮助文档 中文版本1 中文版本2(反应速度比较慢) 基本操作 字符串操作 set key value get key 哈希 HMSET user:1 ...

  9. Hadoop学习笔记(1) ——菜鸟入门

    Hadoop学习笔记(1) ——菜鸟入门 Hadoop是什么?先问一下百度吧: [百度百科]一个分布式系统基础架构,由Apache基金会所开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序. ...

  10. Redis学习笔记(二)-key相关命令【转载】

    转自 Redis学习笔记(二)-key相关命令 - 点解 - 博客园http://www.cnblogs.com/leny/p/5638764.html Redis支持的各种数据类型包括string, ...

随机推荐

  1. Linux confluence5.8.10 wiki安装

    选择首先需要java环境 其次需要mysql mysql安装请参考: http://www.cnblogs.com/syuf/p/7818710.html 安装好mysql之后,创建一个库 # mys ...

  2. Web Api 中返回JSON的正确做法(转)

    出处:http://www.cnblogs.com/acles/archive/2013/06/21/3147667.html 在使用Web Api的时候,有时候只想返回JSON:实现这一功能有多种方 ...

  3. UVa 10559 Blocks (DP)

    题意:一排带有颜色的砖块,每一个可以消除相同颜色的砖块,,每一次可以到块数k的平方分数.求最大分数是多少. 析:dp[i][j][k] 表示消除 i ~ j,并且右边再拼上 k 个 颜色等于a[j] ...

  4. linux每天一小步---find命令详解

    1 命令功能 find命令用于搜索指定目录下的文件,并配合参数做出相应的处理. 2 命令语法      find  搜索路径pathname 选项option [-exec -ok -print  执 ...

  5. VMware安装Ubuntu

    1. VMware Ubuntu安装详细过程:http://blog.csdn.net/u013142781/article/details/50529030 2. 怎样在VMware虚拟机中使用安装 ...

  6. git 上传项目

    参考:https://blog.csdn.net/qq_28304687/article/details/69959238?fps=1&locationNum=8 第一部分 初次上传 1.先在 ...

  7. (模拟)Arithmetic Sequence -- HDU -- 5400

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=5400 Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  8. [C#]读取指定路径的配置文件[转]

    ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = @"C:\App.c ...

  9. 开源WebGIS实施方案(三):Shapefile数据导入到PostGIS

    PostGIS新版中提供了一个可视化的工具,用于Shapefile数据的导入和导出,极大的方便了使用者的操作. 创建空间数据库 以具有创建用户权限的账号登录pgAdminIII,连接到数据库 创建一个 ...

  10. NOIP2012BLOCKADE贪心思想证明

    NOIP2012BLOCKADE贪心思想证明 这道题的做法是二分时间并检验这个时间是否可行.检验的方法要用到贪心思想. 对于不能到根结点的军队应该尽量向根结点走. 如果军队A能走到根结点但到根结点后剩 ...