php下使用redis

php下使用redis

client的选择

先去官网看了下php的client支持,选择了:https://github.com/hiproz/php-redis-client
根据文档描述,只支持到redis4.0,所以我们需要安装redis4.0

安装redis4.0

下载 redis4.0

编译:

$ wget http://download.redis.io/releases/redis-5.0.0.tar.gz
$ tar xzf redis-5.0.0.tar.gz
$ cd redis-5.0.0
$ make

编译完成后,执行文件就在 src目录下,启动redis server

src/redis-server

安装好后,就可以用本地内置的client来测试:

$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

开机服务自启动

make install 后,reids在源码的util目录下提供了可以直接创建系统service的脚本install_server.sh。直接执行,就可以安装成系统服务:

[root@sz-svr1 utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli

设置成开机自启动:

[root@sz-svr1 /]# systemctl enable redis_6379
redis_6379.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig redis_6379 on

增加密码后的停止

在设置密码后,在我们用systemctl stop redis_xxxx时,redis不会停止,一直输出:

Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...

所以我们需要在结束指令中,增加密码参数。具体修改自行参看/etc/rc.d/init.d/reids_xxxx

测试

至此,我们的环境完成了,我们执行测试代码 examples/raw_commands.php,在浏览器输入路径后,页面显示:

result: bar result: bar result: or value with spaces

观察php日志,确认代码,我们知道页面的结果是正确的。

支持php环境下的redis支持就完成了,剩下的就是码业务逻辑了。

hanve fun!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注