0

0

一个简单的memcache类

PHP中文网

PHP中文网

发布时间:2016-05-25 17:12:00

|

1291人浏览过

|

来源于php中文网

原创

一个简单的memcache类

 '127.0.0.1', 
        'default_port' => 11211, 
        'default_weight' => 1, 
    ); 
 
    public function __construct() { 
 
        $this->_memcached = new Memcache($this->_memcached_conf); 
        $this->_memcached->addserver('127.0.0.1', '11211'); 
    } 
 
    // Save data into cache 
    public function save($key, $data, $ttl = 3600) { 
        if (get_class($this->_memcached) == 'Memcached') { 
            return $this->_memcached->set($key, array($data, time(), $ttl), $ttl); 
        } else if (get_class($this->_memcached) == 'Memcache') { 
            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 ""; 
        echo ""; 
    echo ""; 
        echo ""; 
        echo ""; 
        echo "";
        echo ""; 
        echo ""; 
        echo ""; 
        echo ""; 
        echo ""; 
        echo ""; 
     $percCacheHit = ((real) $server_status["get_hits"] / (real) $server_status["cmd_get"] * 100); 
        $percCacheHit = round($percCacheHit, 3); 
        $percCacheMiss = 100 - $percCacheHit; 
        echo ""; 
        echo ""; 
        $MBRead = (real) $server_status["bytes_read"] / (1024 * 1024); 
        echo ""; 
        $MBWrite = (real) $server_status["bytes_written"] / (1024 * 1024); 
        echo ""; 
        $MBSize = (real) $server_status["limit_maxbytes"] / (1024 * 1024); 
        echo ""; 
        echo ""; 
        echo "
Memcache Server version: " . $server_status["version"] . "
Process id of this server process " . $server_status["pid"] . "
Number of seconds this server has been running " . $server_status["uptime"] . "
Accumulated user time for this process " . $server_status["rusage_user"] . " seconds
Accumulated system time for this process " . $server_status["rusage_system"] . " seconds
Total number of items stored by this server ever since it started " . $server_status["total_items"] . "
Number of open connections " . $server_status["curr_connections"] . "
Total number of connections opened since the server started running " . $server_status["total_connections"] . "
Number of connection structures allocated by the server " . $server_status["connection_structures"] . "
Cumulative number of retrieval requests " . $server_status["cmd_get"] . "
Cumulative number of storage requests " . $server_status["cmd_set"] . "
Number of keys that have been requested and found present " . $server_status["get_hits"] . " ($percCacheHit%)
Number of items that have been requested and not found " . $server_status["get_misses"] . "($percCacheMiss%)
Total number of bytes read by this server from network " . $MBRead . " Mega Bytes
Total number of bytes sent by this server to network " . $MBWrite . " Mega Bytes
Number of bytes this server is allowed to use for storage. " . $MBSize . " Mega Bytes
Number of valid items removed from cache to free memory for new items. " . $server_status["evictions"] . "
"; } } ?>

2. 操作实例

require_once'memcache2.php'; 
$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('memcache2.php'); 
$setData = new cache_memcache(); 
$data =  $setData->save("Shibly","Hello there ! you've stored me in a memcached server !"); 
 
if($data) 
{ 
    echo "You'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)!

Jamboss
Jamboss

Jamboss是一款简单的AI音乐生成App,可以一键生成歌曲。

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

65

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

43

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

35

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

41

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

204

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

9

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

8

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

3

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Go 教程
Go 教程

共32课时 | 3.2万人学习

MongoDB 教程
MongoDB 教程

共17课时 | 1.7万人学习

TypeScript 教程
TypeScript 教程

共19课时 | 1.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号