sudo apt-get install redis-server # ubuntu命令安装redis服务
ubuntu通过上面命令安装完redis,会自动启动redis服务,通过ps命令确认:
wxncom@wxncom-virtual-machine:~$ ps -ef | grep redis
redis 6594 1 0 16:28 ? 00:00:00 /usr/bin/redis-server 127.0.0.1:6379
wxncom 7003 1827 0 16:29 pts/0 00:00:00 grep --color=auto redis
可以看到redis默认工作在本地主机的6379端口上。
而mysql默认运行在3306端口上.
redis支持多种数据结构,如:
字符串、list列表、set集合、map映射表等结构。
启动redis-cli客户端,连接redis server体验一下数据缓存功能,如下:
redis存储普通key-value:
wxncom@wxncom-virtual-machine:~$ redis-cli
s127.0.0.1:6379> set "abc" "hello world!"
OK
127.0.0.1:6379> get "abc"
"hello world!"
127.0.0.1:6379>
subscribe 13 // 订阅通道
publish 13 "message" //向某个通道发送消息
redis支持多种不同的客户端编程语言,例如Java对应jedis、php对应phpredis、C++对应的则是
hiredis。下面是安装hiredis的步骤:
git clone https://github.com/redis/hiredis
从github上下载hiredis客户端,进行源码wxncom@wxncom-virtual-machine:~/redis$ git clone https://github.com/redis/hiredis
Cloning into 'hiredis'...
fatal: unable to access 'https://github.com/redis/hiredis/': Failed to connect to github.com port 443: Connection refused
wxncom@wxncom-virtual-machine:~/redis$ git clone https://github.com/redis/hiredis
Cloning into 'hiredis'...
remote: Enumerating objects: 4682, done.
remote: Counting objects: 100% (239/239), done.
remote: Compressing objects: 100% (113/113), done.
remote: Total 4682 (delta 140), reused 177 (delta 126), pack-reused 4443
Receiving objects: 100% (4682/4682), 1.68 MiB | 1.93 MiB/s, done.
Resolving deltas: 100% (2924/2924), done.
wxncom@wxncom-virtual-machine:~/redis$ ls
hiredis
wxncom@wxncom-virtual-machine:~/redis$ cd hiredis/
wxncom@wxncom-virtual-machine:~/redis/hiredis$
如果遇到下面这个情况,多试几次,github那边的服务器很拉胯的,你需要多尝试几次,来建立tcp连接(http协议使用的是 : 基于TCP的传输层协议)
wxncom@wxncom-virtual-machine:~/redis$ git clone https://hub.fastgit.org/redis/hiredis
Cloning into 'hiredis'...
fatal: unable to access 'https://hub.fastgit.org/redis/hiredis/': Failed to connect to hub.fastgit.org port 443: Connection refused
wxncom@wxncom-virtual-machine:~/redis/hiredis$ make
cc -std=c99 -c -O3 -fPIC -Wall -Wextra -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -Werror -g -ggdb -pedantic alloc.c
cc -std=c99 -c -O3 -fPIC -Wall -Wextra -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -Werror -g -ggdb -pedantic net.c
cc -std=c99 -c -O3 -fPIC -Wall -Wextra -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -Werror -g -ggdb -pedantic hiredis.c
cc -std=c99 -c -O3 -fPIC -Wall -Wextra -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -Werror -g -ggdb -pedantic sds.c
cc -std=c99 -c -O3 -fPIC -Wall -Wextra -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -Werror -g -ggdb -pedantic async.c
cc -std=c99 -c -O3 -fPIC -Wall -Wextra -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -Werror -g -ggdb -pedantic read.c
cc -std=c99 -c -O3 -fPIC -Wall -Wextra -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -Werror -g -ggdb -pedantic sockcompat.c
cc -shared -Wl,-soname,libhiredis.so.1.2.1-dev -o libhiredis.so alloc.o net.o hiredis.o sds.o async.o read.o sockcompat.o
ar rcs libhiredis.a alloc.o net.o hiredis.o sds.o async.o read.o sockcompat.o
cc -std=c99 -c -O3 -fPIC -Wall -Wextra -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -Werror -g -ggdb -pedantic test.c
cc -o hiredis-test -O3 -fPIC -Wall -Wextra -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -Werror -g -ggdb -pedantic -I. test.o libhiredis.a
Generating hiredis.pc for pkgconfig...
wxncom@wxncom-virtual-machine:~/redis/hiredis$
编译成功!
拷贝生成的动态库到/usr/local/lib目录下!
然后.感兴趣的话,或者工作用到了,
可以继续学一下:如何通过C++使用hiredis客户端进行subscribe 和publish编程
简单来说 , hiredis就是一个c++可用的redis库(但是底层居然是用c语言编写的),现在处于一个知识爆炸的时代,做什么都是库,需要造的轮子越来越少了
比如: