那就开始吧,直入主题。
1、什么是redis?
Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库。
Redis 与其他 key - value 缓存产品有以下三个特点:
- Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
- Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
- Redis支持数据的备份,即master-slave模式的数据备份。
优势:
- 性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s 。
- 丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。
- 原子 – Redis的所有操作都是原子性的,意思就是要么成功执行要么失败完全不执行。单个操作是原子性的。多个操作也支持事务,即原子性,通过MULTI和EXEC指令包起来。
- 丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性。
2、redis的安装
2.1、redis的下载。
官网地址:http://redis.io/
下载地址:http://download.redis.io/releases/redis-3.0.0.tar.gz
2.2、redis的安装
安装到linux系统中。我直接安装到阿里云服务器。
第一步:将redis的压缩包,上传到linux系统
第二步:对redis的压缩包进行解压缩
Redis解压缩之后的文件是用c语言写的源码文件,所以需要你当前的系统有c语言环境。
[root@itheima ~]# tar -zxf redis-3.0.0.tar.gz
第三步:编译redis源码
[root@itheima ~]# cd redis-3.0.0
[root@itheima redis-3.0.0]# make
第四步:安装redis(安装到/usr/local/redis目录下,没有redis目录下面这个操作会自动创建)
[root@itheima redis-3.0.0]# make install PREFIX=/usr/local/redis
第五步:查看有没有安装成功。(进入到/usr/local/redis目录下,如果看到有个bin目录,则说明安装成功)
2.3、启动redis
2.3.1、前端启动
[root@itheima bin]# ./redis-server
启动成功后如下所示。
前端启动的关闭:
强制关闭:Ctrl+c
正常关闭:[root@itheima bin]# ./redis-cli shutdown
前端启动的缺点:一旦客户端关闭,redis服务也随之停掉。
2.3.2、后端启动
第一步:需要将redis解压之后的源码包中的redis.conf文件拷贝到bin目录下
[root@itheima bin]# cp /root/redis-3.0.0/redis.conf ./
第二步:修改redis.conf文件,将daemonize (是否要改为守护线程的方式启动)改为yes
先要使用vim redis.conf
第三步:后端启动redis
[root@itheima bin]# ./redis-server redis.conf
第四步:查看是否启动成功
命令:ps -aux | grep redis
后端启动关闭的方式:
强制关闭:[root@itheima bin]# kill -9 4118(redis服务进程号)
正常关闭:[root@itheima bin]# ./redis-cli shutdown
注意:
在项目中,建议使用正常关闭。 因为redis作为缓存来使用的话,将数据存储到内存中,如果使用正常关闭,则会将内存数据持久化到本地之后,再关闭。如果是强制关闭,则不会进行持久化操作,可能会造成部分数据的丢失。
3、redis客户端
3.1、redis自带的客户端
3.1.1、 启动客户端命令:[root@itheima bin]# ./redis-cli -h 127.0.0.1 -p 6379
-h:指定访问的redis服务器的ip地址
-p:指定访问的redis服务器的port端口
还可以写成:[root@itheima bin]# ./redis-cli
使用默认配置:默认的ip【127.0.0.1】,默认的port【6379】
3.1.2、关闭客户端:Ctrl+c
127.0.0.1:6379> quit
3.2、图形界面客户端。
安装一个图形界面客户端。这个步骤就自行百度了啊。安装好后大概就如下所示。
从图形化界面可以看出redis是数据库数量是默认16个
当然这个数据库数量是可以改的,在redis.conf配置文件中,
选择数据库的方式:
使用select 加上数据库的下标 就可以选择指定的数据库来使用,下标从0开始
下标为0 的数据库,并不会显示出类似于图中[15]的下标。
3.3、Jedis客户端。
redis不仅是使用命令来操作,现在主流的语言都有客户端支持,比如java,C,C++,php,Go等。
java客户端有很多种,Jedis,Redisson,Jredis,JDBC-Redis等。其中官方推荐的是Jedis和Redisson,在企业中用的比较多的就是Jedis. Jedis是脱管到github上的,地址:https://github.com/xetorthio/jedis
3.3.1、单实例连接redis
3.3.1.1、导包(只需要导付下两个jar包即可)
3.3.1.2、代码测试
3.3.2、使用Jedis连接池连接redis
3.3.3、spring整合Jedis。
所需jar包:
spring配置文件:applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="30" />
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="10" />
<!-- 每次释放连接的最大数目 -->
<property name="numTestsPerEvictionRun" value="1024" />
<!-- 释放连接的扫描间隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<!-- 连接最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在获取连接的时候检查有效性, 默认false -->
<property name="testOnBorrow" value="false" />
<!-- 在空闲时检查有效性, 默认false -->
<property name="testWhileIdle" value="true" />
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<!-- redis单机 通过连接池 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool"
destroy-method="close">
<constructor-arg name="poolConfig" ref="jedisPoolConfig" />
<constructor-arg name="host" value="39.105.2.100" />
<constructor-arg name="port" value="6379" />
</bean>
</beans>
代码测试:
好了 ,今天到此为止,明天继续。
希望此文能给像我一样初学redis的你带来一点儿帮助。