Quantcast
Channel: CodeSection,代码区,数据库(综合) - CodeSec
Viewing all articles
Browse latest Browse all 6262

Redis入门实战(附源码)

$
0
0
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>

由于需要和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>

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

配置文件搞定,开始编写测试代码:

测试基类:BaseTest.java

//指定bean注入的配置文件 @ContextConfiguration(locations = {"classpath:application.xml"}) //使用标准的JUnit @RunWith 注释运行Spring Test Runner @RunWith(SpringJUnit4ClassRunner.class) public class BaseTest extends AbstractJUnit4SpringContextTests{ }

测试代码:

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); } }

运行结果如下:


Redis入门实战(附源码)

从以上测试结果可以看出,测试结果正常。

下面附上整个工程的源码: https://github.com/happyxiaofan/redis-demo.git


Viewing all articles
Browse latest Browse all 6262

Trending Articles