wget http://download.redis.io/releases/redis-stable.tar.gz tar -xvf redis-stable.tar.gz cd redis-stable/ make make install ./utils/install_server.sh
安裝成功:php
ps -ef|grep redis /etc/init.d/redis_6379 start /etc/init.d/redis_6379 stop service redis_6379 start service redis_6379 stop
在composer.json中添加redis
"yiisoft/yii2-redis": "~2.0.0"
並執行json
composer update
'components' => [ 'redis' => [ 'class' => 'yii\redis\Connection', 'hostname' => 'localhost', 'port' => 6379, 'database' => 0, ], ],
public function actionSetcart(){ if (isset($_REQUEST['uid'])&&isset($_REQUEST['cart'])) { $uid = $_REQUEST['uid']; $cart = $_REQUEST['cart']; //Redis保存購物車數據:30分鐘 $redis = Yii::$app->redis; $redis->set('cart:'.$uid, $cart); $redis->expire('cart:'.$uid, 30*60); $result['error'] = 0; $result['msg'] = '保存成功'; } else { $result['error'] = 1; $result['msg'] = '參數錯誤'; } return $result; } public function actionGetcart(){ if (isset($_REQUEST['uid'])) { $uid = $_REQUEST['uid']; //Redis保存購物車數據:30分鐘 $redis = Yii::$app->redis; $cart = $redis->get('cart:'.$uid); $result['error'] = 0; $result['msg'] = '獲取成功'; $result['cart'] = $cart ? $cart : ''; } else { $result['error'] = 1; $result['msg'] = '參數錯誤'; } return $result; }
$ip = $_SERVER['REMOTE_ADDR']; $redis = Yii::$app->redis; $redis_ip = $redis->get('ip:'.$ip); if ($redis_ip) { # code... } else { $redis->set('ip:'.$ip, TRUE); $redis->expire('ip:'.$ip, 60); # code... }