<?php

namespace Heanup\Frame\Package\Cache;

use Heanup\Frame\Request;

/**
 * 使用Memcache做缓存
 */
class Memcache extends \Heanup\Frame\Package\Cache
{
    const CONFIG_CLASS = 'Memcache\\Core';
    const PREFIX = '';
    const EXPIRE = 259200;

    /**
     * 单个读取
     * @param $id
     * @return array
     */
    public static function get($id)
    {
        if(Request::isCacheEnabled() == false){
            return array();
        }
        $key = self::getKey($id);
        $result = self::getConnection()->get($key);
        return $result;
    }

    /**
     * 批量读取，如果没有数据，则不在返回的列表中
     * @param array $idList
     * @return array
     */
    public static function getBatch(Array $idList)
    {
        if(Request::isCacheEnabled() == false){
            return array();
        }
        $keyList = array();
        foreach ($idList as $id) {
            $keyList[$id] = self::getKey($id);
        }
        $result = self::getConnection()->getMulti($keyList);
        $data = array();
        if ($result) {
            $keyList = array_flip($keyList);
            foreach ($result as $k => $v) {
                $k = $keyList[$k];
                $data[$k] = $v;
            }
        }
        unset($result);
        unset($idList);
        unset($keyList);
        return $data;
    }

    /**
     * @param $id
     * @param $data
     * @param expire
     * @return bool
     */
    public static function set($id, $data, $expire = null)
    {
        $key = self::getKey($id);
        $class = get_called_class();
        if(is_null($expire)){
            $expire = $class::EXPIRE;
        }
        $expire = time() + intval($expire);
        $result = self::getConnection()->set($key, $data, $expire);
        return $result;
    }

    /**
     * 批量设置
     * @param array $data
     * @param $expire
     * @return bool
     */
    public static function setBatch($data, $expire = null)
    {
        $dataList = array();
        foreach ($data as $k => $v) {
            $k = self::getKey($k);
            $dataList[$k] = $v;
        }
        $class = get_called_class();
        if(is_null($expire)){
            $expire = $class::EXPIRE;
        }
        $expire = time() + intval($expire);
        $result = self::getConnection()->setMulti($dataList, $expire);
        unset($data);
        unset($dataList);
        return $result;
    }

    /**
     * 增加
     * @param $id
     * @param int $value
     * @return int
     */
    public static function increase($id, $value = 1)
    {
        $key = self::getKey($id);
        $value = abs($value);
        return self::getConnection()->increment($key, $value);
    }

    /**
     * 减少
     * @param $id
     * @param int $value
     * @return int
     */
    public static function decrease($id, $value = 1)
    {
        $key = self::getKey($id);
        $value = abs($value);
        return self::getConnection()->decrement($key, $value);
    }

    /**
     * 删除
     * @param $id
     * @return bool
     */
    public static function delete($id)
    {
        $key = self::getKey($id);
        return self::getConnection()->delete($key);
    }

    /**
     * 获取连接
     * @return \Memcached
     */
    protected static function getConnection()
    {
        $class = get_called_class();
        $configClass = $class::CONFIG_CLASS;
        $memcached = new \Heanup\Frame\Library\Memcache($configClass);
        $connection = $memcached->getConnection();
        return $connection;
    }

}