Redis入门实战(附源码)
Redis入门实战(附源码)
近期由于需要对Redis进行改造,所以顺便学习了下Redis。这篇文章是自己学习Redis写的小实例,简单易懂,超适合入门。
在网上看了很多Redis的入门文章,要么写的太深入,要么写的Level太高。总之,没有找到真正适合初学者的Redis入门文章。
在此之前,对Redis有必要清楚以下问题:
- Redis是什么?
- Redis解决了什么问题?
- Redis的优势?
- 如何使用Redis?(本文重点)
Redis是什么
首先看官网的定义:
Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。
所以,Redis是一个key-value的内存数据库。不过Redis在生产环境中使用最多的功能是缓存系统。至于其他作用比如数据库和消息中间件,则不会展开。
Redis解决了什么问题
在大型网站技术架构中,缓存系统对减轻数据库的压力,提高请求的执行效率,降低服务器的压力具有重要意义。在生产环境中,Redis作为缓存系统的作用包括:预读取(就是预先读取要访问的数据)、存储访问过的数据以及对写入的数据进行暂时的缓存。以每年的“双十一”为例,整个网站包含了大量的图片等多媒体资源,通常一次请求需要请求多个以及多种类型的数据,如果每次都直接访问服务器,那么淘宝早就挂了(当然没有那么简单,还有很多因素在这里,比如负载均衡等就不展开了)。
相反,如果把每次访问过的数据都缓存起来,这样下次再发起相同的请求时就不需要访问服务器了,直接从缓存中读取就可以,效率极高。
Redis的优势
这块没有专门的研究,这里也只是从官网总结得到的:
- 极快的访问速度:每秒能执行约11万集合,每秒约81000+条记录
- 支持丰富的数据类型:Redis支持大多数常见的数据类型:列表、集合、有序列表、散列表
- 原子操作:原子操作可以保证多个客户端同时访问时获取的是更新后的数据
- 丰富的语言支持(client):目前Redis已支持包括Java、C、Go等46中语言的客户端
Redis入门实战
使用Redis之前需要在系统中安装Redis服务(可以理解为Redis服务端),在Windows平台下可以在Windows的Redis安装文件下载安装,安装成功后可以在任务管理器中就可以看到Redis服务了。注意,只支持64位。
下面开始创建Demo项目,需要如下文件:
pom.xml文件:
<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.rhwayfun</groupId> <artifactId>redis-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>redis-demo</name> <description>redis-demo</description> <properties> <spring.version>3.2.8.RELEASE</spring.version> </properties> <dependencies> <!-- junit依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- Redis客户端jedis依赖 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.0</version> </dependency> <!-- spring-data-redis依赖 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.5.0.RELEASE</version> </dependency> <!-- spring相关 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</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-context</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-web</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-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</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>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> </dependencies></project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
由于需要和Spring整合使用,因此需要添加Spring相关的依赖。
application.xml配置文件:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!--使用注解 --> <context:annotation-config /> <!-- 加载redis配置文件 --> <context:property-placeholder location="classpath:redis.properties" /> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" /> <!-- redis操作模板 --> <bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> </bean></beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
redis参数文件:redis.properties
#redis服务器地址redis.host=127.0.0.1 #redis端口redis.port=6379 #redis安全认证的密码(这里暂不需要)redis.password=123456 #最大空闲数redis.maxIdle=100 #最大活跃数redis.maxActive=300 #最长等待时间redis.maxWait=1000 #从连接池获取连接对象前是否需要进行检验,如果为true,表示需要redis.testOnBorrow=true #最大客户端连接超时时间,单位为毫秒redis.timeout=10000
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
配置文件搞定,开始编写测试代码:
测试基类:BaseTest.java
//指定bean注入的配置文件@ContextConfiguration(locations = {"classpath:application.xml"})//使用标准的JUnit @RunWith 注释运行Spring Test Runner@RunWith(SpringJUnit4ClassRunner.class)public class BaseTest extends AbstractJUnit4SpringContextTests{}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
测试代码:
package com.rhwayfun.redis.test;import java.util.concurrent.TimeUnit;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;/** * * @ClassName: RedisTest * @Description: TODO * @author ZhongCB * @date 2016年8月8日 下午4:52:35 * */public class RedisTest extends BaseTest { @Autowired private RedisTemplate<String, Object> redisTemplate; /** * 测试插入与获取Redis的数据 * @Title: testPutAndGet * @Description: TODO * @throws */ @Test public void testPutAndGet() { redisTemplate.opsForHash().put("user", "name", "rhwayfun"); Object object = redisTemplate.opsForHash().get("user", "name"); System.out.println(object); } /** * 测试Redis作为缓存的例子 * @Title: testCache * @Description: TODO * @throws InterruptedException * @throws */ @Test public void testCache() throws InterruptedException { // 插入一条数据 redisTemplate.opsForHash().put("user", "name", "rhwayfun"); // 设置失效时间为2秒 redisTemplate.expire("user", 2, TimeUnit.SECONDS); Thread.sleep(1000); // 1秒后获取 Object object = redisTemplate.opsForHash().get("user", "name"); System.out.println("1秒后:" + object); Thread.sleep(1000); // 2秒后获取 object = redisTemplate.opsForHash().get("user", "name"); System.out.println("2秒后:" + object); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
运行结果如下:
从以上测试结果可以看出,测试结果正常。
下面附上整个工程的源码:https://github.com/happyxiaofan/redis-demo.git
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.****.net/jiangjunshow
Redis入门实战(附源码)
近期由于需要对Redis进行改造,所以顺便学习了下Redis。这篇文章是自己学习Redis写的小实例,简单易懂,超适合入门。
在网上看了很多Redis的入门文章,要么写的太深入,要么写的Level太高。总之,没有找到真正适合初学者的Redis入门文章。
在此之前,对Redis有必要清楚以下问题:
- Redis是什么?
- Redis解决了什么问题?
- Redis的优势?
- 如何使用Redis?(本文重点)
Redis是什么
首先看官网的定义:
Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。
所以,Redis是一个key-value的内存数据库。不过Redis在生产环境中使用最多的功能是缓存系统。至于其他作用比如数据库和消息中间件,则不会展开。
Redis解决了什么问题
在大型网站技术架构中,缓存系统对减轻数据库的压力,提高请求的执行效率,降低服务器的压力具有重要意义。在生产环境中,Redis作为缓存系统的作用包括:预读取(就是预先读取要访问的数据)、存储访问过的数据以及对写入的数据进行暂时的缓存。以每年的“双十一”为例,整个网站包含了大量的图片等多媒体资源,通常一次请求需要请求多个以及多种类型的数据,如果每次都直接访问服务器,那么淘宝早就挂了(当然没有那么简单,还有很多因素在这里,比如负载均衡等就不展开了)。
相反,如果把每次访问过的数据都缓存起来,这样下次再发起相同的请求时就不需要访问服务器了,直接从缓存中读取就可以,效率极高。
Redis的优势
这块没有专门的研究,这里也只是从官网总结得到的:
- 极快的访问速度:每秒能执行约11万集合,每秒约81000+条记录
- 支持丰富的数据类型:Redis支持大多数常见的数据类型:列表、集合、有序列表、散列表
- 原子操作:原子操作可以保证多个客户端同时访问时获取的是更新后的数据
- 丰富的语言支持(client):目前Redis已支持包括Java、C、Go等46中语言的客户端
Redis入门实战
使用Redis之前需要在系统中安装Redis服务(可以理解为Redis服务端),在Windows平台下可以在Windows的Redis安装文件下载安装,安装成功后可以在任务管理器中就可以看到Redis服务了。注意,只支持64位。
下面开始创建Demo项目,需要如下文件:
pom.xml文件:
<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.rhwayfun</groupId> <artifactId>redis-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>redis-demo</name> <description>redis-demo</description> <properties> <spring.version>3.2.8.RELEASE</spring.version> </properties> <dependencies> <!-- junit依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- Redis客户端jedis依赖 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.0</version> </dependency> <!-- spring-data-redis依赖 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.5.0.RELEASE</version> </dependency> <!-- spring相关 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</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-context</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-web</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-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</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>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> </dependencies></project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
由于需要和Spring整合使用,因此需要添加Spring相关的依赖。
application.xml配置文件:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!--使用注解 --> <context:annotation-config /> <!-- 加载redis配置文件 --> <context:property-placeholder location="classpath:redis.properties" /> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" /> <!-- redis操作模板 --> <bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> </bean></beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
redis参数文件:redis.properties
#redis服务器地址redis.host=127.0.0.1 #redis端口redis.port=6379 #redis安全认证的密码(这里暂不需要)redis.password=123456 #最大空闲数redis.maxIdle=100 #最大活跃数redis.maxActive=300 #最长等待时间redis.maxWait=1000 #从连接池获取连接对象前是否需要进行检验,如果为true,表示需要redis.testOnBorrow=true #最大客户端连接超时时间,单位为毫秒redis.timeout=10000
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
配置文件搞定,开始编写测试代码:
测试基类:BaseTest.java
//指定bean注入的配置文件@ContextConfiguration(locations = {"classpath:application.xml"})//使用标准的JUnit @RunWith 注释运行Spring Test Runner@RunWith(SpringJUnit4ClassRunner.class)public class BaseTest extends AbstractJUnit4SpringContextTests{}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
测试代码:
package com.rhwayfun.redis.test;import java.util.concurrent.TimeUnit;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;/** * * @ClassName: RedisTest * @Description: TODO * @author ZhongCB * @date 2016年8月8日 下午4:52:35 * */public class RedisTest extends BaseTest { @Autowired private RedisTemplate<String, Object> redisTemplate; /** * 测试插入与获取Redis的数据 * @Title: testPutAndGet * @Description: TODO * @throws */ @Test public void testPutAndGet() { redisTemplate.opsForHash().put("user", "name", "rhwayfun"); Object object = redisTemplate.opsForHash().get("user", "name"); System.out.println(object); } /** * 测试Redis作为缓存的例子 * @Title: testCache * @Description: TODO * @throws InterruptedException * @throws */ @Test public void testCache() throws InterruptedException { // 插入一条数据 redisTemplate.opsForHash().put("user", "name", "rhwayfun"); // 设置失效时间为2秒 redisTemplate.expire("user", 2, TimeUnit.SECONDS); Thread.sleep(1000); // 1秒后获取 Object object = redisTemplate.opsForHash().get("user", "name"); System.out.println("1秒后:" + object); Thread.sleep(1000); // 2秒后获取 object = redisTemplate.opsForHash().get("user", "name"); System.out.println("2秒后:" + object); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
运行结果如下:
从以上测试结果可以看出,测试结果正常。
下面附上整个工程的源码:https://github.com/happyxiaofan/redis-demo.git