回到顶部(go to top)html
C语言编写的高性能键值对数据,支持的键值数据类型:python
Redis的应用场景:c++
1 环境准备 #官网下载 redis 3.2.5版本 wget http://download.redis.io/releases/redis-4.0.1.tar.gz #安装 C 编译环境 yum -y install cpp binutils glibc glibc-kernheaders glibc-common glibc-devel gcc gcc-c++ 2 安装 解压安装包后,进入文件目录编译,编译结束时,会提示 Hint: It's a good idea to run 'make test' ,建议在安装前先测试预安装下,make test预安装后,遇到错误:You need tcl 8.5 or newer in order to run the Redis test ,缺失安装包tcl,因此须要先安装这个 安装包后再次运行 make test,正常后再进行redis安装。 详细步骤以下: #解压二进制包 tar -zvxf /opt/redis-3.2.5 #进入到文件目录 cd redis-3.2.5 #编译 make #测试安装(稍微耗费点时间) make test #可能会提醒须要安装最新版的tcl #yum install tcl #指定路径安装 make PREFIX=/usr/local/redis install
安装结束后,进入到安装路径中,github
[root@bogon bin]# ls -lh /usr/local/redis/bin/web
total 22Mredis
-rwxr-xr-x. 1 root root 2.4M Aug 13 18:40 redis-benchmark算法
-rwxr-xr-x. 1 root root 5.5M Aug 13 18:40 redis-check-aofsql
-rwxr-xr-x. 1 root root 5.5M Aug 13 18:40 redis-check-rdbmongodb
-rwxr-xr-x. 1 root root 2.5M Aug 13 18:40 redis-cli
lrwxrwxrwx. 1 root root 12 Aug 13 18:40 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 5.5M Aug 13 18:40 redis-server
这几个指令用途分别是:
1 #拷贝conf文件到/etc目录 2 cp /opt/redis/redis-4.0.1/redis.conf /etc/redis.conf 3 4 5 #redis.conf 参数说明 6 7 ################################## NETWORK ##################################### 8 9 #绑定的主机地址 10 bind 127.0.0.1 11 12 #保护模式,是否容许 没有认证配置的主机或接口链接redis,默认是启动保护模式,则不容许这种状况 13 protected-mode yes 14 15 #指定redis的监听端口,默认端口是6379,做者在本身的一篇博文中解释了为何选用6379做为默认端口,由于6379在手机按键上MERZ对应的号码,而MERZ取自意大利歌女Alessia Merz的名字,嗯,你开发的,你说了算。 16 port 6379 17 18 # In high requests-per-second environments you need an high backlog in order 19 # to avoid slow clients connections issues. Note that the Linux kernel 20 # will silently truncate it to the value of /proc/sys/net/core/somaxconn so 21 # make sure to raise both the value of somaxconn and tcp_max_syn_backlog 22 # in order to get the desired effect. 23 24 tcp-backlog 511 25 26 #客户端连接多长时间后关闭连接,单位是秒,指定为0,则表示关闭该功能 27 timeout 0 28 29 # A reasonable value for this option is 300 seconds, which is the new 30 # Redis default starting with Redis 3.2.1. 31 tcp-keepalive 300 32 33 ################################# GENERAL ##################################### 34 35 #Redis默认不是以守护进程的方式运行,能够经过该配置项修改,使用yes启用守护进程 36 daemonize yes 37 38 # If you run Redis from upstart or systemd, Redis can interact with your 39 # supervision tree. Options: 40 # supervised no - no supervision interaction 41 # supervised upstart - signal upstart by putting Redis into SIGSTOP mode 42 # supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET 43 # supervised auto - detect upstart or systemd method based on 44 # UPSTART_JOB or NOTIFY_SOCKET environment variables 45 # Note: these supervision methods only signal "process is ready." 46 # They do not enable continuous liveness pings back to your supervisor. 47 supervised no 48
1 #服务端启动 2 [root@bogon redis-4.0.1]# cd /usr/local/redis/ 3 [root@bogon redis]# ./bin/redis-server /etc/redis.conf 4 74537:C 13 Aug 18:53:30.774 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5 74537:C 13 Aug 18:53:30.774 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=74537, just started 6 74537:C 13 Aug 18:53:30.774 # Configuration loaded 7 [root@bogon redis]# ps axu | grep redis 8 root 74538 0.6 0.2 145248 2168 ? Ssl 18:53 0:00 ./bin/redis-server 127.0.0.1:6379 9 root 74665 0.0 0.0 112648 968 pts/4 S+ 18:53 0:00 grep --color=auto redis 10 11 #客户端启动 12 redis-cli [-h 127.0.0.1] [-p 6379] 13 127.0.0.1:6379> ping 14 PONG 15 16 #存储键值对 17 127.0.0.1:6379> set name imooc 18 OK 19 20 #获取name对应的value 21 127.0.0.1:6379> get name 22 "imooc" 23 24 #获取全部keys 25 127.0.0.1:6379> keys * 26 1) "name" 27 28 #删除keys 29 127.0.0.1:6379> del name 30 (integer) 1 31 127.0.0.1:6379> get name 32 (nil) 33 34 #关闭服务端 35 redis-cli shutdow
redis有支持很是多种语言编写的客户端,能够从官网查看 https://redis.io/clients ,redis-py是redis官方网站首选的python客户端开发包,本人只会点点python,因此从这个入门。
redis-py的github地址:https://github.com/andymccurdy/redis-py
键值对种key的注意事项:
string中的一个key对应一个value,values最长可达512Mb。
string经常使用命令:
hash能够存储多个键值对之间的映射,它就像是一个迷你型的redis。
lish的顺序是按照插入的顺序,能够在头部跟尾部插入数据,若是是在list的两头进行操做,那么效率是很高的,可是若是在list中,则会耗费必定时间。
list的类型:
list经常使用的命令:
和list类型不一样的是,set集合中不容许出现重复的元素,set最大能够包含的元素是 4294967295 。注意,set中是没有顺序的。
用于维护用户对象的惟一性,以及处理数据对象之间的关联关系,能够进行并集交集差集运算。好比购买A产品的用户ID,放在一个set中,购买另一个B产品的用户ID,放在另一个set中,这样就很方便计算同时购买两个产品的用户等。
list经常使用指令:
sorted set跟set是比较相似的,集合中不容许出现重复的元素,那么有啥区别呢?sorted set有顺序,从小到大排序,更新操做很是快,访问数据也很是高效。
应用场景:游戏排名、微博热点
经常使用命令:
redis最多支持16个数据,下标0-15表示第几个数据库。默认是在0号数据。切换数据库能够经过select dbnumber 来切换,也能够经过move 来移动key从当前数据到指定的数据库。
事务的指令:multi、exec、discard。redis中,若是某个命令执行失败,后面的命令还会继续执行。multi,开启事务,这个指令后的指令默认为在同一个事务内,exec等同于提交,discard等同于回滚。
redis的高性能是由于数据都在内存中,若是数据库重启,则全部数据都会丢失,那么如何进行数据持久化呢?