ホームページ バックエンド開発 PHPチュートリアル PHP memcache クラスの共有_PHP チュートリアル

PHP memcache クラスの共有_PHP チュートリアル

Jul 13, 2016 am 10:35 AM
memcache php 主要 説明書 共有 記事 親切

この記事では主にPHPのmemcacheクラス(memcache queue)の使い方を紹介していますので、必要な方は参考にしてください

。memcacheQueue.class.php コードは以下のように表示されます。 add('1asdf'); * $obj->getQueueLength(); * $obj->read(10); * $obj->get(8); ​*/ クラス 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_' //キューの最初のキー const TAIL_KEY = '_lkkQueueTail_'; //キューの末尾のキー const VALU_KEY = '_lkkQueueValu_'; //キュー値のキー const LOCK_KEY = '_lkkQueueLock_'; //キューのロックキー ​ /*** コンストラクター * @param string $queueName キュー名 * @param int $expire 有効期限 * @param array $config memcache 設定 * * @return ; ​​*/ パブリック関数 __construct($queueName ='',$expire=0,$config =''){ if(空($config)){ Self::$client = memcache_pconnect('127.0.0.1',11211); }elseif(is_array($config)){//array('ホスト'=>'127.0.0.1','ポート'=>'11211') self::$client = memcache_pconnect($config['host'],$config['port']); }elseif(is_string($config)){//"127.0.0.1:11211" $tmp =explode(':',$config); $conf['ホスト'] = isset($tmp[0]) $tmp[0] : '127.0.0.1'; $conf['ポート'] = isset($tmp[1]) $tmp[1] : '11211'; Self::$client = memcache_pconnect($conf['host'],$conf['port']); } if(!self::$client) は false を返します。 ​ ignore_user_abort(true);//クライアントが切断された場合、実行の継続を許可します set_time_limit(0);//スクリプト実行遅延の上限を解除 ​ $this->access = false; $this->睡眠時間 = 1000; $expire = 空($expire) ? 3600 : intval($expire)+1; $this->expire = $expire; $this->キュー名 = $キュー名; $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(); } ​ /*** キューの先頭と末尾の値を初期化して設定します ​​*/ プライベート関数 _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 ステップ値 ​​*/ プライベート関数 _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 ​​*/ プライベート関数 _changeTail($step=1, $reverse =false){ if(!$reverse){ $this->currentTail += $step; }それ以外{ $this->currentTail -= $step; } ​ memcache_set(self::$client, $this->tail_key,$this->currentTail,false,$this->expire); } ​ /*** キューが空かどうか * @return bool ​​*/ プライベート関数 _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次      false を返します。      壊す;     }    }      $this->_initSetHeadNTail();    $this->access = true を返します。   }     $this->アクセスを返す;  }       /*** キューのロック解除 ​​*/  プライベート関数 _unLock(){   memcache_delete(self::$client, $this->lock_key, 0);   $this->access = false;  }       /*** 現在のキューの長さを取得します * この長さは理論上の長さです。実際の長さはこの長さ以下になります。 * @return int ​​*/  パブリック関数 getQueueLength(){   $this->_initSetHeadNTail();   return intval($this->currentTail - $this->currentHead);  }       /*** キューデータを追加 * @param void $data 追加するデータ * @return bool ​​*/  パブリック関数 add($data){   if(!$this->_getLock()) は false を返します。     if($this->_isFull()){    $this->_unLock();    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();   $result を返します。  }       /*** キューデータの読み取り * @param int $length 読み取る長さ (逆読み取りの場合は負の数値を使用) * @return 配列 ​​*/  パブリック関数 read($length=0){   if(!is_numeric($length)) は false を返します。   $this->_initSetHeadNTail();     if($this->_isEmpty()){    false を返します。   }     if(empty($length)) $length = self::MAXNUM;//すべて   $keyArr = 配列();   if($length > 0){//正向读取(从队列首向队列尾)    $tmpMin = $this->currentHead;    $tmpMax = $tmpMin + $length;    for($i= $tmpMin; $iqueueName 。 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);     $result を返します。  }       /*** キューデータの取得 * @param int $length 取得する長さ (逆読みの場合は負の数値を使用) * @return 配列 ​​*/  パブリック関数 get($length=0){   if(!is_numeric($length)) は false を返します。   if(!$this->_getLock()) は false を返します。     if($this->_isEmpty()){    $this->_unLock();    false を返します。   }     if(empty($length)) $length = self::MAXNUM;//すべて   $length = 整数($length);   $keyArr = 配列();   if($length > 0){//正向读取(从队列首向队列尾)    $tmpMin = $this->currentHead;    $tmpMax = $tmpMin + $length;    for($i= $tmpMin; $iqueueName 。 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();     $result を返します。  }       /*** キューをクリアする ​​*/  パブリック関数clear(){   if(!$this->_getLock()) は false を返します。     if($this->_isEmpty()){    $this->_unLock();    false を返します。   }     $tmpMin = $this->currentHead--;   $tmpMax = $this->currentTail++;     for($i= $tmpMin; $iqueueName 。 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保存データを削除します   */  パブリック関数 memFlush(){   memcache_flush(self::$client);  }   }//クラスを終了  

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/746591.html技術記事この篇文章主介绍了phpのmemcacheクラスの使用方法(memcache队列)、必要な友人は参照可能下memcacheQueue.class.php代码如下:?php/*** PHP memcache 队列类* @aut...
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

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

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

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

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

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

Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド Dec 24, 2024 pm 04:42 PM

PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

CakePHP データベースの操作 CakePHP データベースの操作 Sep 10, 2024 pm 05:25 PM

CakePHP でデータベースを操作するのは非常に簡単です。この章では、CRUD (作成、読み取り、更新、削除) 操作について理解します。

CakePHP の日付と時刻 CakePHP の日付と時刻 Sep 10, 2024 pm 05:27 PM

Cakephp4 で日付と時刻を操作するには、利用可能な FrozenTime クラスを利用します。

CakePHP ファイルのアップロード CakePHP ファイルのアップロード Sep 10, 2024 pm 05:27 PM

ファイルのアップロードを行うには、フォーム ヘルパーを使用します。ここではファイルアップロードの例を示します。

CakePHP について話し合う CakePHP について話し合う Sep 10, 2024 pm 05:28 PM

CakePHP は、PHP 用のオープンソース フレームワークです。これは、アプリケーションの開発、展開、保守をより簡単にすることを目的としています。 CakePHP は、強力かつ理解しやすい MVC のようなアーキテクチャに基づいています。モデル、ビュー、コントローラー

CakePHP バリデータの作成 CakePHP バリデータの作成 Sep 10, 2024 pm 05:26 PM

Validator は、コントローラーに次の 2 行を追加することで作成できます。

CakePHP のロギング CakePHP のロギング Sep 10, 2024 pm 05:26 PM

CakePHP へのログインは非常に簡単な作業です。使用する関数は 1 つだけです。 cronjob などのバックグラウンド プロセスのエラー、例外、ユーザー アクティビティ、ユーザーが実行したアクションをログに記録できます。 CakePHP でのデータのログ記録は簡単です。 log()関数が提供されています

PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法 PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法 Dec 20, 2024 am 11:31 AM

Visual Studio Code (VS Code とも呼ばれる) は、すべての主要なオペレーティング システムで利用できる無料のソース コード エディター (統合開発環境 (IDE)) です。 多くのプログラミング言語の拡張機能の大規模なコレクションを備えた VS Code は、

See all articles