Home Backend Development PHP Tutorial 队列在编程中的实际应用(php)_php技巧

队列在编程中的实际应用(php)_php技巧

May 17, 2016 am 09:23 AM
queue

一:队列的概念、数据结构

队列(Queue)是运算受到限制的一种线性表。只允许在表的一端进行插入,而在另一端进行删除元素的线性表。队尾(rear)是允许插入的一端。队头(front)是允许删除的一端。空队列是不含元素的空表。

假设有个队列Q=(a1,a2,…,an),则a1为队头元素,an为队尾元素。元素入队的次序为a1,a2,…,an,而出队的次序为a1,a2,…,an。可见队列的操作是按照先进先出的原则进行的。

其他详细的介绍请在网上搜索很多资料。

二:PHP的队列

在PHP中队列以数组的形式表现。数组中的第一个元素作为队头,最后一个元素作为队尾,这样就可以操作这个队列了。

结果就是

网上有很多封装好的类,可以直接使用。

array_push:将一个或多个单元压入数组的末尾(入栈)

array_unshift:在数组开头插入一个或多个单元

array_pop:将数组最后一个单元弹出(出栈)

array_shift:将数组开头的单元移出数组

三:Ruby Starling

Starling是一个支持MemCache协议的轻量级持久化服务器。Starling是让创建网络访问队列或者多个队列异常简单,也就是说多点和多台机器间的异步工作进程。它是著名微博客网站Twitter开发用来处理大量的队列消息,以及保持服务的响应。Starling已经在生产环境中使用,不仅是Twitter在使用,FiveRuns同样在使用。FiveRuns甚至还根据自己的应用做了改进。

Starling和Memcache使用的是一个协议只是端口不一样。Starling使用的是22122端口,Memcache使用的是11211端口。

Ruby
tar xzvf ruby-1.9.1-p0.tar.gz
cd ruby-1.9.1-p0
./configure --prefix=/usr/local/huiyangruby
make
make install
 
Gem
tar -zxvf rubygems-1.3.6.tgz
cd rubygems-1.3.6
ruby setup.rb
 
Starling
gem install memcache-client starling
starling
starling & //后台执行
starling_top //查看PS信息

接下来你就可以使用队列做自己的事情啦。Starling和Memcache用法一样,两者配合处理更佳。

使用Memcache::addServer可以建立一个memcache连接池。他不同于connect与pconnect他是在有请求是才连接,无则端口连接。

Memcache::connect -- 打开一个到Memcache的连接。

Memcache::pconnect -- 打开一个到Memcache的长连接。

Memcache::close -- 关闭一个Memcache的连接。

Memcache::set -- 保存数据到Memcache服务器上。

Memcache::get -- 提取一个保存在Memcache服务器上的数据。

Memcache::replace -- 替换一个已经存在Memcache服务器上的项目(功能类似Memcache::set)。

Memcache::delete -- 从Memcache服务器上删除一个保存的项目。

Memcache::flush -- 刷新所有Memcache服务器上保存的项目(类似于删除所有的保存的项目)。

Memcache::getStats -- 获取当前Memcache服务器运行的状态。

四:张宴作品HTTPSQS

HTTPSQS(HTTP Simple Queue Service)是一款基于 HTTP GET/POST 协议的轻量级开源简单消息队列服务,使用 Tokyo Cabinet 的 B+Tree Key/Value 数据库来做数据的持久化存储。

有兴趣的可以看看网址:http://blog.s135.com/httpsqs_1_2/

五:队列的应用

队列可以很好地异步处理数据传送和存储,当你频繁地向数据库中插入数据、频繁地向搜索引擎提交数据,就可采取队列来异步插入。另外,还可以将较慢的处理逻辑、有并发数量限制的处理逻辑,通过消息队列放在后台处理,例如FLV视频转换、发送手机短信、发送电子邮件等。(文/侯惠阳 PHPer.yang)

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Deque in Python: Implementing efficient queues and stacks Deque in Python: Implementing efficient queues and stacks Apr 12, 2023 pm 09:46 PM

deque in Python is a low-level, highly optimized deque useful for implementing elegant and efficient Pythonic queues and stacks, which are the most common list-based data types in computing. In this article, Mr. Yun Duo will learn the following together with you: Start using deque to effectively pop up and append elements. Access any element in deque. Use deque to build an efficient queue. Start using deque to append elements to the right end of a Python list and pop up elements. The operations are generally very Efficient. If time complexity is expressed in Big O, then we can say that they are O(1). And when Python needs to reallocate memory to increase the underlying list to accept new elements, these

How to use Supervisor to manage ThinkPHP6 queue? How to use Supervisor to manage ThinkPHP6 queue? Jun 12, 2023 am 08:51 AM

As web applications continue to develop, we need to handle a large number of tasks to maintain the stability and availability of the application. Using a queuing system is one solution. ThinkPHP6 provides a built-in queue system to manage tasks. However, handling a large number of tasks requires better queue management, which can be achieved using Supervisor. This article will introduce how to use Supervisor to manage ThinkPHP6 queues. Before that, we need to understand some basic concepts: queue system queue system is

Application of queue technology in message delay and message retry in PHP and MySQL Application of queue technology in message delay and message retry in PHP and MySQL Oct 15, 2023 pm 02:26 PM

Application summary of queue technology in message delay and message retry in PHP and MySQL: With the continuous development of web applications, the demand for high concurrency processing and system reliability is getting higher and higher. As a solution, queue technology is widely used in PHP and MySQL to implement message delay and message retry functions. This article will introduce the application of queue technology in PHP and MySQL, including the basic principles of queues, methods of using queues to implement message delay, and methods of using queues to implement message retries, and give

Analysis and optimization strategies for Java Queue queue performance Analysis and optimization strategies for Java Queue queue performance Jan 09, 2024 pm 05:02 PM

Performance Analysis and Optimization Strategy of JavaQueue Queue Summary: Queue (Queue) is one of the commonly used data structures in Java and is widely used in various scenarios. This article will discuss the performance issues of JavaQueue queues from two aspects: performance analysis and optimization strategies, and give specific code examples. Introduction Queue is a first-in-first-out (FIFO) data structure that can be used to implement producer-consumer mode, thread pool task queue and other scenarios. Java provides a variety of queue implementations, such as Arr

In Java, what is the difference between add() method and offer() method in queue? In Java, what is the difference between add() method and offer() method in queue? Aug 27, 2023 pm 02:25 PM

Queue in Java is a linear data structure with multiple functions. A queue has two endpoints and it follows the first-in-first-out (FIFO) principle for inserting and deleting its elements. In this tutorial, we will learn about two important functions of queues in Java, which are add() and Offer(). What is a queue? Queue in Java is an interface that extends the util and collection packages. Elements are inserted in the backend and removed from the frontend. Queues in Java can be implemented using classes such as linked lists, DeQueue, and priority queues. A priority queue is an extended form of a normal queue, where each element has a priority. The add() method of the queue is used to insert elements into the queue. It will define the element (as

Implementation plan of queue task monitoring and task scheduling in PHP and MySQL Implementation plan of queue task monitoring and task scheduling in PHP and MySQL Oct 15, 2023 am 09:15 AM

Implementation of queue task monitoring and task scheduling in PHP and MySQL Introduction In modern web application development, task queue is a very important technology. Through queues, we can queue some tasks that need to be executed in the background, and control the execution time and order of tasks through task scheduling. This article will introduce how to implement task monitoring and scheduling in PHP and MySQL, and provide specific code examples. 1. Working principle of queue Queue is a first-in-first-out (FIFO) data structure that can be used to

What is the principle and implementation of the PHP mail queue system? What is the principle and implementation of the PHP mail queue system? Sep 13, 2023 am 11:39 AM

What is the principle and implementation of the PHP mail queue system? With the development of the Internet, email has become one of the indispensable communication methods in people's daily life and work. However, as the business grows and the number of users increases, sending emails directly may lead to server performance degradation, email delivery failure and other problems. To solve this problem, you can use a mail queue system to send and manage emails through a serial queue. The implementation principle of the mail queue system is as follows: when the mail is put into the queue, when it is necessary to send the mail, it is no longer directly

Queues in the Yii framework: Handling asynchronous operations efficiently Queues in the Yii framework: Handling asynchronous operations efficiently Jun 21, 2023 am 10:13 AM

With the rapid development of the Internet, applications have become increasingly important to handle large numbers of concurrent requests and tasks. In such cases, handling asynchronous tasks is essential as this makes the application more efficient and better responsive to user requests. The Yii framework provides a convenient queue component that makes handling asynchronous operations easier and more efficient. In this article, we will explore the use and advantages of queues in the Yii framework. What is a Queue A queue is a data structure used to handle data in a first-in-first-out (FIFO) order. Team

See all articles