使用ThinkPHP框架结合阿里云OCS缓存。

配置文件config.php

      'Aliyunocs',
      'MEMCACHED_SERVER' => 'xxxxxxxx.ocs.aliyuncs.com',//阿里云 OCS Server地址
      'MEMCACHED_PORT' => 11211,//端口
      'MEMCACHED_USERNAME' =>'xxxxxxxx',//阿里云 OCS 账户
      'MEMCACHED_PASSWORD' =>'xxxxxxxx',//阿里云 OCS 密码
    );

Aliyunocs.class.php放在ThinkPHP\Library\Think\Cache\Driver下

     C('MEMCACHED_SERVER') ? C('MEMCACHED_SERVER') : null,
                'port' => C('MEMCACHED_PORT') ? C('MEMCACHED_PORT') : 11211,
                'lib_options' => C('MEMCACHED_LIB') ? C('MEMCACHED_LIB') : null,
                'username'        =>  C('MEMCACHED_USERNAME') ? C('MEMCACHED_USERNAME') : 'username',
                'password'        =>  C('MEMCACHED_PASSWORD') ? C('MEMCACHED_PASSWORD') : 'password',
            ), $options);

            $this->options      =   $options;
            $this->options['expire'] =  isset($options['expire'])?  $options['expire']  :   C('DATA_CACHE_TIME');
            $this->options['prefix'] =  isset($options['prefix'])?  $options['prefix']  :   C('DATA_CACHE_PREFIX');
            $this->options['length'] =  isset($options['length'])?  $options['length']  :   0;

            $this->handler      =   new MemcachedResource;
            $this->handler->setOption(MemcachedResource::OPT_COMPRESSION, false);
            $this->handler->setOption(MemcachedResource::OPT_BINARY_PROTOCOL, true);
            $options['servers'] && $this->handler->addServer($options['servers'],$options['port']);
            $options['username'] && $options['password'] && $this->handler->setSaslAuthData($options['username'],$options['password']);
            $options['lib_options'] && $this->handler->setOptions($options['lib_options']);
        }

        /**
         * 读取缓存
         * @access public
         * @param string $name 缓存变量名
         * @return mixed
         */
        public function get($name)
        {
            N('cache_read',1);
            return $this->handler->get($this->options['prefix'].$name);
        }

        /**
         * 写入缓存
         * @access public
         * @param string $name 缓存变量名
         * @param mixed $value  存储数据
         * @param integer $expire  有效时间(秒)
         * @return boolean
         */
        public function set($name, $value, $expire = null)
        {
            N('cache_write',1);
            if(is_null($expire)) {
                $expire  =  $this->options['expire'];
            }
            $name   =   $this->options['prefix'].$name;
        $results =$this->handler->set($name, $value, $expire);
            if($results) {
                if($this->options['length']>0) {
                    // 记录缓存队列
                    $this->queue($name);
                }
                return $results;
            }
            return false;
        }

        /**
         * 删除缓存
         * @access public
         * @param string $name 缓存变量名
         * @return boolean
         */
        public function rm($name, $ttl = false) {
            $name   =   $this->options['prefix'].$name;
            return $ttl === false ?
                $this->handler->delete($name) :
                $this->handler->delete($name, $ttl);
        }

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

然后直接使用cache()就好了,其他的使用就是按照TP文档来啦。

本作品由 程小白 创作,采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可,可自由转载、引用但需署名作者且注明文章出处。
原文地址:https://www.chengxiaobai.cn/php/thinkphp-configuration-ali-cloud-ocs.html