Redis
Redis是一個開源,高級的鍵值存儲和一個適用的解決方案,用於構建高性能,可擴展的Web應用程序。
Redis有三個主要特點,使它優越於其它鍵值數據存儲系統 -
- Redis將其數據庫完全保存在內存中,僅使用磁盤進行持久化。
- 與其它鍵值數據存儲相比,Redis有一組相對豐富的數據類型。
- Redis可以將數據復制到任意數量的從機中。
Redis支持的數據類型有 Stirng(字符串), List(列表), Hash(字典), Set(集合), Sorted Set(有序集合);
redis 隊列
redis 提供了兩種方式來作消息隊列。一個是生產者消費模式,另外是發布訂閱模式。前者會讓一個或者多個客戶端監聽消息隊列,消費者消費;后者是一個或者多個客戶端訂閱頻道,只要發布者發布消息,所以訂閱者都能收到消息,訂閱者都是平等的。
生產者消費模式
1、定時任務入列rpush
2、定時任務出列lpop
入列文件pre.php:
<?php $redis=new Redis(); $redis->connect('127.0.0.1','6379'); $password='fenglove'; $redis->auth($password); $arr=array('h','e','l','l','o','w','o','r','l','d'); foreach($arr as $k=>$v){ $redis->rpush('mylist',$v); }
出列文件index.php:
<?php $redis=new Redis(); $redis->connect('127.0.0.1',6379); $password='fenglove'; $redis->auth($password); //list類型出隊操作 $value=$redis->lpop('mylist'); if($value){ echo '出隊的值'.$value; }else{ echo "出隊完成"; }
開啟定時任務:
在/etc/中
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed 1 * * * * root /bin/netstat -lntp * * * * * root /usr/local/php/bin/php /root/test.php >> /root/test.log * * * * * root /usr/local/php/bin/php /root/phptest/index.php */10 * * * * root /usr/local/php/bin/php /root/phptest/pre.php
結果:
1 127.0.0.1:6379> lrange mylist 0 -1 2 1) "h" 3 2) "e" 4 3) "l" 5 4) "l" 6 5) "o" 7 6) "w" 8 7) "o" 9 8) "r" 10 9) "l" 11 10) "d" 12 127.0.0.1:6379> lrange mylist 0 -1 13 1) "e" 14 2) "l" 15 3) "l" 16 4) "o" 17 5) "w" 18 6) "o" 19 7) "r" 20 8) "l" 21 9) "d"
