<?php /** * memcache接続用クラス * 実際は、設定を保持して、同じコネクションを使うようにするためのもの。 * $Id: MemcacheManager.inc,v 1.1 2015/10/08 11:18:57 wanggb Exp $ * @author iimuro * @access public * @package jp.aimslib2.net */ class MemcacheManager{ public $host; public $port; public $memcache = null; public $is_connected = false; /** * コンストラクタ */ public function __construct(){ } /** * 初期化 */ public function init($host, $port = "11211"){ if (!defined("MEMCACHE_VERSION")) { define("MEMCACHE_VERSION", 1); } //$this->memcache = new Memcache(); $this->host = $host; if(isset($port)){ $this->port = $port; }else{ $this->port = "11211"; } //$this->memcache = memcache_connect($this->host, $this->port); $this->memcache = new Memcache(); $res = @$this->memcache->pconnect($this->host, $this->port); if(!$this->memcache || !$res) { ErrorLogger::doOutput("memcache connection error!", 0, "memcache"); return; } $this->is_connected = true; } /** * 追加の接続サーバーを指定します。 */ public function addServer($host, $port) { if (MEMCACHE_VERSION < 2) { ErrorLogger::doOutput("unimplimented function: addServer", 0); return false; } return $this->memcache->addServer($host, $port); } /** * 接続をきります。 */ public function close() { if ($this->memcache != null) { return $this->memcache->close(); } return false; } /** * 接続します。 */ public function pconnect() { if (!$this->is_connected) { ErrorLogger::doOutput("new connection!", 0, "memcache"); $this->memcache = new Memcache(); $this->is_connected = @$this->memcache->pconnect($this->host, $this->port); //$this->memcache = memcache_connect($this->host, $this->port); //if($this->memcache->pconnect($this->host, $this->port) === false) { //$this->is_connected = $this->memcache->connect($this->host, $this->port); if(!$this->memcache || !$this->is_connected) { ErrorLogger::doOutput("memcache connection error!", 0, "memcache"); return; } $this->is_connected = true; ErrorLogger::doOutput($this->is_connected, 0, "memcache"); } else { //ErrorLogger::doOutput("memcache persistant OK!", 0, "memcache"); } } /** * 減算 */ public function decrement($key, $value = 1) { $this->pconnect(); if ($this->is_connected) { return $this->memcache->decrement($key, $value); } else { ErrorLogger::doOutput("memcache connection error! increment", 0, "memcache"); return null; } } /** * キーを削除します。 */ public function delete($key) { $this->pconnect(); return $this->memcache->delete($key); } /** * データを削除します。 */ public function flush() { $this->pconnect(); return $this->memcache->flush(); } public function get($keys) { $this->pconnect(); if ($this->is_connected) { return $this->memcache->get($keys); } else { ErrorLogger::doOutput("memcache connection error! get", 0, "memcache"); return null; } } public function getExtendedStats() { if (MEMCACHE_VERSION < 2) { ErrorLogger::doOutput("unimplimented function: getExtendedStats", 0); return false; } return $this->memcache->getExtendedStats(); } /** * 状態を取得する */ public function getStats() { return $this->memcache->getStats(); } /** * バージョンを取得する */ public function getVersion() { $this->pconnect(); return $this->memcache->getVersion(); } /** * 加算 */ public function increment($key, $value = 1) { $this->pconnect(); if ($this->is_connected) { return $this->memcache->increment($key, $value); } else { ErrorLogger::doOutput("memcache connection error! increment", 0, "memcache"); return null; } } /** * 値をsetします */ public function set($key, $var, $flag = null, $expire = 0) { $this->pconnect(); if ($this->is_connected) { if ($expire > 2592000) { ErrorLogger::doOutput("memcache expire exceeds limit. setting to max value:2592000", __CLASS__); $expire = 2592000; } $this->memcache->set($key, $var, $flag, $expire); } else { ErrorLogger::doOutput("memcache connection error! set", 0, "memcache"); } } /** * 大きな値に対する自動圧縮処理を有効にする */ public function setCompressThreshold($threshold, $min_savings ) { $this->memcache->setCompressThreshold($threshold,$min_savings); } } ?>