龚哥哥 - 山里男儿 爱生活、做自己!
CentOS安装memcached服务
发表于 2015-8-31 | 浏览(4985) | 服务器

1;yum安装

yum -y install memcached

2;验证是否安装成功,如果输出一些帮助信息表示安装成功

memcached -h

3;将memcache加入启动列表

chkconfig --level 2345 memcached on

4;配置Memcache

vi /etc/sysconfig/memcached
文件中内容如下
PORT=”11211″ 端口
USER=”root” 使用的用户名
MAXCONN=”1024″ 同时最大连接数
CACHESIZE=”64″ 使用的内存大小
OPTIONS=”" 附加参数

5;启动memcached

启动:service memcached start
关闭:service memcached stop
重启:service memcached restart

加入系统启动项
  echo "service memcached start" >> /etc/rc.local 

6;分配空间(11211 端口号可以自定义设置)

memcached -p 11211 -m 512 -d -u root start

7;连接memcached

telnet 121.199.44.203 11211

阅读全文

php安装memcache和memcached扩展
发表于 2015-8-31 | 浏览(5440) | 服务器

安装php memcache

wget http://pecl.php.net/get/memcache-3.0.6.tgz
tar -zxvf memcache-3.0.6.tgz
cd memcache-3.0.6
/data/soft/php/bin/phpize (如果不知道phpize在什么位置,可以用find / -name phpize查找)
./configure –enable-memcache –with-php-config=/data/soft/php/bin/php-config –with-zlib-dir
make
make install

记录下安装成功后的提示,类似于:
Installing shared extensions: /data/soft/php/modules/
把这个地址记录下来

在php.ini文件里面添加扩展

vim /data/soft/php/bin/php.ini
添加 extension=memcache.so

最后验证一下是否安装完成

php -m|grep memcache
应该会显示memcache

到这里php memcach安装完成

安装php memcached扩展

1、先安装 memcached 依赖库 libmemcached

wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
tar -zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
./configure --prefix=/usr/local/libmemcached --with-memcached
make
make install

现在安装php memcached扩展

wget http://pecl.php.net/get/memcached-2.2.0.tgz
tar -zxvf memcached-2.2.0.tgz
cd memcached-2.2.0
/data/soft/php/bin/phpize
./configure --enable-memcached --with-php-config=/data/soft/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl

在php.ini文件里面添加扩展

vim /data/soft/php/bin/php.ini
  添加 extension=memcached.so

最后验证一下是否安装完成

php -m|grep memcached
应该会显示memcached

到这里php memcachd安装完成

阅读全文

PHP Memcached操作类
发表于 2015-8-31 | 浏览(5596) | PHP
<?php

/**
 * 缓存驱动
 * @author  Devil
 * @version 1.0.0
 */
class CacheLibrary
{
    private $c_obj;
    private $c_time;
    /**
     * [__construct 构造方法]
     */
    public function __construct($host, $port = 11211)
    {
        /* 实例化memcached */
        $this->c_obj = new Memcached();
        if(!$this->c_obj->addServer($host, $port)) Api_Return(L('code_413'), 413);

        /* 基础参数设置 */
        $this->c_time = empty(C('cache')['time']) ? 0 : C('cache')['time'];
    }

    /**
     * [GetTime 获取缓存时间]
     * @return [int] [缓存时间]
     */
    private function GetTime($time = 0)
    {
        return (intval($time) > 0) ? intval($time) : $this->c_time;
    }

    /**
     * [Add 缓存添加]
     * @param  [string]     $key  [索引]
     * @param  [mixed]      $val  [值]
     * @param  [integer]    $time [过期时间(单位秒), 0永久, 或时间戳]
     * @return [boolean]    [成功true, 失败false]
     */
    public function Add($key, $val, $time = 0)
    {
        return $this->c_obj->add($key, $val, $this->GetTime($time));
    }

    /**
     * [Set 缓存替换]
     * @param  [string]     $key  [索引]
     * @param  [mixed]      $val  [值]
     * @param  [integer]    $time [过期时间(单位秒), 0永久, 或时间戳]
     * @return [boolean]    [成功true, 失败false]
     */
    public function Set($key, $val, $time = 0)
    {
        return $this->c_obj->set($key, $val, $this->GetTime($time));
    }

    /**
     * [SetMulti 设置多个索引的缓存数据]
     * @param [type] $key_all [索引数组]
     * @param  [integer]      $time [过期时间(单位秒), 0永久, 或时间戳]
     * @return [boolean]      [成功true, 失败false]
     */
    public function SetMulti($key_all, $time = 0)
    {
        return $this->c_obj->setMulti($key_all, $time);
    }

    /**
     * [Append 向已存在索引后面追加数据]
     * @param [string] $key [索引]
     * @param [string] $val [追加的数据(字符串)]
     */
    public function Append($key, $val)
    {
        $this->setOption();
        return $this->c_obj->append($key, $val);
    }

    /**
     * [Prepend 向已存在索引前面追加数据]
     * @param [string] $key [索引]
     * @param [string] $val [追加的数据(字符串)]
     */
    public function Prepend($key, $val)
    {
        $this->setOption();
        return $this->c_obj->prepend($key, $val);
    }

    /**
     * [Replace 替换已存在索引下的元素]
     * @param  [string]     $key  [索引]
     * @param  [mixed]      $val  [值]
     * @param  [integer]    $time [过期时间(单位秒), 0永久, 或时间戳]
     * @return [boolean]    [成功true, 失败false]
     */
    public function Replace($key, $val, $time = 0)
    {
        $this->c_obj->replace($key, $val, $time);
    }

    /**
     * [setOption 设置选项]
     */
    private function setOption()
    {
        $this->c_obj->setOption(Memcached::OPT_COMPRESSION, false);
    }

    /**
     * [Fetch 抓取下一个结果]
     * @param [string]  $key_all [索引名]
     * @param [boolean] $cas     [是否返回长度, 是true, 否false默认]
     */
    public function Fetch($key_all, $cas = false)
    {
        $this->GetDelayed($key_all, $cas);
        return $this->c_obj->fetch();
    }

    /**
     * [FetchAll 抓取所有剩余的结果]
     * @param [string]  $key_all [索引名]
     * @param [boolean] $cas     [是否返回长度, 是true, 否false默认]
     */
    public function FetchAll($key_all, $cas = false)
    {
        $this->GetDelayed($key_all, $cas);
        return $this->c_obj->fetchAll();
    }

    /**
     * [GetDelayed 请求多个索引]
     * @param [string]  $key_all [索引名]
     * @param [boolean] $cas     [是否返回长度, 是true, 否false默认]
     */
    private function GetDelayed($key_all, $cas)
    {
        $this->c_obj->getDelayed($key_all, $cas);
    }

    /**
     * [Get 缓存获取]
     * @param  [string]  $key  [索引]
     * @return [mixed]   [当前索引对应的数据]
     */
    public function Get($key)
    {
        return $this->c_obj->get($key);
    }

    /**
     * [GetMulti 获取多个索引的缓存数据]
     * @param [type] $key_all [索引数组]
     */
    public function GetMulti($key_all)
    {
        return $this->c_obj->getMulti($key_all);
    }

    /**
     * [Delete 删除一个索引]
     * @param  [string]  $key [索引]
     * @param  [integer] $time [等待时间(单位秒), 0立即, 或时间戳]
     * @return [boolean] [成功true, 失败false]
     */
    public function Delete($key, $time = 0)
    {
        return $this->c_obj->delete($key, $time);
    }

    /**
     * [DeleteMulti 删除多个索引]
     * @param  [array]            $key_all     [索引数组]
     * @param  [integer]          $time [过期时间(单位秒), 0立即, 或时间戳]
     * @return [boolean或array]    [成功true, 失败false]
     */
    public function DeleteMulti($key_all, $time = 0)
    {
        $s = $this->c_obj->deleteMulti($key_all, $time);
        if(empty($s)) return false;
        foreach($s as $key=>$val)
        {
            if($val !== true) if($this->Get($key) == false) $s[$key] = true;
        }
        return $s;
    }

    /**
     * [Flush 作废所有元素]
     * @param  [integer] $time [等待时间(单位秒), 0立即]
     * @return [boolean] [成功true, 失败false]
     */
    public function Flush($time = 0)
    {
        return $this->c_obj->flush($time);
    }
}
?>

阅读全文

TOP