tag() public static method

缓存标签
public static tag ( string $name, string | array $keys = null, boolean $overlay = false ) : Driver
$name string 标签名
$keys string | array 缓存标识
$overlay boolean 是否覆盖
return think\cache\Driver
コード例 #1
0
ファイル: helper.php プロジェクト: pangPython/iNewsCMS
 /**
  * 缓存管理
  * @param mixed     $name 缓存名称,如果为数组表示进行缓存设置
  * @param mixed     $value 缓存值
  * @param mixed     $options 缓存参数
  * @param string    $tag 缓存标签
  * @return mixed
  */
 function cache($name, $value = '', $options = null, $tag = null)
 {
     if (is_array($options)) {
         // 缓存操作的同时初始化
         Cache::connect($options);
     } elseif (is_array($name)) {
         // 缓存初始化
         return Cache::connect($name);
     }
     if ('' === $value) {
         // 获取缓存
         return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name);
     } elseif (is_null($value)) {
         // 删除缓存
         return Cache::rm($name);
     } else {
         // 缓存数据
         if (is_array($options)) {
             $expire = isset($options['expire']) ? $options['expire'] : null;
             //修复查询缓存无法设置过期时间
         } else {
             $expire = is_numeric($options) ? $options : null;
             //默认快捷缓存设置过期时间
         }
         if (is_null($tag)) {
             return Cache::set($name, $value, $expire);
         } else {
             return Cache::tag($tag)->set($name, $value, $expire);
         }
     }
 }
コード例 #2
0
ファイル: Query.php プロジェクト: Dragonbuf/god-s_place
 /**
  * 查找单条记录
  * @access public
  * @param array|string|Query|\Closure $data
  * @return array|false|\PDOStatement|string|Model
  * @throws DbException
  * @throws ModelNotFoundException
  * @throws DataNotFoundException
  */
 public function find($data = null)
 {
     if ($data instanceof Query) {
         return $data->find();
     } elseif ($data instanceof \Closure) {
         call_user_func_array($data, [&$this]);
         $data = null;
     }
     // 分析查询表达式
     $options = $this->parseExpress();
     if (!is_null($data)) {
         // AR模式分析主键条件
         $this->parsePkWhere($data, $options);
     }
     $options['limit'] = 1;
     $result = false;
     if (empty($options['fetch_sql']) && !empty($options['cache'])) {
         // 判断查询缓存
         $cache = $options['cache'];
         if (true === $cache['key'] && !is_null($data) && !is_array($data)) {
             $key = 'think:' . $options['table'] . '|' . $data;
         } else {
             $key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
         }
         $result = Cache::get($key);
     }
     if (!$result) {
         // 生成查询SQL
         $sql = $this->builder()->select($options);
         // 获取参数绑定
         $bind = $this->getBind();
         if ($options['fetch_sql']) {
             // 获取实际执行的SQL语句
             return $this->connection->getRealSql($sql, $bind);
         }
         // 执行查询
         $result = $this->query($sql, $bind, $options['master'], $options['fetch_class']);
         if ($result instanceof \PDOStatement) {
             // 返回PDOStatement对象
             return $result;
         }
         if (isset($cache)) {
             // 缓存数据
             if (isset($cache['tag'])) {
                 Cache::tag($cache['tag'])->set($key, $result, $cache['expire']);
             } else {
                 Cache::set($key, $result, $cache['expire']);
             }
         }
     }
     // 数据处理
     if (!empty($result[0])) {
         $data = $result[0];
         if (!empty($this->model)) {
             // 返回模型对象
             $model = $this->model;
             $data = new $model($data);
             $data->isUpdate(true, isset($options['where']['AND']) ? $options['where']['AND'] : null);
             if ($this->allowField) {
                 $data->allowField($this->allowField);
             }
             // 关联查询
             if (!empty($options['relation'])) {
                 $data->relationQuery($options['relation']);
             }
             if (!empty($options['with'])) {
                 // 预载入
                 $data->eagerlyResult($data, $options['with'], is_object($result) ? get_class($result) : '');
             }
         }
     } elseif (!empty($options['fail'])) {
         $this->throwNotFound($options);
     } else {
         $data = null;
     }
     return $data;
 }