memcached封装类

<?php
/**
  * memcache 操作
 */
class MemcachedTool{
    /**
     * 配置信息
     *
     * @var unknown_type
     */
    private $config;

    /**
     * 键值前缀(区分各业务层)
     *
     * @var string
     */
    private $type;

    /**
     * 键值前缀(系统级别)
     *
     * @var string
     */
    private $prefix;

    public function __construct(){
        $this->config = C('memcache');
        if (!extension_loaded('memcached') || !is_array($this->config[1])) {
                throw_exception('memcached failed to load');
        }
        $this->init();
    }

    /**
     * 初始化
     * @return void
     */
    private function init(){
        $this->prefix= $this->config['prefix'] ? $this->config['prefix'] : substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_';
        $this->handler = new Memcached;
        $this->enable = $this->handler->addServer($this->config[1]['host'], $this->config[1]['port']);
    }

    /**
     * 设置值
     *
     * @param mixed $key
     * @param mixed $value
     * @param string $type
     * @param int $ttl
     * @return bool
     */
    public function set($key, $value, $type='', $time=86400){
        if (!$this->enable) return false;
        $this->type = $type;
        return $this->handler->set($this->_key($key), $value, $time);
    }
    
    /**
     * 追加值
     *
     * @param mixed $key
     * @param mixed $value
     * @param string $type
     * @param int $ttl
     * @return bool
     */
    public function add($key, $value, $type='', $time=86400){
        if (!$this->enable) return false;
        $this->type = $type;
        return $this->handler->add($this->_key($key), $value, $time);
    }

    /**
     * 取得值
     *
     * @param mixed $key
     * @param mixed $type
     * @return bool
     */
    public function get($key, $type=''){
        if (!$this->enable) return false;
        $this->type = $type;
        return $this->handler->get($this->_key($key));
    }

    /**
     * 删除值
     *
     * @param mixed $key
     * @param mixed $type
     * @return bool
     */
    public function rm($key, $type=''){
        $this->type = $type;
        return $this->handler->delete($this->_key($key));
    }

    /**
     * 清空值
     *
     * @return bool
     */
    public function clear(){
        return $this->handler->flush();
    }

    /**
     * 加前缀
     *
     * @param string $str
     * @return string
     */
    private function _key($str) {
        return $this->prefix.$this->type.$str;
    }

    /**
     * 递增
     *
     * @param string $key
     * @param int $step
     * @return int/false
     */
    public function inc($key, $step = 1) {
        return $this->handler->increment($this->_key($key), $step);
    }

    /**
     * 递减
     *
     * @param string $key
     * @param int $step
     * @return int/false
     */
    public function dec($key, $step = 1) {
        return $this->handler->decrement($this->_key($key), $step);
    }



}

memcached方法使用查询http://php.net/manual/zh/class.memcached.php

转载于:https://www.cnblogs.com/Dong-Ge/articles/9166272.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值