Jedis

  Jedis是Redis官方推出的一款面向Java的客户端,提供了很多接口供Java语言调用。可以在Redis官网下载,当然还有一些开源爱好者提供的客户端,如Jredis、SRP等等,推荐使用Jedis。

Spring Data Redis

  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类型的数据操作

简单案例:

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>parent_demo</artifactId>
  7. <groupId>cn.zy.demo</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11.  
  12. <artifactId>spring_data_redis_demo</artifactId>
  13.  
  14. <!-- 集中定义依赖版本号 -->
  15. <properties>
  16. <spring.version>4.2.4.RELEASE</spring.version>
  17. </properties>
  18.  
  19. <dependencies>
  20. <!-- Spring -->
  21. <dependency>
  22. <groupId>org.springframework</groupId>
  23. <artifactId>spring-context</artifactId>
  24. <version>${spring.version}</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework</groupId>
  28. <artifactId>spring-beans</artifactId>
  29. <version>${spring.version}</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework</groupId>
  33. <artifactId>spring-webmvc</artifactId>
  34. <version>${spring.version}</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework</groupId>
  38. <artifactId>spring-jdbc</artifactId>
  39. <version>${spring.version}</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework</groupId>
  43. <artifactId>spring-aspects</artifactId>
  44. <version>${spring.version}</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework</groupId>
  48. <artifactId>spring-jms</artifactId>
  49. <version>${spring.version}</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>org.springframework</groupId>
  53. <artifactId>spring-context-support</artifactId>
  54. <version>${spring.version}</version>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework</groupId>
  58. <artifactId>spring-test</artifactId>
  59. <version>${spring.version}</version>
  60. </dependency>
  61. <dependency>
  62. <groupId>junit</groupId>
  63. <artifactId>junit</artifactId>
  64. <version>4.9</version>
  65. </dependency>
  66.  
  67. <!-- redis做缓存 -->
  68. <dependency>
  69. <groupId>redis.clients</groupId>
  70. <artifactId>jedis</artifactId>
  71. <version>2.8.1</version>
  72. </dependency>
  73. <dependency>
  74. <groupId>org.springframework.data</groupId>
  75. <artifactId>spring-data-redis</artifactId>
  76. <version>1.7.2.RELEASE</version>
  77. </dependency>
  78.  
  79. </dependencies>
  80.  
  81. <build>
  82. <plugins>
  83. <!-- java编译插件 -->
  84. <plugin>
  85. <groupId>org.apache.maven.plugins</groupId>
  86. <artifactId>maven-compiler-plugin</artifactId>
  87. <version>3.2</version>
  88. <configuration>
  89. <source>1.7</source>
  90. <target>1.7</target>
  91. <encoding>UTF-8</encoding>
  92. </configuration>
  93. </plugin>
  94.  
  95. </plugins>
  96.  
  97. </build>
  98. </project>

resources文件:

redis-config.properties

  1. # Redis settings
  2. # 服务器 IP
  3. redis.host=192.168.44.32
  4. # 端口
  5. redis.port=6379
  6. # 密码
  7. redis.pass=
  8. # 使用的dbIndex
  9. redis.database=0
  10. # 最大空闲数
  11. redis.maxIdle=300
  12. # 连接时的最大等待毫秒数
  13. redis.maxWait=3000
  14. # 在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的;
  15. redis.testOnBorrow=true

applicationContext-redis.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9.  
  10. <context:property-placeholder location="classpath*:properties/*.properties"/>
  11.  
  12. <!-- redis 相关配置 -->
  13. <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  14. <property name="maxIdle" value="${redis.maxIdle}"/>
  15. <property name="maxWaitMillis" value="${redis.maxWait}"/>
  16. <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
  17. </bean>
  18.  
  19. <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
  20. p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:database="${redis.database}"
  21. p:pool-config-ref="poolConfig"/>
  22.  
  23. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  24. <property name="connectionFactory" ref="JedisConnectionFactory"/>
  25. </bean>
  26.  
  27. </beans>

连接Redis-Cluster的配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!-- 加载配置属性文件 -->
  10. <context:property-placeholder ignore-unresolvable="true" location="classpath:properties/redis-cluster-config.properties" />
  11. <bean id="redis-clusterConfiguration" class="org.springframework.data.redis.connection.redis-clusterConfiguration">
  12. <property name="maxRedirects" value="${redis.maxRedirects}"></property>
  13. <property name="clusterNodes">
  14. <set>
  15. <bean class="org.springframework.data.redis.connection.redis-clusterNode">
  16. <constructor-arg name="host" value="${redis.host1}"></constructor-arg>
  17. <constructor-arg name="port" value="${redis.port1}"></constructor-arg>
  18. </bean>
  19. <bean class="org.springframework.data.redis.connection.redis-clusterNode">
  20. <constructor-arg name="host" value="${redis.host2}"></constructor-arg>
  21. <constructor-arg name="port" value="${redis.port2}"></constructor-arg>
  22. </bean>
  23. <bean class="org.springframework.data.redis.connection.redis-clusterNode">
  24. <constructor-arg name="host" value="${redis.host3}"></constructor-arg>
  25. <constructor-arg name="port" value="${redis.port3}"></constructor-arg>
  26. </bean>
  27. <bean class="org.springframework.data.redis.connection.redis-clusterNode">
  28. <constructor-arg name="host" value="${redis.host4}"></constructor-arg>
  29. <constructor-arg name="port" value="${redis.port4}"></constructor-arg>
  30. </bean>
  31. <bean class="org.springframework.data.redis.connection.redis-clusterNode">
  32. <constructor-arg name="host" value="${redis.host5}"></constructor-arg>
  33. <constructor-arg name="port" value="${redis.port5}"></constructor-arg>
  34. </bean>
  35. <bean class="org.springframework.data.redis.connection.redis-clusterNode">
  36. <constructor-arg name="host" value="${redis.host6}"></constructor-arg>
  37. <constructor-arg name="port" value="${redis.port6}"></constructor-arg>
  38. </bean>
  39. </set>
  40. </property>
  41. </bean>
  42. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  43. <property name="maxIdle" value="${redis.maxIdle}" />
  44. <property name="maxTotal" value="${redis.maxTotal}" />
  45. </bean>
  46. <bean id="jeidsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
  47. <constructor-arg ref="redis-clusterConfiguration" />
  48. <constructor-arg ref="jedisPoolConfig" />
  49. </bean>
  50. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  51. <property name="connectionFactory" ref="jeidsConnectionFactory" />
  52. </bean>
  53. </beans>

redis-cluster-config.properties

  1. #cluster configuration
  2. redis.host1=192.168.25.140
  3. redis.port1=7001
  4.  
  5. redis.host2=192.168.25.140
  6. redis.port2=7002
  7.  
  8. redis.host3=192.168.25.140
  9. redis.port3=7003
  10.  
  11. redis.host4=192.168.25.140
  12. redis.port4=7004
  13.  
  14. redis.host5=192.168.25.140
  15. redis.port5=7005
  16.  
  17. redis.host6=192.168.25.140
  18. redis.port6=7006
  19.  
  20. redis.maxRedirects=3
  21. redis.maxIdle=100
  22. redis.maxTotal=600

测试:

  1. /**
  2. * 值类型操作
  3. */
  4. @RunWith(SpringJUnit4ClassRunner.class)
  5. @ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
  6. public class TestValue {
  7.  
  8. @Autowired
  9. private RedisTemplate redisTemplate;
  10.  
  11. @Test
  12. public void setValue() throws Exception {
  13. redisTemplate.opsForValue().set("name", "tom");
  14. redisTemplate.boundValueOps("age").set("22", 10, TimeUnit.SECONDS);//带过期时间的
  15. }
  16.  
  17. @Test
  18. public void getValue() throws Exception {
  19. String age = (String) redisTemplate.opsForValue().get("age");
  20. String name = (String) redisTemplate.boundValueOps("name").get();
  21. System.out.println(name);
  22. }
  23.  
  24. @Test
  25. public void delete() throws Exception {
  26. redisTemplate.delete("name");
  27. }
  28. }
  1. /**
  2. * set类型操作
  3. *@Param
  4. *@return
  5. */
  6. @RunWith(SpringJUnit4ClassRunner.class)
  7. @ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
  8. public class TestSet {
  9. @Autowired
  10. private RedisTemplate redisTemplate;
  11. //set1:['','']
  12. @Test
  13. public void setValue() throws Exception {
  14. redisTemplate.boundSetOps("set1").add("刘备");
  15. redisTemplate.boundSetOps("set1").add("曹操");
  16. redisTemplate.boundSetOps("set1").add("孙权");
  17. }
  18.  
  19. @Test
  20. public void getValue() throws Exception {
  21. Set set = redisTemplate.boundSetOps("set1").members();
  22. System.out.println(set);
  23. }
  24.  
  25. @Test
  26. public void remove() throws Exception {
  27. redisTemplate.boundSetOps("set1").remove("曹操");
  28. }
  29.  
  30. @Test
  31. public void delete() throws Exception {
  32. redisTemplate.delete("set1");
  33. }
  34. }
  1. /**
  2. * list类型操作
  3. */
  4. @RunWith(SpringJUnit4ClassRunner.class)
  5. @ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
  6. public class TestList {
  7. @Autowired
  8. private RedisTemplate redisTemplate;
  9.  
  10. // list1 : []
  11.  
  12. @Test
  13. public void setValue1() throws Exception {
  14. redisTemplate.boundListOps("list1").leftPush("唐僧");
  15. redisTemplate.boundListOps("list1").leftPush("悟空");
  16. redisTemplate.boundListOps("list1").leftPush("八戒");
  17. redisTemplate.boundListOps("list1").leftPush("沙僧");
  18. }
  19.  
  20. @Test
  21. public void getValue1() throws Exception {
  22. List list = redisTemplate.boundListOps("list1").range(0, 10);
  23. System.out.println(list);
  24. }
  25.  
  26. @Test
  27. public void setValue2() throws Exception {
  28. redisTemplate.boundListOps("list2").rightPush("大乔");
  29. redisTemplate.boundListOps("list2").rightPush("小乔");
  30. redisTemplate.boundListOps("list2").rightPush("孙尚香");
  31. }
  32.  
  33. @Test
  34. public void getValue2() throws Exception {
  35. List list = redisTemplate.boundListOps("list2").range(0, 10);
  36. System.out.println(list);
  37. }
  38.  
  39. @Test
  40. public void getByIndex() throws Exception {
  41. String name = (String) redisTemplate.boundListOps("list1").index(1);
  42. System.out.println(name);
  43. }
  44.  
  45. @Test
  46. public void remove() throws Exception {
  47. redisTemplate.boundListOps("list1").remove(2,"八戒");
  48. }
  49.  
  50. }
  1. /**
  2. * hash类型操作
  3. */
  4. @RunWith(SpringJUnit4ClassRunner.class)
  5. @ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
  6. public class TestHash {
  7. @Autowired
  8. private RedisTemplate redisTemplate;
  9. //specList:{key:value}
  10.  
  11. @Test
  12. public void setValue() throws Exception {
  13. redisTemplate.boundHashOps("map1").put("a","刘备");
  14. redisTemplate.boundHashOps("map1").put("b","关羽");
  15. redisTemplate.boundHashOps("map1").put("c","张飞");
  16. }
  17.  
  18. @Test
  19. public void getKeys() throws Exception {
  20. Set set = redisTemplate.boundHashOps("map1").keys();
  21. System.out.println(set);
  22. }
  23.  
  24. @Test
  25. public void getValues() throws Exception {
  26. List list = redisTemplate.boundHashOps("map1").values();
  27. System.out.println(list);
  28. }
  29.  
  30. @Test
  31. public void getValue() throws Exception {
  32. String name = (String) redisTemplate.boundHashOps("map1").get("b");
  33. System.out.println(name);
  34. }
  35.  
  36. @Test
  37. public void remove() throws Exception {
  38. redisTemplate.boundHashOps("map1").delete("c");
  39. }
  40.  
  41. @Test
  42. public void delete() throws Exception {
  43. redisTemplate.delete("map1");
  44. }
  45. }

SpringDataRedis操作Redis简单案例的更多相关文章

  1. Spring-data-redis操作redis cluster

    Redis 3.X版本引入了集群的新特性,为了保证所开发系统的高可用性项目组决定引用Redis的集群特性.对于Redis数据访问的支持,目前主要有二种方式:一.以直接调用jedis来实现:二.使用sp ...

  2. Spring-data-redis操作redis知识汇总

    什么是spring-data-redis spring-data-redis是spring-data模块的一部分,专门用来支持在spring管理项目对redis的操作,使用java操作redis最常用 ...

  3. Spring-data-redis操作redis知识总结

    什么是spring-data-redis spring-data-redis是spring-data模块的一部分,专门用来支持在spring管理项目对redis的操作,使用java操作redis最常用 ...

  4. Java操作redis简单示例

    第一:安装Redis    首先我们要安装Redis,就像我们操作数据库一样,在操作之前肯定要先创建好数据库的环境.    Redis的下载可以百度一下,或者打开下面的下载链接:    https:/ ...

  5. php操作redis简单例子

    <?php //在PHP里操作Redis //Redis就是php的一个功能类 //创建Redis对象 $redis = new Redis(); //链接redis服务器 $redis -&g ...

  6. Redis简单案例(二) 网站最近的访问用户

    我们有时会在网站中看到最后的访问用户.最近的活跃用户等等诸如此类的一些信息.本文就以最后的访问用户为例, 用Redis来实现这个小功能.在这之前,我们可以先简单了解一下在oracle.sqlserve ...

  7. Redis简单案例(三) 连续登陆活动的简单实现

    连续登陆活动,或许大家都不会陌生,简单理解就是用户连续登陆了多少天之后,系统就会送一些礼品给相应的用户.最常见的 莫过于游戏和商城这些.游戏就送游戏币之类的东西,商城就送一些礼券.正值国庆,应该也有不 ...

  8. Redis简单案例(一) 网站搜索的热搜词

    对于一个网站来说,无论是商城网站还是门户网站,搜索框都是有一个比较重要的地位,它的存在可以说是 为了让用户更快.更方便的去找到自己想要的东西.对于经常逛这个网站的用户,当然也会想知道在这里比较“火” ...

  9. Redis简单案例(四) Session的管理

    负载均衡,这应该是一个永恒的话题,也是一个十分重要的话题.毕竟当网站成长到一定程度,访问量自然也是会跟着增长,这个时候, 一般都会对其进行负载均衡等相应的调整.现如今最常见的应该就是使用Nginx来进 ...

随机推荐

  1. Java中BIO,NIO,AIO的理解

    在高性能的IO体系设计中,有几个名词概念常常会使我们感到迷惑不解.具体如下: 1 什么是同步? 2 什么是异步? 3 什么是阻塞? 4 什么是非阻塞? 5 什么是同步阻塞? 6 什么是同步非阻塞? 7 ...

  2. (二)java环境搭建

    Java运行环境的搭建: 什么是JRE,什么是JDK? JRE:(java运行环境)包括jvm(java虚拟机)和java运行的核心类库,如果只是运行java程序,只需安装JRE JDK:(java开 ...

  3. [qt][问题记录] 无法定位程序输入点 _ZdaPvj 于动态链接库 libstdc++-6.dll

    无法定位程序输入点 _ZdaPvj 于动态链接库 libstdc++-6.dll 该问题是没有打包库的问题,之所以出现这个问题的是直接用系统自带的命令行使用qt的windeployqt命令导致提供的库 ...

  4. 【vs2013】使用VS2013打包程序

    如何用 VS 2013 打包 程序? 摘自:http://www.zhihu.com/question/25415940 更多请见摘自. 答案就在这里,想要你的exe独立运行在XP中:1.将平台工具集 ...

  5. Ant入门: Hello World with Apache Ant

    笔者因项目需要进行java程序打包,之前一直使用的最多的打包工具要数fat-jar了.此工具将所有引用的jar包以及源码生成的class一起打到一个包里面,运行程序的时候直接运行命令:java –ja ...

  6. 【转】C# Socket编程(5)使用TCP Socket

    [转自:https://www.cnblogs.com/IPrograming/archive/2012/10/18/CSharp_Socket_5.html] TCP 协议(Transmission ...

  7. NSArray四种遍历方法

  8. 洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver

    传送门 题目大意: n个谷仓 ,每次关闭一个谷仓,问剩下没被关闭的谷仓是 否联通. 题解:并查集+倒序处理 代码: #include<iostream> #include<cstdi ...

  9. Windows Communication Foundation (WCF)和Windows CardSpace的示例程序

    微软公司昨天发布了一个Windows Communication Foundation (WCF)和Windows CardSpace的示例程序包,内容极为丰富,从最简单的Hello World到复杂 ...

  10. RbbitMQ基础知识

    MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消息传递指的是程序之间 ...