系统性学习,移步IT-BLOG

一、简介


对于类似于首页这种每天都有大量的人访问,对数据库造成很大的压力,严重时可能导致瘫痪。解决方法:一种是数据缓存、一种是网页静态化。今天就讨论数据缓存的实现

Redis:是一种开源的 Key-Value 数据库,运行在内存中,企业开发通常采用 Redis 来实现缓存。同类的产品还有 memcache、memchached、MongoDB 等。
Jedis:是 Redis 官方推出的一款面向 Java 的客户端,提供了很多接口以供 Java 语言调用。可以在 Redis 官网下载当然还有一些开源爱好者提供的客户端,如:Jredis、SRP 等等,推荐使用 Jedis
Spring Data Redis:是 Spring 大家族的一部分提供了在 Spring 应用中通过简单的配置访问 redis 服务,对 redis 底层开发包(Jedis,JRedis,RPC)进行了高度封装,RedisTemplate 提供了 redis 各种操作,异常处理及序列化,支持发布订阅
Spring-data-Redis 针对 jedis 提供了如下功能:
【1】连接池自动管理,提供了一个高度封装的 “RedisTemplate” 类。
【2】针对 jedis 客户端中大量 api 进行了归类封装,将同一类型操作封装为 operation 接口,例如:
      ValueOperations:简单 K-V 操作
      SetOperations:set 类型数据操作
      ZSetOperationszset 类型数据操作
      HashOperations针对 map 类型的数据操作
      ListOperations针对 list 类型的数据操作

二、需要依赖的 jar 包如下:(jedis 与 spring-data-redis 相关的 jar 包)


  1. 1 <!-- 缓存 -->
  2. 2 <dependency>
  3. 3 <groupId>redis.clients</groupId>
  4. 4 <artifactId>jedis</artifactId>
  5. 5 <version>2.8.1</version>
  6. 6 </dependency>
  7. 7 <dependency>
  8. 8 <groupId>org.springframework.data</groupId>
  9. 9 <artifactId>spring-data-redis</artifactId>
  10. 10 <version>1.7.2.RELEASE</version>
  11. 11 </dependency>

三、Spring Data Redis 入门案例


第一步:创建一个 maven project 项目,以 jar 包的形式创建。引入 Spring 及 Redis 的相关 Jar 包(pom.xml 文件内容如下)

  1. 1 <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4. 4 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. 5 <modelVersion>4.0.0</modelVersion>
  6. 6 <groupId>com.pinyougou-itcast</groupId>
  7. 7 <artifactId>springDataRedis</artifactId>
  8. 8 <version>0.0.1-SNAPSHOT</version>
  9. 9
  10. 10 <!-- 集中定义依赖版本号 -->
  11. 11 <properties>
  12. 12 <spring.version>4.2.4.RELEASE</spring.version>
  13. 13 </properties>
  14. 14
  15. 15 <dependencies>
  16. 16 <!-- Spring -->
  17. 17 <dependency>
  18. 18 <groupId>org.springframework</groupId>
  19. 19 <artifactId>spring-context</artifactId>
  20. 20 <version>${spring.version}</version>
  21. 21 </dependency>
  22. 22 <dependency>
  23. 23 <groupId>org.springframework</groupId>
  24. 24 <artifactId>spring-beans</artifactId>
  25. 25 <version>${spring.version}</version>
  26. 26 </dependency>
  27. 27 <dependency>
  28. 28 <groupId>org.springframework</groupId>
  29. 29 <artifactId>spring-webmvc</artifactId>
  30. 30 <version>${spring.version}</version>
  31. 31 </dependency>
  32. 32 <dependency>
  33. 33 <groupId>org.springframework</groupId>
  34. 34 <artifactId>spring-jdbc</artifactId>
  35. 35 <version>${spring.version}</version>
  36. 36 </dependency>
  37. 37 <dependency>
  38. 38 <groupId>org.springframework</groupId>
  39. 39 <artifactId>spring-aspects</artifactId>
  40. 40 <version>${spring.version}</version>
  41. 41 </dependency>
  42. 42 <dependency>
  43. 43 <groupId>org.springframework</groupId>
  44. 44 <artifactId>spring-jms</artifactId>
  45. 45 <version>${spring.version}</version>
  46. 46 </dependency>
  47. 47 <dependency>
  48. 48 <groupId>org.springframework</groupId>
  49. 49 <artifactId>spring-context-support</artifactId>
  50. 50 <version>${spring.version}</version>
  51. 51 </dependency>
  52. 52 <dependency>
  53. 53 <groupId>org.springframework</groupId>
  54. 54 <artifactId>spring-test</artifactId>
  55. 55 <version>${spring.version}</version>
  56. 56 </dependency>
  57. 57 <!-- 缓存 -->
  58. 58 <dependency>
  59. 59 <groupId>redis.clients</groupId>
  60. 60 <artifactId>jedis</artifactId>
  61. 61 <version>2.8.1</version>
  62. 62 </dependency>
  63. 63 <dependency>
  64. 64 <groupId>org.springframework.data</groupId>
  65. 65 <artifactId>spring-data-redis</artifactId>
  66. 66 <version>1.7.2.RELEASE</version>
  67. 67 </dependency>
  68. 68 </dependencies>
  69. 69 </project>

第二步:启动 redis 服务端,并在项目的 src/main/resource 中创建 properties/redis-config.properties 配置文件

  1. 1 # Redis settings
  2. 2 # server IP
  3. 3 redis.host=192.168.88.131
  4. 4 # server port
  5. 5 redis.port=6379
  6. 6 # server pass
  7. 7 redis.pass=
  8. 8 # use dbIndex
  9. 9 redis.database=0
  10. 10 # \u63A7\u5236\u4E00\u4E2Apool\u6700\u591A\u6709\u591A\u5C11\u4E2A\u72B6\u6001\u4E3Aidle(\u7A7A\u95F2\u7684)\u7684jedis\u5B9E\u4F8B
  11. 11 redis.maxIdle=300
  12. 12 # \u8868\u793A\u5F53borrow(\u5F15\u5165)\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u6700\u5927\u7684\u7B49\u5F85\u65F6\u95F4\uFF0C\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4(\u6BEB\u79D2)\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException\uFF1B
  13. 13 redis.maxWait=3000
  14. 14 # \u5728borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Cvalidate\u64CD\u4F5C\uFF1B\u5982\u679C\u4E3Atrue\uFF0C\u5219\u5F97\u5230\u7684jedis\u5B9E\u4F8B\u5747\u662F\u53EF\u7528\u7684
  15. 15 redis.testOnBorrow=true

第三步:在 src/main/resource 中创建 spring/applicationContext-redis.xml 配置文件

  1. 1 <?xml version="1.0" encoding="UTF-8"?>
  2. 2 <beans xmlns="http://www.springframework.org/schema/beans"
  3. 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. 4 xmlns:context="http://www.springframework.org/schema/context"
  5. 5 xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. 6 xmlns:cache="http://www.springframework.org/schema/cache"
  7. 7 xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. 8 http://www.springframework.org/schema/beans/spring-beans.xsd
  9. 9 http://www.springframework.org/schema/context
  10. 10 http://www.springframework.org/schema/context/spring-context.xsd
  11. 11 http://www.springframework.org/schema/mvc
  12. 12 http://www.springframework.org/schema/mvc/spring-mvc.xsd
  13. 13 http://www.springframework.org/schema/cache
  14. 14 http://www.springframework.org/schema/cache/spring-cache.xsd">
  15. 15
  16. 16 <context:property-placeholder location="classpath*:properties/*.properties" />
  17. 17
  18. 18 <!-- redis 相关配置 -->
  19. 19 <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  20. 20 <property name="maxIdle" value="${redis.maxIdle}" />
  21. 21 <property name="maxWaitMillis" value="${redis.maxWait}" />
  22. 22 <property name="testOnBorrow" value="${redis.testOnBorrow}" />
  23. 23 </bean>
  24. 24
  25. 25 <!-- Spring Data Redis 相关配置 -->
  26. 26 <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
  27. 27 p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
  28. 28
  29. 29 <!-- 在业务配置中主要通过 此模板调用redis 进行数据操作 -->
  30. 30 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  31. 31 <property name="connectionFactory" ref="JedisConnectionFactory" />
  32. 32 </bean>
  33. 33
  34. 34 </beans>

第四步:数据操作之 String 值类型操作

  1. 1 package com.yintong.test;
  2. 2
  3. 3 import org.junit.Test;
  4. 4 import org.junit.runner.RunWith;
  5. 5 import org.springframework.beans.factory.annotation.Autowired;
  6. 6 import org.springframework.data.redis.core.RedisTemplate;
  7. 7 import org.springframework.test.context.ContextConfiguration;
  8. 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9. 9
  10. 10 @RunWith(SpringJUnit4ClassRunner.class)
  11. 11 @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
  12. 12 public class StringDemo {
  13. 13
  14. 14 @Autowired
  15. 15 private RedisTemplate redisTemplate;
  16. 16 @Test
  17. 17 public void setValue() {//redis中设置值
  18. 18 redisTemplate.boundValueOps("name").set("zzx");
  19. 19 }
  20. 20 @Test
  21. 21 public void getValue() {
  22. 22 String name = (String) redisTemplate.boundValueOps("name").get();
  23. 23 System.out.println(name);
  24. 24 }
  25. 25 @Test
  26. 26 public void deleteValue(){
  27. 27 redisTemplate.delete("name");;
  28. 28 }
  29. 29 }

第五步:Set 数据类型操作

  1. 1 package com.yintong.test;
  2. 2
  3. 3 import java.util.Set;
  4. 4
  5. 5 import org.junit.Test;
  6. 6 import org.junit.runner.RunWith;
  7. 7 import org.springframework.beans.factory.annotation.Autowired;
  8. 8 import org.springframework.data.redis.core.BoundSetOperations;
  9. 9 import org.springframework.data.redis.core.RedisTemplate;
  10. 10 import org.springframework.test.context.ContextConfiguration;
  11. 11 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  12. 12
  13. 13 @RunWith(SpringJUnit4ClassRunner.class)
  14. 14 @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
  15. 15 public class SetDemo {
  16. 16 @Autowired
  17. 17 private RedisTemplate redisTemplate;
  18. 18 /**
  19. 19 * 存入值:Bound系列操作示例,Bound系列操作的优势在于只需要绑定一次,然后可以进行一个系列的操作,代码十分精炼。
  20. 20 */
  21. 21 @Test
  22. 22 public void setValue() {//redis中设置值
  23. 23 BoundSetOperations boundSetOps = redisTemplate.boundSetOps("nameSet");
  24. 24 boundSetOps.add("zzx");
  25. 25 boundSetOps.add("love");
  26. 26 boundSetOps.add("fj");
  27. 27 }
  28. 28 /**
  29. 29 * 提取值
  30. 30 */
  31. 31 @Test
  32. 32 public void getValue() {
  33. 33 Set members = redisTemplate.boundSetOps("nameSet").members();
  34. 34 System.out.println(members);
  35. 35 }
  36. 36 /**
  37. 37 * 删除集合中的某一个值
  38. 38 */
  39. 39 @Test
  40. 40 public void deleteValue(){
  41. 41 redisTemplate.boundSetOps("nameset").remove("孙权");
  42. 42 }
  43. 43 /**
  44. 44 * 删除整个集合
  45. 45 */
  46. 46 @Test
  47. 47 public void deleteAllValue(){
  48. 48 redisTemplate.delete("nameset");
  49. 49 }
  50. 50 }

结果展示:

第六步:List 类型操作

  1. 1 package com.yintong.test;
  2. 2
  3. 3 import java.util.List;
  4. 4
  5. 5 import org.junit.Test;
  6. 6 import org.junit.runner.RunWith;
  7. 7 import org.springframework.beans.factory.annotation.Autowired;
  8. 8 import org.springframework.data.redis.core.RedisTemplate;
  9. 9 import org.springframework.test.context.ContextConfiguration;
  10. 10 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  11. 11
  12. 12 @RunWith(SpringJUnit4ClassRunner.class)
  13. 13 @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
  14. 14 public class TestList {
  15. 15 @Autowired
  16. 16 private RedisTemplate redisTemplate;
  17. 17 /**
  18. 18 * 右压栈:后添加的对象排在后边
  19. 19 */
  20. 20 @Test
  21. 21 public void setValueList() {
  22. 22 redisTemplate.boundListOps("nameList").rightPush("zzx");
  23. 23 redisTemplate.boundListOps("nameList").rightPush("fj");
  24. 24 }
  25. 25 /**
  26. 26 * 显示右压栈集合
  27. 27 */
  28. 28 @Test
  29. 29 public void getValueList() {
  30. 30 List range = redisTemplate.boundListOps("nameList").range(0, -1);
  31. 31 System.out.println(range);
  32. 32 }
  33. 33 /**
  34. 34 * 左压栈:后添加的对象排在前边
  35. 35 */
  36. 36 @Test
  37. 37 public void testSetValue2(){
  38. 38 redisTemplate.boundListOps("nameList").leftPush("love");
  39. 39 }
  40. 40 /**
  41. 41 * 查询集合某个元素
  42. 42 */
  43. 43 @Test
  44. 44 public void testSearchByIndex(){
  45. 45 Object index = redisTemplate.boundListOps("nameList").index(1);
  46. 46 System.out.println(index);
  47. 47 }
  48. 48 /**
  49. 49 * 移除集合某个元素
  50. 50 */
  51. 51 @Test
  52. 52 public void testRemoveByIndex(){
  53. 53 redisTemplate.boundListOps("nameList").remove(1, "zzx");
  54. 54 }
  55. 55 }

第七步:Hash 类型操作

  1. 1 package com.yintong.test;
  2. 2
  3. 3 import java.util.List;
  4. 4 import java.util.Set;
  5. 5
  6. 6 import org.junit.Test;
  7. 7 import org.junit.runner.RunWith;
  8. 8 import org.springframework.beans.factory.annotation.Autowired;
  9. 9 import org.springframework.data.redis.core.RedisTemplate;
  10. 10 import org.springframework.test.context.ContextConfiguration;
  11. 11 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  12. 12
  13. 13 @RunWith(SpringJUnit4ClassRunner.class)
  14. 14 @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
  15. 15 public class TestHash {
  16. 16 @Autowired
  17. 17 private RedisTemplate redisTemplate;
  18. 18 //插入值
  19. 19 @Test
  20. 20 public void setValue() {
  21. 21 redisTemplate.boundHashOps("nameHash").put("zzx", "boy");
  22. 22 redisTemplate.boundHashOps("nameHash").put("fj", "girl");
  23. 23 }
  24. 24 //提取所有的KEY
  25. 25 @Test
  26. 26 public void getKey() {
  27. 27 Set keys = redisTemplate.boundHashOps("nameHash").keys();
  28. 28 System.out.println(keys);
  29. 29 }
  30. 30 //获取所有值
  31. 31 @Test
  32. 32 public void getValues() {
  33. 33 List values = redisTemplate.boundHashOps("nameHash").values();
  34. 34 System.out.println(values);
  35. 35 }
  36. 36 @Test
  37. 37 //根据key获取值(常用)
  38. 38 public void getValueByKey() {
  39. 39 Object nameValue = redisTemplate.boundHashOps("nameHash").get("zzx");
  40. 40 System.out.println(nameValue);
  41. 41 }
  42. 42 @Test
  43. 43 //根据key溢出值
  44. 44 public void deleteKey() {
  45. 45 redisTemplate.boundHashOps("nameHash").delete("zzx");
  46. 46 }
  47. 47 }


 ----关注公众号,获取更多内容----

Spring Data Redis 框架的更多相关文章

  1. Spring Data Redis 详解及实战一文搞定

    SDR - Spring Data Redis的简称. Spring Data Redis提供了从Spring应用程序轻松配置和访问Redis的功能.它提供了与商店互动的低级别和高级别抽象,使用户免受 ...

  2. spring mvc Spring Data Redis RedisTemplate [转]

    http://maven.springframework.org/release/org/springframework/data/spring-data-redis/(spring-data包下载) ...

  3. Spring Data Redis简介以及项目Demo,RedisTemplate和 Serializer详解

    一.概念简介: Redis: Redis是一款开源的Key-Value数据库,运行在内存中,由ANSI C编写,详细的信息在Redis官网上面有,因为我自己通过google等各种渠道去学习Redis, ...

  4. spring data redis 理解

    前言 Spring Data Redis project,应用了Spring概念来开发使用键值形式的数据存储的解决方案.我们(官方)提供了一个 "template" ,这是一个高级 ...

  5. Spring Data Redis 让 NoSQL 快如闪电(2)

    [编者按]本文作者为 Xinyu Liu,文章的第一部分重点概述了 Redis 方方面面的特性.在第二部分,将介绍详细的用例.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 把 Redis ...

  6. Spring Data Redis 让 NoSQL 快如闪电 (1)

    [编者按]本文作者为 Xinyu Liu,详细介绍了 Redis 的特性,并辅之以丰富的用例.在本文的第一部分,将重点概述 Redis 的方方面面.文章系国内 ITOM 管理平台 OneAPM 编译呈 ...

  7. spring data redis使用1——连接的创建

    spring data redis集成了几个Redis客户端框架,Jedis , JRedis (Deprecated since 1.7), SRP (Deprecated since 1.7) a ...

  8. Spring Data Redis学习

    本文是从为知笔记上复制过来的,懒得调整格式了,为知笔记版本是带格式的,内容也比这里全.点这里 为知笔记版本 Spring Data Redis 学习 Version 1.8.4.Release 前言 ...

  9. Redis(八):spring data redis 理解

    前言 Spring Data Redis project,应用了Spring概念来开发使用键值形式的数据存储的解决方案.我们(官方)提供了一个 "template" ,这是一个高级 ...

  10. Spring Data Redis整体介绍 (一)

    为什么使用Spring Data Redis 首先Spring Data Redis 是Spring 框架提供的用于操作Redis的客户端. Spring框架是一个全栈Java程序框架,通过DI.AO ...

随机推荐

  1. ffmpeg编译错误/libfdk-aacenc.c: In function 'aac_encode_init'

    ffmpeg编译错误/libfdk-aacenc.c: In function 'aac_encode_init' 需要手动打一个补丁 https://git.libav.org/?p=libav.g ...

  2. PSO 算法的变体python实现

    上演化计算课的时候老师让我们实现EOPSO算法(一种精英反向的粒子群优化算法),下面是他的算法步骤: 首先我们需要知道一些基础知识: (1)基础PSO算法 (2)精英反向解 import numpy ...

  3. IDEA 2018.3.*本地启动tomcat项目无法设置Application context localhost 404

    记录一个开发中遇到的坑,网上找了好久才找到一个能解决的办法,特此转载一下. 旧版的idea启动web项目,在tomcat配置环节,有设置Application content的功能.我们可以设置成&q ...

  4. 2003031126-石升福-python数据分析第三周作业

    项目 Numpy 博客名称 2003031126-石升福-python数据分析第三周作业 课程班级博客链接 https://edu.cnblogs.com/campus/pexy/20sj 作业链接 ...

  5. 解决navicat远程连接MySQL失败,报错1130问题

    1select host from user where user='root'; 2update user set host = '%' where user ='root';3flush priv ...

  6. vue3 技术浏览 收藏

    Vue3教程:Vue3.0 + Vant3.0 搭建种子项目 链接:https://www.cnblogs.com/han-1034683568/p/13875663.html

  7. 递推(dp)纪中真题

    前言: 日月如梭,光阴似箭.大家好,我盛艺承又回来了.今天给大家讲一下纪中的DP(递推)真题. 题目描述 在网格中取一个N x 1的矩形,并把它当作一个无向图.这个图有2(N+1)个顶点,有3(N-1 ...

  8. P2330 繁忙的都市

    题目描述 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条 ...

  9. 关于数据传递 json

    关于这几种语言的json 操作 Lua local cjson2 = require "cjson" local lua_object = { ["name"] ...

  10. Javaheima21

    Java 学习内容 XML XML解析技术 XPath 设计模式 在有些业务场景下,存储数据或者传输数据给别人的时候,数据需要满足优点的规范进行组织 XML文件存储的数据需要提取出来 如何方便的在XM ...