> For the complete documentation index, see [llms.txt](https://xiaoxiami.gitbook.io/php/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xiaoxiami.gitbook.io/php/chapter1/dan-li-mo-shi/chang-666f-memcache.md).

# 场景\*memcache操作类

memcache操作类

在实际的PHP项目中，应用单例模式可以应用到网络连接的。比如Mysql、Memcache、Redis连接等等，一般的需求通常Redis，Memcache都只有一台服务器，所以用单例模式将连接封装到getInstance()，这样做的好处是不用每次都去调用connect()方法，减少网络连接开销。PHP大部分情况是单线程同步执行的，所以整个程序只用实例化一个Redis对象即可。

Memcache.php

```php
<?php
class MemcacheClass
{
    private static $instance = null;

    private $handler;

    private function __construct($config)
    {
        if(!isset($config['host']))
        {
            $config['host'] = '127.0.0.1';
        }

        if(!isset($config['port']))
        {
            $config['port'] = 11211;
        }

        if(!isset($config['timeout']))
        {
            $config['timeout'] = 1;
        }

        $this->handler = new Memcache();
        $this->handler->connect($config['host'], $config['port'], $config['timeout']);
    }

    public static function init($config = array())
    {
        if (!self::$instance) {
            self::$instance = new self($config);
        }
        return self::$instance;
    }

    /**
     * get cache
     * @access public
     * @param string $name
     * @return mixed
     */
    public function get($name)
    {
        return $this->handler->get($name);
    }

    /**
     * set cache
     * @access public
     * @param string $name 
     * @param mixed $value
     * @param integer $expire
     * @return boolean
     */
    public function set($name, $value, $expire = 0)
    {
        $this->handler->set($name, $value, MEMCACHE_COMPRESSED, $expire);
    }

    /**
     * delete cache
     * @access public
     * @param string $name
     * @return boolean
     */
    public function rm($name, $ttl = false)
    {
        return $ttl === false ?
        $this->handler->delete($name) :
        $this->handler->delete($name, $ttl);
    }

    /**
     * flush cache
     * @access public
     * @return boolean
     */
    public function clear()
    {
        return $this->handler->flush();
    }

    function __destruct()
    {
        $this->handler->close();
    }
}
```

test.php

```php
include 'Memcache.class.php';

$cache = MemcacheClass::init();
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/php/chapter1/dan-li-mo-shi/chang-666f-memcache.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
