zoukankan      html  css  js  c++  java
  • PHP和Memcached

    1、Memcached类的介绍

    详见PHP官方文档:点击访问

    2、封装自己的Memcached类库

    <?php
    
    namespace CacheLib;
    
    
    class MemCache
    {
        /**
         * @var Memcached
         * 访问变量可以使用Memcached类库的其他方法
         */
        public $_memcache;
    
        public function __construct($persistentId = null)
        {
            $cache = new Memcached($persistentId);
    
            //判断服务器地址是否为空
            if(!$cache->getServerList())
            {
                //一般从config文件中读取服务器地址
                //添加服务器地址,去掉s只能添加一个服务器地址
                $cache->addServers(
                    array(
                        //ip地址,端口,权重 | 权重越大,该服务越容易被选中
                        array('192.168.3.105', 11211, 60),
                        array('192.168.3.105', 11311, 40),
                    )
                );
            }
    
            //key 前缀的设置
            $cache->setOption(Memcached::OPT_PREFIX_KEY, 'imooc.');
    
            $this->_memcache = $cache;
        }
    
        /**
         * 设置缓存
         * @param string $key 缓存key
         * @param string|array $value
         * @param int $ttl 过期时间
         * @return bool
         */
        public function set($key, $value, $ttl = 3600)
        {
            if(empty($key) || empty($value) || is_numeric($ttl))
            {
                return false;
            }
    
            return $this->_memcache->set($key, $value, $_SERVER['REQUEST_TIME'] + $ttl);
        }
    
        /**
         * 获取缓存
         * @param $key
         * @return bool|mixed
         */
        public function get($key)
        {
            if(empty($key))
            {
                return false;
            }
            return $this->_memcache->get($key);
        }
    
    
        /**
         * 清空缓存处理
         * @param $key
         * @return bool
         */
        public function clean($key)
        {
            if(empty($key))
            {
                return false;
            }
    
            return $this->_memcache->delete($key);
        }
    
        /**
         * 自增处理
         * @param $key
         * @param $offset
         * @return bool|int
         */
        public function incr($key, $offset = 1)
        {
            if(empty($key))
            {
                return false;
            }
            $offset = intval($offset);
            return $this->_memcache->increment($key, $offset);
        }
    
        /**
         * 自减少处理
         * @param $key
         * @param int $offset
         * @return bool|int
         */
        public function decr($key, $offset = 1)
        {
            if(empty($key))
            {
                return false;
            }
            $offset = intval($offset);
            return $this->_memcache->decrement($key, $offset);
        }
    
    
        /**
         * add处理
         * @param $key
         * @param $value
         * @param int $ttl
         * @return bool
         */
        public function add($key, $value, $ttl = 3600)
        {
            if(empty($key) || empty($value) || is_numeric($ttl))
            {
                return false;
            }
    
            return $this->_memcache->add($key, $value, $_SERVER['REQUEST_TIME'] + $ttl);
        }
    
    
    }

    如有错误之处,请纠正。谢谢!

  • 相关阅读:
    行盒(line box)垂直方向的属性详解:从font-size、line-height到vertical-align
    CSS绝对定位的原点:是在border上、padding上还是在content上?
    从知乎首页用户操作入口学习到的CSS技巧
    重置表单中的文件上传控件(file input)的方法
    ASCII, Unicode, UTF-8, 8进制, 16进制等各种编码学习理解笔记
    Yarn的Linking dependencies特别慢的优化方法
    MongoDB学习记录(四)
    MongoDB学习记录(三)
    MongoDB学习记录(二)
    AdBlock——拦截网页广告
  • 原文地址:https://www.cnblogs.com/lzijiangg/p/11978494.html
Copyright © 2011-2022 走看看