検索
ホームページphp教程PHP源码一个简单的memcache类

一个简单的memcache类

<?php 
 
class cache_memcache { 
 
    private $_memcached; 
    protected $_memcached_conf = array( 
        &#39;default_host&#39; => &#39;127.0.0.1&#39;, 
        &#39;default_port&#39; => 11211, 
        &#39;default_weight&#39; => 1, 
    ); 
 
    public function __construct() { 
 
        $this->_memcached = new Memcache($this->_memcached_conf); 
        $this->_memcached->addserver(&#39;127.0.0.1&#39;, &#39;11211&#39;); 
    } 
 
    // Save data into cache 
    public function save($key, $data, $ttl = 3600) { 
        if (get_class($this->_memcached) == &#39;Memcached&#39;) { 
            return $this->_memcached->set($key, array($data, time(), $ttl), $ttl); 
        } else if (get_class($this->_memcached) == &#39;Memcache&#39;) { 
            return $this->_memcached->set($key, array($data, time(), $ttl), 0, $ttl); 
        } 
 
        return false; 
    } 
 
    // Fetch data from cache 
 
    public function get($key) { 
        $data = $this->_memcached->get($key); 
        return (is_array($data)) ? $data[0] : false; 
    } 
 
    // Detele data from cache 
    public function delete($key) { 
        return $this->_memcached->delete($key); 
    } 
 
    //   
    // clean will marks all the items as expired, 
    so occupied memory will be overwritten by new items. 
    public function clean() { 
        return $this->_memcached->flush(); 
    } 
 
    public function ServerStatus() { 
        $server_status = $this->_memcached->getstats(); 
        echo "<table border=&#39;1&#39;>"; 
        echo "<tr><td>Memcache Server version:</td><td> " . $server_status["version"] . "</td>
        </tr>"; 
    echo "<tr><td>Process id of this server process </td><td>" . $server_status["pid"] . "</td>
    </tr>"; 
        echo "<tr><td>Number of seconds this server has been running </td>
        <td>" . $server_status["uptime"] . "</td></tr>"; 
        echo "<tr><td>Accumulated user time for this process </td>
        <td>" . $server_status["rusage_user"] . " seconds</td></tr>"; 
        echo "<tr><td>Accumulated system time for this process </td>
        <td>" . $server_status["rusage_system"] . " seconds</td></tr>";
        echo "<tr><td>Total number of items stored by this server ever since it started</td>
        <td>" . $server_status["total_items"] . "</td></tr>"; 
        echo "<tr><td>Number of open connections </td>
        <td>" . $server_status["curr_connections"] . "</td></tr>"; 
        echo "<tr>
        <td>Total number of connections opened since the server started running </td>
        <td>" . $server_status["total_connections"] . "</td></tr>"; 
        echo "<tr><td>Number of connection structures allocated by the server </td>
        <td>" . $server_status["connection_structures"] . "</td></tr>"; 
        echo "<tr><td>Cumulative number of retrieval requests </td>
        <td>" . $server_status["cmd_get"] . "</td></tr>"; 
        echo "<tr><td> Cumulative number of storage requests </td>
        <td>" . $server_status["cmd_set"] . "</td></tr>"; 
     $percCacheHit = ((real) $server_status["get_hits"] / (real) $server_status["cmd_get"] * 100); 
        $percCacheHit = round($percCacheHit, 3); 
        $percCacheMiss = 100 - $percCacheHit; 
        echo "<tr><td>Number of keys that have been requested and found present </td>
        <td>" . $server_status["get_hits"] . " ($percCacheHit%)</td></tr>"; 
        echo "<tr><td>Number of items that have been requested and not found </td>
        <td>" . $server_status["get_misses"] . "($percCacheMiss%)</td></tr>"; 
        $MBRead = (real) $server_status["bytes_read"] / (1024 * 1024); 
        echo "<tr><td>Total number of bytes read by this server from network </td>
        <td>" . $MBRead . " Mega Bytes</td></tr>"; 
        $MBWrite = (real) $server_status["bytes_written"] / (1024 * 1024); 
        echo "<tr><td>Total number of bytes sent by this server to network </td>
        <td>" . $MBWrite . " Mega Bytes</td></tr>"; 
        $MBSize = (real) $server_status["limit_maxbytes"] / (1024 * 1024); 
        echo "<tr><td>Number of bytes this server is allowed to use for storage.</td>
        <td>" . $MBSize . " Mega Bytes</td></tr>"; 
        echo "<tr>
        <td>Number of valid items removed from cache to free memory for new items.</td>
        <td>" . $server_status["evictions"] . "</td>
        </tr>"; 
        echo "</table>"; 
    } 
 
} 
 
?>

2. 操作实例

require_once&#39;memcache2.php&#39;; 
$getData = new cache_memcache (); 
$res = $getData->get("Shibly"); 
if ($res) { 
    echo $res; 
} else { 
    echo "No data to display. The cache may be destroyed"; 
}
 
 
 
$srv_stat = new cache_memcache(); 
$srv_stat->ServerStatus();
 
 
require_once(&#39;memcache2.php&#39;); 
$setData = new cache_memcache(); 
$data =  $setData->save("Shibly","Hello there ! you&#39;ve stored me in a memcached server !"); 
 
if($data) 
{ 
    echo "You&#39;ve successfully stored data in the memcached server.
     Please Run the example_get_cache.php file to retrive your data";
} 
 
else{ 
    echo "There is something wrong with your caching server"; 
}

 以上就是一个简单的memcache类的内容,更多相关内容请关注PHP中文网(www.php.cn)!

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、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ヘンタイを無料で生成します。

ホットツール

SublimeText3 中国語版

SublimeText3 中国語版

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

DVWA

DVWA

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

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。