CacheDb::set PHP Method

set() public method

写入缓存
public set ( string $name, mixed $value, integer $expire = null ) : boolen
$name string 缓存变量名
$value mixed 存储数据
$expire integer 有效时间(秒)
return boolen
    public function set($name, $value, $expire = null)
    {
        $data = serialize($value);
        $name = $this->options['prefix'] . addslashes($name);
        N('cache_write', 1);
        if (C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
            //数据压缩
            $data = gzcompress($data, 3);
        }
        if (C('DATA_CACHE_CHECK')) {
            //开启数据校验
            $crc = md5($data);
        } else {
            $crc = '';
        }
        if (is_null($expire)) {
            $expire = $this->options['expire'];
        }
        $expire = $expire == 0 ? 0 : time() + $expire;
        //缓存有效期为0表示永久缓存
        $result = $this->handler->query('select `cachekey` from `' . $this->options['table'] . '` where `cachekey`=\'' . $name . '\' limit 0,1');
        if (!empty($result)) {
            //更新记录
            $result = $this->handler->execute('UPDATE ' . $this->options['table'] . ' SET data=\'' . $data . '\' ,datacrc=\'' . $crc . '\',expire=' . $expire . ' WHERE `cachekey`=\'' . $name . '\'');
        } else {
            //新增记录
            $result = $this->handler->execute('INSERT INTO ' . $this->options['table'] . ' (`cachekey`,`data`,`datacrc`,`expire`) VALUES (\'' . $name . '\',\'' . $data . '\',\'' . $crc . '\',' . $expire . ')');
        }
        if ($result) {
            if ($this->options['length'] > 0) {
                // 记录缓存队列
                $this->queue($name);
            }
            return true;
        } else {
            return false;
        }
    }