<?php
namespace Heanup\Frame\Library;

/**
 * 缓存设置，使用memcache缓存数据
 */
class Memcached extends \Heanup\Frame\Library
{

    /**
     * 获取连接池
     * 
     * @return \Memcached
     */
    public function getConnection()
    {
        static $pool = array();
        $connectionName = $this->getFlag(true);
        if (! isset($pool[$connectionName]) || ($pool[$connectionName] == false)) {
            $cfg = $this->getConfig();
            $pool[$connectionName] = $this->connect($cfg);
        }
        return $pool[$connectionName];
    }

    /**
     * 连接Memcache
     * 
     * @param
     *            $cfg
     * @return \Memcached
     */
    protected function connect($cfg)
    {
        $connection = new \Memcache();
        foreach ($cfg as $val) {
            $connection->addServer($val['host'], $val['port']);
        }
        return $connection;
    }
}
