On the application of redis in the project
- 2020-05-15 02:30:27
- OfStack
ps:PHP will automatically close the redis connection without manually closing it. For temporary data, you can operate directly on redis without going through the database
/* Message queue instance */
public function insertinfo(){
// Locally connected Redis service
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
// Store data in a list
$infos = array('info1' => 66, 'info2' => 88);
$redis->lpush($key, json_encode($infos));
// Get the stored data and output it
$arList = $redis->lrange("tutorial-list", 0, 30);
print_r($arList);
exit();
}
/* Read the instance */
public function getinfo(){
// Locally connected Redis service
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
// Get the stored data and output it
$result = json_decode($redis->get("tutoriallist"),'true');
if(empty($result)){
$sql="select * from mobantestinfo";
$VModel = new HuanShanVoteModel();
$result = $VModel->query($sql);
// Re-place the cache into the database redis You can't store an array directly and you have to convert it to json
$redis->set(json_encode($result));
}else{
// Locally connected Redis service
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
// Get the stored data and output it
$result = json_decode($redis->get("tutoriallist"),'true');
}
print_r($result);
exit();
}
/* Update the instance */
public function updateinfo(){
// run sql statements
$sql="update mobantestinfo set info1=1 where id=40";
$VModel = new HuanShanVoteModel();
$isOk = $VModel->execute($sql);
// Locally connected Redis service
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
/* delete key*/
$redis->del('tutoriallist');
}
/* Delete the instance */
public function deleteinfo(){
// run sql statements
$sql="delete from mobantestinfo where id=40";
$VModel = new HuanShanVoteModel();
$isOk = $VModel->execute($sql);
// Locally connected Redis service
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
$redis->del('tutoriallist');
}