一、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. memcache can't run as root without the -u switch

    memcached是一款高速.分布式的内存缓存系统.其官方主页在http://www.danga.com/memcached/1.安装前的准备要安装memcached,需要有libevent的支持.c ...

  2. Java的类名与文件名必须一致

    1.Java保存的文件名必须与类名一致:2.如果文件中只有一个类,文件名必须与类名一致:3.一个Java文件中只能有一个public类:4.如果文件中不止一个类,文件名必须与public类名一致:5. ...

  3. ubuntu 出来菜单栏和任务栏

    http://blog.csdn.net/terence1212/article/details/51340595 命令行输入:sudo apt-get install compizconfig-se ...

  4. FPGA时序约束和timequest timing analyzer

    FPGA时序约束 时钟约束 #************************************************************** # Create Clock #****** ...

  5. msfconsole邮件收集器模块

    msfconsole search email collector use auxiliary/gather/search_email_collector show options 下面我们设置域名. ...

  6. RabbitMQ client ( java )

    Maven 依赖 <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-clien ...

  7. Spring AOP 自定义注解获取http接口及WebService接口入参和出参

    注解方法实现过程中可以采用如下获取方式:—以下为例  HttpServletRequest request = ((ServletRequestAttributes) RequestContextHo ...

  8. 关于 Kafka offset

    查询topic的offset的范围 用下面命令可以查询到topic:Mytopic broker:SparkMaster:9092的offset的最小值: bin/kafka-run-class.sh ...

  9. backbone.js 学习笔记

    Backbone.Model 模型.相当于表定义,定义一个表当中有的列 defaults:设置属性的默认值 initialize():初始化函数 get(key):获取属性值 set(data):设置 ...

  10. [ruby]rubyGem出现ERROR: Could not find a valid gem时的处理方法

    场景: 想安装SASS的时候,打开cmd,输入gem install sass的时候却出现了: ERROR:  Could not find a valid gem 'sass' (>= 0), ...