PHP 队列
php
何
特定の
応用
線形
列
什么是队列,是先进先出的线性表,在具体应用中通常用链表或者数组来实现,队列只允许在后端进行插入操作,在前端进行删除操作。 什么情况下会用了队列呢,并发请求又要保证事务的完整性的时候就会用到队列,当然不排除使用其它更好的方法,知道的不仿说说看
什么是队列,是先进先出的线性表,在具体应用中通常用链表或者数组来实现,队列只允许在后端进行插入操作,在前端进行删除操作。
什么情况下会用了队列呢,并发请求又要保证事务的完整性的时候就会用到队列,当然不排除使用其它更好的方法,知道的不仿说说看。
队列还可以用于减轻数据库服务器压力,我们可以将不是即时数据放入到队列中,在数据库空闲的时候或者间隔一段时间后执行。比如访问计数器,没有必要即时的执行访问增加的Sql,在没有使用队列的时候sql语句是这样的,假设有5个人访问:
- update table1 set count=count+1 where id=1
- update table1 set count=count+1 where id=1
- update table1 set count=count+1 where id=1
- update table1 set count=count+1 where id=1
- update table1 set count=count+1 where id=1
update table1 set count=count+1 where id=1 update table1 set count=count+1 where id=1 update table1 set count=count+1 where id=1 update table1 set count=count+1 where id=1 update table1 set count=count+1 where id=1
ログイン後にコピー
而使用队列这后就可以这样:
- update table1 set count=count+5 where id=1
update table1 set count=count+5 where id=1
ログイン後にコピー
减少sql请求次数,从而达到减轻服务器压力的效果, 当然访问量不是很大网站根本没有这个必要。
下面一个队列类:
- /**
- * 队列
- *
- * @author jaclon
- *
- */
- class Queue
- {
- private$_queue = array();
- protected$cache = null;
- protected$queuecachename;
- /**
- * 构造方法
- * @param string $queuename 队列名称
- */
- function __construct($queuename)
- {
- $this->cache =& Cache::instance();
- $this->queuecachename = 'queue_' . $queuename;
- $result = $this->cache->get($this->queuecachename);
- if (is_array($result)) {
- $this->_queue = $result;
- }
- }
- /**
- * 将一个单元单元放入队列末尾
- * @param mixed $value
- */
- function enQueue($value)
- {
- $this->_queue[] = $value;
- $this->cache->set($this->queuecachename, $this->_queue);
- return$this;
- }
- /**
- * 将队列开头的一个或多个单元移出
- * @param int $num
- */
- function sliceQueue($num = 1)
- {
- if (count($this->_queue) $num) {
- $num = count($this->_queue);
- }
- $output = array_splice($this->_queue, 0, $num);
- $this->cache->set($this->queuecachename, $this->_queue);
- return$output;
- }
- /**
- * 将队列开头的单元移出队列
- */
- function deQueue()
- {
- $entry = array_shift($this->_queue);
- $this->cache->set($this->queuecachename, $this->_queue);
- return$entry;
- }
- /**
- * 返回队列长度
- */
- function size()
- {
- returncount($this->_queue);
- }
- /**
- * 返回队列中的第一个单元
- */
- function peek()
- {
- return$this->_queue[0];
- }
- /**
- * 返回队列中的一个或多个单元
- * @param int $num
- */
- function peeks($num)
- {
- if (count($this->_queue) $num) {
- $num = count($this->_queue);
- }
- returnarray_slice($this->_queue, 0, $num);
- }
- /**
- * 消毁队列
- */
- function destroy()
- {
- $this->cache->remove($this->queuecachename);
- }
- }
http://blog.163.com/lgh_2002/blog/static/44017526201172511139202/
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事
R.E.P.O.説明されたエネルギー結晶と彼らが何をするか(黄色のクリスタル)
2週間前
By 尊渡假赌尊渡假赌尊渡假赌
レポ:チームメイトを復活させる方法
4週間前
By 尊渡假赌尊渡假赌尊渡假赌
ハローキティアイランドアドベンチャー:巨大な種を手に入れる方法
3週間前
By 尊渡假赌尊渡假赌尊渡假赌
スプリットフィクションを打ち負かすのにどれくらい時間がかかりますか?
3週間前
By DDD
R.E.P.O.ファイルの保存場所:それはどこにあり、それを保護する方法は?
3週間前
By DDD

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック
Gmailメールのログイン入り口はどこですか?
7313
9


Java チュートリアル
1625
14


CakePHP チュートリアル
1348
46


Laravel チュートリアル
1260
25


PHP チュートリアル
1207
29



Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド

PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法
