教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP连接Memcache程序代码

PHP连接Memcache程序代码

发布时间:2016-10-16   编辑:jiaochengji.com
教程集为您提供PHP连接Memcache程序代码等资源,欢迎您收藏本站,我们将为您提供最新的PHP连接Memcache程序代码资源
Memcache是php中常用到的一个高性能的分布式的内存对象缓存系统,我们可以利用它来对网站进行性能提高并且减少服务器负载,下面是我的学习笔记本大家一起看看。 最简单的连接方法

例子

<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy6473')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy6473" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">

<?php
$mem = new Memcache;
$mem->connect('127.0.0.1', 11211) or die ("Could not connect");
$mem->set('key', 'This is a test!', 0, 60);
$val = $mem->get('key');
echo $val;
?>

</td> </tr> </tbody> </table>

应用中会做一个类

<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy2117')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy2117" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">

class MCache
{

    private static $server  = array('127.0.0.1', 11211);
    private static $client  = 'memcached'; // memcache or memcached

    private static function get_memcache()
    {
        static $memcache;
        if (!is_object($memcache))
        { www.jiaochengji.com
            $memcache = self::$client === 'memcached' ?  new Memcached() : new Memcache ;
            $memcache->addServer(self::$server[0],self::$server[1]);
        }
        return $memcache;
    }

    public static  function set($key, $value, $expire=600)
    {
        self::$client === 'memcached' ?  self::get_memcache()->set($key, $value, $expire)
                                :  self::get_memcache()->set($key, $value, MEMCACHE_COMPRESSED, $expire);
    }

    public static  function get($key)
    {
        return self::get_memcache()->get($key);
    }

}

MCache::set('test_one', 'value_one', 60); # 60 Sec
print_r(MCache::get('test_one'));

</td> </tr> </tbody> </table>


PHP的Memcache客户端所有方法总结

memcache函数所有的方法列表如下:
Memcache::add – 添加一个值,如果已经存在,则返回false
Memcache::addServer – 添加一个可供使用的服务器地址
Memcache::close – 关闭一个Memcache对象
Memcache::connect – 创建一个Memcache对象
memcache_debug – 控制调试功能
Memcache::decrement – 对保存的某个key中的值进行减法操作
Memcache::delete – 删除一个key值
Memcache::flush – 清除所有缓存的数据
Memcache::get – 获取一个key值
Memcache::getExtendedStats – 获取进程池中所有进程的运行系统统计
Memcache::getServerStatus – 获取运行服务器的参数
Memcache::getStats – 返回服务器的一些运行统计信息
Memcache::getVersion – 返回运行的Memcache的版本信息
Memcache::increment – 对保存的某个key中的值进行加法操作
Memcache::pconnect – 创建一个Memcache的持久连接对象
Memcache::replace -对一个已有的key进行覆写操作
Memcache::set – 添加一个值,如果已经存在,则覆写
Memcache::setCompressThreshold – 对大于某一大小的数据进行压缩
Memcache::setServerParams – 在运行时修改服务器的参数

 

您可能感兴趣的文章:
PHP如何操作Memcache缓存?
PHP连接Memcache程序代码
php将session保存在memcached中的方法
PHP调用MEMCACHE高速缓存技术实例
php memcached安装与使用
Memcached安装测试一例
php购物车是怎么实现的
Memcache防止被非法读取数据安全配置
php提示PHP class x has no unserializer解决办法
memcache图形化管理工具MemAdmin

[关闭]
~ ~