<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>运达&#039;s  blog &#187; 抢购</title>
	<atom:link href="https://www.yunda51.com/?feed=rss2&#038;tag=%E6%8A%A2%E8%B4%AD" rel="self" type="application/rss+xml" />
	<link>https://www.yunda51.com</link>
	<description>运达的博客</description>
	<lastBuildDate>Wed, 12 Nov 2025 07:58:26 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.0.19</generator>
	<item>
		<title>PHP操作redis(list)队列操作</title>
		<link>https://www.yunda51.com/?p=1976</link>
		<comments>https://www.yunda51.com/?p=1976#comments</comments>
		<pubDate>Sun, 24 Dec 2023 13:13:32 +0000</pubDate>
		<dc:creator><![CDATA[运达]]></dc:creator>
				<category><![CDATA[Redis]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[redis]]></category>
		<category><![CDATA[抢购]]></category>
		<category><![CDATA[队列]]></category>

		<guid isPermaLink="false">http://www.yunda51.com/?p=1976</guid>
		<description><![CDATA[好久没有更新文章了。 list使用场景如下： 消息队列 list类型的lpop和rpush（或者反过来，lpu<a href="https://www.yunda51.com/?p=1976" class="read-more">Continue Reading</a>]]></description>
				<content:encoded><![CDATA[<p><strong>好久没有更新文章了。</strong><br />
list使用场景如下：<br />
<strong>消息队列</strong><br />
list类型的lpop和rpush（或者反过来，lpush和rpop）能实现队列的功能，故而可以用Redis的list类型实现简单的点对点的消息队列。<br />
<strong>最新列表</strong><br />
list类型的lpush命令和lrange命令能实现最新列表的功能，每次通过lpush命令往列表里插入新的元素，<br />
然后通过lrange命令读取最新的元素列表，比如评论列表、朋友圈的点赞列表。</p>
<p><strong>######消息队列demo,简单模拟抢购商品#####</strong></p>
<pre class="wp-code-highlight prettyprint">
$redis = new redis();
$redis-&gt;connect(&#039;127.0.0.1&#039;, 6379);
$redis-&gt;auth(&#039;123456&#039;);
$redis-&gt;flushAll(); //清空所有数据
/***入列***/
//用户点击抢购的时候，把商品信息存入消息队列
$key = &#039;product1&#039;;#商品1
for ($i = 0; $i &lt; 100; $i++) {
    $uid = rand(1000, 9999);
    $number = 10; //次数，假设这件商品只能10个人抢购
    if ($number &gt; $redis-&gt;lLen($key)) {
        $redis-&gt;rpush($key, $uid); //不够10个就入列
    } else {
        continue;
    }
}
/***出列***/
while (true){
    $flag = $redis-&gt;exists($key);
    if($flag){
        $uid = $redis-&gt;rpop($key);
        echo $uid .&quot;抢购成功！，进行订单处理操作&lt;br/&gt;&quot;;
    } else {
        echo &quot;出列完毕！&quot;;
        exit;
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>https://www.yunda51.com/?feed=rss2&#038;p=1976</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
