---
title: "认识缓存之Memcached【2】安装使用"
author: "student_lp"
date: "2014-08-12T14:39:29Z"
tags: []
categories: ["互联网"]
slug: "memcached-2"
draft: false
lastmod: "2014-08-12T14:36:00Z"
id: 2102834
url: "https://student-lp.iteye.com/blog/2102834"
---

**一、安装Memcached服务器**

1、在安装Memcached之前，必须先安装依赖库libevent，安装方法如下：

```
wget https://github.com/downloads/libevent/libevent/libevent-2.0.15-stable.tar.gz
tar -zxvf libevent-2.0.15-stable.tar.gz
cd libevent-2.0.15-stable
./configure --prefix=/usr/local
make 
make install
```

 2、libevent安装完毕，安装Memcached服务器：

```
wget http://memcached.googlecode.com/files/memcached-1.4.9.tar.gz
tar -zxvf memcached-1.4.9.tar.gz
cd memcached-1.4.9
./configure --prefix=/usr/local/memcached
make
make install
```

 3、安装完毕，使用如下指令启动Memcached服务器：

```
/usr/local/memcached/bin/memcached -d -m 128 -u root -p 11211
```

- -d：以守护程序（daemon）方式运行memcached
- -m：设置memcached可以使用的内存大小，单位为MB；
- -l：设置监听IP地址，如果是本机的话，通常不设置此参数；
- -p：设置监听的端口，默认为11211，也可以不设置此参数；
- -u：指定用户，如果当前为root，需要使用此参数指定用户；

**二、安装memcached客户端**

    memcached客户端非常多，对应于各种语言，现在以php为例说明：

1、安装php的memcached扩展如下：

```
wget http://pecl.php.net/get/memcache-2.2.5.tgz
tar -zxvf memcache-2.2.5.tar
cd memcache-2.2.5
phpize
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
make
make install
```

 2、在php配置文件php.ini中加入以下配置：

extension=/usr/local/php/lib/php/extensions/no-debug-zts-20120625/memcache.so

3、重启Web服务器。

如果安装成功，可以通过phpinfo()获得扩展的相关信息。

![](http://dl2.iteye.com/upload/attachment/0100/0989/a114e657-aafc-35f1-8b4f-a201d39c0420.png)  
 **三、memcached的操作使用**

```
class myMemcache {
    private $memcache;
    /**
     * 一般建议这2个值做成常量的形式
     */
    public function __construct($host = '192.102.1.8', $port = 6379) {
        $this->memcache = new Memcache();
        $this->memcache->connect($host, $port);
        return $this->memcache;
    }
 
    /**
     * add  添加一个新key,但是如果 key已经在服务端存在，此操作会失败。
     * @param string $Key    KEY名称
     * @param string $value  值----可以是数组，对象，单值
     * @param int $timelift 生存时间   add生存时间默认为0表示数据用不过期
     */
    public function add($key, $value, $timeLife) {
        if ($time > 0) {
            $ret = $this->memcache->add($key, $value, MEMCACHE_COMPRESSED, $timeLife);
        } else {
            $ret = $this->memcache->add($key, $value);
        }
        return $ret;
    }
 
    /**
     * set設置一致key  修改键名的值
     * @param string $key 键名
     * @param string $value 键值
     * @param int $timeLife 生命周期
     */
    public function set($key, $value, $timeLife) {
        if ($timeLife > 0) {
            $ret = $this->memcache->set($key, $value, MEMCACHE_COMPRESSED, $timeLife);
        } else {
            $ret = $this->memcache->set($key, $value);
        }
        return $ret;
    }
 
    /**
     * 获取key
     * @param string  $key 键名
     */
    public function get($key) {
        $ret = $this->memcache->get($key);
        return $ret;
    }
 
    /**
     * 删除单个key
     * @param string $key 键名
     */
    public function deleteKey($key) {
        $ret = $this->memcache->delete($key);
        return $ret;
    }
 
    /**
     * 删除所有key
     */
    public function deleteAll() {
        return $this->memcache->flush();
    }
     
    /**
     * 返回memcache对象
     * memcache我们只封装了常用的一部分
     * 拿着这个对象就可以直接调用memcache自身方法
     * eg:$memcache->memcacheOtherMethods()->getStats()   getStats方法没封
     */
    public function memcacheOtherMethods() {
        return $this->memcache;
    }
 
    /**
     * 释放
     */
    public function __destruct() {
        $this->memcache->close();
    }
}
```