Application examples of redis in php

小云云
Release: 2023-03-21 17:32:01
Original
2055 people have browsed it

This article mainly shares with you the application examples of redis in php, mainly in the form of code, hoping to help everyone.

<?php
// 将消息存于 redis 队列中
$redis = new Redis(&#39;127.0.0.1&#39;, 6379);
$redis->connect();

$weiboInfo = array(
    &#39;uid&#39; => get_uid(),
    &#39;content&#39; => get_content(),
    &#39;timestamp&#39; => time(),
);
$redis->lpush(&#39;weibo_list&#39;, json_encode($weibo_info));
$redis->close();


// 利用 cron 程序遍历 redis 队列进行数据库操作
$redis = new Redis(&#39;127.0.0.1&#39;, 6379);
$redis->connect();
$weibo = new Weibo();
while (true) {
    if ($redis->lsize(&#39;weibo_list&#39;) > 0) {
        $info = json_decode($redis->rpop(&#39;weibo_list&#39;));
        $weibo->post($info->uid, $info->content, $info->timestamp);
    } else {
        sleep(1);
    }
}
$redis->close();
Copy after login

Related recommendations:

PHP and Redis realize rush buying and flash sales under high concurrency

Sharing of common usage scenarios of Redis

PHP link redis method code

The above is the detailed content of Application examples of redis in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template