wget http://download.redis.io/releases/redis-4.0.1.tar.gz 下载redis4.0.1 稳定版
tar zxf redis-4.0.1.tar.gz 解压redis
cd redis-4.0.1 进入redis
make 编译
make install 编译安装
cp redis.conf /etc/redis/端口号.conf
vim /etc/redis/端口号.conf
{
daemonize no //改成后台运行 (改成yes)
port //改成自己需要的端口
pidfile /var/run/redis_6379.pid (改成和你端口号一致)
}
/usr/local/bin/redis-server /etc/redis/端口号.conf 启动redis
redis-cli -h 127.0.0.1 -p 7200 启动redis客户端 并指定端口和ip
能连接上就完事了
php 安装redis
wget https://github.com/phpredis/phpredis/archive/develop.zip 下载安装包
unzip develop.zip 解压
cd phpredis-develop
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
vim /usr/local/php/lib/php.ini (最底下加入extension="redis.so")
php -m 有redis就成功了
systemctl restart nginx.service
systemctl restart php-fpm.service
测试redis类型
string类型
set cdd lovenmm 存入字符串
get cdd (lovenmm) 取出字符串
set cdd2 5 存入数字
incr cdd2 自增1
decrby cdd2 2 自减2
list 类型 (值可以重复)
lpush list1 cdd 从左侧插入数据
lpush list1 cdd3 从左侧插入数据3
lpop 从左侧弹出(cdd3) 先进后出
rpop 从右侧弹出 (cdd) 先进先出
rpush list2 cdd 从右侧插入数据
set 类型(值不能重复)
sadd set1 12 插入记录
scard set1 查看里面有几个数据
sismember set1 12 判断数据是否存在 存在返回1 不存在返回0
srem set1 13 删除其中元素
hash 类型 (key值不能重复)
hset hash1 key1 12
hset hash1 key2 12
hget hash1 key1 (12) 获取key1里面的值
hlen hash1 获取集合里共有多少个元素
hmget hash1 key1 key2 一次查看多个值
sort set类型(如果两个元素的值相同,按照value的字典序排序 value全局唯一)
zadd zset1 12.2 value1
zadd zset1 12.2 value2
zadd zset1 12.2 value3
zcard zset1 查看共有多少个数据
zrange zset1 0 2 查看0到2的排名
zrange zset1 0 2 withscores 查看0到2的排名并且列出分数
操作例子
1 ########################################################################
2 # File Name :redis.php
3 # Author:Cdd
4 # mail:1277600680@qq.com
5 # Created Time: Fri 04 Aug 2017 01:56:55 PM CST
6 #==============================================================
7 <?php
8 $redis = new \Redis();
9 $redis->connect('127.0.0.1',7200);
10
11 //string 操作
12 $redis->delete('string1');
13 $redis->set('string1','val1');
14 $val = $redis->get('string1');
15 var_dump($val);
16
17 $redis->set('string1',4);
18 $redis->incr("string1",2);
19 $val = $redis->get('string1');
20 var_dump($val);
21
22 //list 操作
23 $redis->delete('list1');
24
25 $redis->lPush('list1',"A");
26 $redis->lPush('list1',"B");
27 $redis->lPush('list1',"C");
28
29 $val = $redis->rPop('list1');
30 var_dump($val);
31
32 //set 操作
33 $redis->delete("set1");
34
35 $redis->sAdd('set1',"A");
36 $redis->sAdd('set1',"B");
37 $redis->sAdd('set1',"C");
38 $redis->sAdd('set1',"C");
39
40 $val = $redis->sCard("set1");
41 var_dump($val);
42
43 $val = $redis->sMembers("set1");
44 var_dump($val);
45
46 //hash 操作
47
48 $redis->delete('driver1');
49 $redis->hSet('driver1',"name","mingming");
50 $redis->hSet('driver1',"age",25);
51 $redis->hSet('driver1',"gender",1);
52 $val = $redis->hGet('driver1',"name");
53 var_dump($val);
54
55 $val = $redis->hMGet('driver1',array('name','age'));
56 var_dump($val);
57
58 //sort set 操作
59
60 $redis->delete('zset1');
61 $redis->zAdd('zset1',100,'xiaoming');
62 $redis->zAdd('zset1',90,'xiaohong');
63 $redis->zAdd('zset1',93,'xiaowang');
64
65 $val = $redis->zRange("zset1",0,-1);
66 var_dump($val);
67
68 $val = $redis->zRevRange("zset1",0,-1);
69 var_dump($val);
开启redisAOF 持久化机制
appendonly no (no改成yes)
选项
# appendfsync always //每次操作都保存(推荐)
appendfsync everysec //每天保存一次
# appendfsync no //从不保存
redis 开机启动地址
http://blog.163.com/chen_hui/blog/static/227643095201712734710343/
网友评论