memcacheQueue.class.php
/**
* PHP memcache 队列类
* @author LKK/lianq.net
* @version 0.3
* @修改说明:
* 1.放弃了之前的AB面轮值思路,使用类似数组的构造,重写了此类.
* 2.队列默认先进先出,但增加了反向读取功能.
* 3.感谢网友FoxHunter提出的宝贵意见.
* @example:
* $obj = new memcacheQueue('duilie');
* $obj->add('1asdf');
* $obj->getQueueLength();
* $obj->read(10);
* $obj->get(8);
*/
class memcacheQueue{
public static $client; //memcache客户端连接
public $access; //队列是否可更新
private $expire; //过期时间,秒,1~2592000,即30天内
private $sleepTime; //等待解锁时间,微秒
private $queueName; //队列名称,唯一值
private $retryNum; //重试次数,= 10 * 理论并发数
public $currentHead; //当前队首值
public $currentTail; //当前队尾值
const MAXNUM = 20000; //最大队列数,建议上限10K
const HEAD_KEY = '_lkkQueueHead_'; //队列首kye
const TAIL_KEY = '_lkkQueueTail_'; //队列尾key
const VALU_KEY = '_lkkQueueValu_'; //队列值key
const LOCK_KEY = '_lkkQueueLock_'; //队列锁key
/**
*コンストラクター
* @param String $ queuename queue name*/
public function __construct($queueName ='',$expire=0,$config =''){
if(empty($config)){
self::$client = memcache_pconnect ('127.0.0.1',11211);
}elseif(is_array($config)){//array('host'=>'127.0.0.1','port'=>'11211')
self: :$client = memcache_pconnect($config['host'],$config['port']);
}elseif(is_string($config)){//"127.0.0.1:11211"
$tmp =explode(' :',$config);
$conf['host'] = isset($tmp[0]) ? $tmp[0] : '127.0.0.1';
$conf['port'] = isset($tmp[1]) ? $tmp[1] : '11211';
self::$client = memcache_pconnect($conf['host'],$conf['port']);
}
if(!self::$client) return false ;
ignore_user_abort(true);//当客户断开连接,允许继续行
set_time_limit(0);//取消画面行延時間上限
$this->access = false;
$this->gt; sleepTime = 1000;
$expire = empty($expire) ? 3600 : intval($expire)+1;
$this->expire = $expire;
$this->queueName = $queueName;
$this->retryNum = 1000;
$this->head_key = $this->queueName 。 self::HEAD_KEY;
$this->tail_key = $this->queueName 。 self::TAIL_KEY;
$this->lock_key = $this->queueName 。 self::LOCK_KEY;
$this->_initSetHeadNTail();
}
/**
* キューの最初と最後の値を初期化して設定します
*/
private function _initSetHeadNTail(){
//当前队列首の数值
$this->currentHead = memcache_get(self::$client, $this->head_key);
if($this->currentHead === false) $this->currentHead =0;
//当前队列尾の数值
$this->currentTail = memcache_get(self::$client, $this->tail_key);
if($this->currentTail === false) $this->currentTail =0;
}
/**
* 要素を取り出す際はキュー先頭の値を変更します
* @param int $step ステップサイズの値
*/
private function _changeHead($step=1){
$this->currentHead += $step;
memcache_set(self::$client, $this->head_key,$this- >currentHead,false,$this->expire);
}
/**
* 要素を追加する場合は、キューの最後尾の値を変更します
* @param int $step ステップ値
* @param bool $reverse 反転するかどうか
* @return null
*/
private function _changeTail($step=1, $reverse =false){
if(!$reverse){
$this->currentTail += $step;
}else{
$this->currentTail -= $step;
}
memcache_set(self::$client, $this->tail_key,$this- >currentTail,false,$this->expire);
}
/**
* キューが空かどうか
* @return bool
*/
private function _isEmpty(){
return (bool)($this->currentHead === $this ->currentTail);
}
/**
* キューはいっぱいですか
* @return bool
*/
プライベート関数 _isFull(){
$len = $this->currentTail - $this->currentHead;
return (bool)($ len === self::MAXNUM);
}
/**
* キューロック
*/
プライベート関数 _getLock(){
if($this->access === false){
while(!memcache_add( self::$client, $this->lock_key, 1, false, $this->expire) ){
usleep($this->sleepTime);
@$i++;
if($i > $this->retryNum){///尝试等待N次
return false;
break;
}
}
$this->_initSetHeadNTail();
return $this->access = true;
}
return $this->access;
}
/**
* キューのロックが解除されました
*/
プライベート関数 _unLock (){
memcache_delete(self::$client, $this->lock_key, 0);
$this->access = false;
}
/**
* 現在のキューの長さを取得します
* この長さは理論上の長さであり、一部の要素は期限切れにより失われます。実際の長さ * @return int
*/
public function getQueueLength() {
$this->_initSetHeadNTail();
return intval($this->currentTail - $this->currentHead);
}
/**
* キューデータを追加します
* @param void $data 追加するデータ
* @return bool
*/
public function add($data) {
if(!$this->_getLock()) return false;
if($this->_isFull()){
$this->_unLock();
return false;
}
$ value_key = $this->queueName 。 self::VALU_KEY 。 strval($this->currentTail +1);
$result = memcache_set(self::$client, $value_key, $data, MEMCACHE_COMPRESSED, $this->expire);
if($result){
$this ->_changeTail();
}
$this->_unLock();
return $result;
}
/**
* キューデータの読み取り
* @param int $length 読み取る長さ(逆読み取りの場合は負の数を使用)
* @return array
*/
public function read($length=0){
if (!is_numeric($length)) return false;
$this->_initSetHeadNTail();
if($this->_isEmpty()){
return false;
}
if(empty($length) ) $length = self::MAXNUM;//默认全部
$keyArr = array();
if($length >0){//正向读取(从队列首向队列尾)
$tmpMin = $ this->currentHead;
$tmpMax = $tmpMin + $length;
for($i= $tmpMin; $i $keyArr[] = $this->queueName 。 self::VALU_KEY 。 $i;
}
}else{//逆方向读取(从队列尾向队列首)
$tmpMax = $this->currentTail;
$tmpMin = $tmpMax + $length;
for($i= $tmpMax; $i >$tmpMin; $i--){
$keyArr[] = $this->queueName 。 self::VALU_KEY 。 $i;
}
}
$result = @memcache_get(self::$client, $keyArr);
return $result;
}
/**
*/
public function get($length =0){
if(!is_numeric($length)) return false;
if(!$this->_getLock()) return false;
if($this->_isEmpty()){
$this ->_unLock();
return false;
}
if(empty($length)) $length = self::MAXNUM;//默认全部
$length = intval($length);
$keyArr = array ();
if($length >0){//正向读取(从队列首向队列尾)
$tmpMin = $this->currentHead;
$tmpMax = $tmpMin + $length;
for ($i= $tmpMin; $i $keyArr[] = $this->queueName 。 self::VALU_KEY 。 $i;
}
$this->_changeHead($length);
}else{//逆方向读取(从队列尾向队列首)
$tmpMax = $this->currentTail;
$tmpMin = $tmpMax + $length;
for($i= $tmpMax; $i >$tmpMin; $i--){
$keyArr[] = $this->queueName 。 self::VALU_KEY 。 $i;
}
$this->_changeTail(abs($length), true);
}
$result = @memcache_get(self::$client, $keyArr);
foreach($keyArr as $v){//取出之後删除
@memcache_delete(self::$client, $v, 0);
}
$this->_unLock();
return $result;
}
/**
* キューをクリアします
*/
public function clear(){
if(!$this->_getLock()) return false;
if($this->>_isEmpty()){
$this->>_unLock();
return false;
}
$tmpMin = $this->currentHead--;
$tmpMax = $this->currentTail++;
for($i= $tmpMin; $i $tmpKey = $this->queueName 。 self::VALU_KEY 。 $i;
@memcache_delete(self::$client, $tmpKey, 0);
}
$this->currentTail = $this->currentHead = 0;
memcache_set(self::$client, $this ->head_key,$this->currentHead,false,$this->expire);
memcache_set(self::$client, $this->tail_key,$this->currentTail,false,$this- >expire);
$this->_unLock();
}
/*
* 清除すべてmemcache缓存数据
*/
public function memFlush(){
memcache_flush(self::$client);
}
}//授業終了

PHPは動的なWebサイトを構築するために使用され、そのコア関数には次のものが含まれます。1。データベースに接続することにより、動的コンテンツを生成し、リアルタイムでWebページを生成します。 2。ユーザーのインタラクションを処理し、提出をフォームし、入力を確認し、操作に応答します。 3.セッションとユーザー認証を管理して、パーソナライズされたエクスペリエンスを提供します。 4.パフォーマンスを最適化し、ベストプラクティスに従って、ウェブサイトの効率とセキュリティを改善します。

PHPはMySQLIおよびPDO拡張機能を使用して、データベース操作とサーバー側のロジック処理で対話し、セッション管理などの関数を介してサーバー側のロジックを処理します。 1)MySQLIまたはPDOを使用してデータベースに接続し、SQLクエリを実行します。 2)セッション管理およびその他の機能を通じて、HTTPリクエストとユーザーステータスを処理します。 3)トランザクションを使用して、データベース操作の原子性を確保します。 4)SQLインジェクションを防ぎ、例外処理とデバッグの閉鎖接続を使用します。 5)インデックスとキャッシュを通じてパフォーマンスを最適化し、読みやすいコードを書き、エラー処理を実行します。

PHPで前処理ステートメントとPDOを使用すると、SQL注入攻撃を効果的に防ぐことができます。 1)PDOを使用してデータベースに接続し、エラーモードを設定します。 2)準備方法を使用して前処理ステートメントを作成し、プレースホルダーを使用してデータを渡し、メソッドを実行します。 3)結果のクエリを処理し、コードのセキュリティとパフォーマンスを確保します。

PHPとPythonには独自の利点と短所があり、選択はプロジェクトのニーズと個人的な好みに依存します。 1.PHPは、大規模なWebアプリケーションの迅速な開発とメンテナンスに適しています。 2。Pythonは、データサイエンスと機械学習の分野を支配しています。

PHPは、電子商取引、コンテンツ管理システム、API開発で広く使用されています。 1)eコマース:ショッピングカート機能と支払い処理に使用。 2)コンテンツ管理システム:動的コンテンツの生成とユーザー管理に使用されます。 3)API開発:RESTFUL API開発とAPIセキュリティに使用されます。パフォーマンスの最適化とベストプラクティスを通じて、PHPアプリケーションの効率と保守性が向上します。

PHPにより、インタラクティブなWebコンテンツを簡単に作成できます。 1)HTMLを埋め込んでコンテンツを動的に生成し、ユーザー入力またはデータベースデータに基づいてリアルタイムで表示します。 2)プロセスフォームの提出と動的出力を生成して、XSSを防ぐためにHTMLSPECIALCHARSを使用していることを確認します。 3)MySQLを使用してユーザー登録システムを作成し、Password_HashおよびPreprocessingステートメントを使用してセキュリティを強化します。これらの手法を習得すると、Web開発の効率が向上します。

PHPとPythonにはそれぞれ独自の利点があり、プロジェクトの要件に従って選択します。 1.PHPは、特にWebサイトの迅速な開発とメンテナンスに適しています。 2。Pythonは、データサイエンス、機械学習、人工知能に適しており、簡潔な構文を備えており、初心者に適しています。

PHPは依然として動的であり、現代のプログラミングの分野で重要な位置を占めています。 1)PHPのシンプルさと強力なコミュニティサポートにより、Web開発で広く使用されています。 2)その柔軟性と安定性により、Webフォーム、データベース操作、ファイル処理の処理において顕著になります。 3)PHPは、初心者や経験豊富な開発者に適した、常に進化し、最適化しています。


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

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

DVWA
Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、
