cache_name = $cache_name; return $this; } /** * 过期时间 * @param int $cache_expire * @return $this */ public function expire(int $cache_expire): self { $this->cache_expire = $cache_expire; return $this; } /** * 设置 * @param $cache_value * @return bool * @throws DtaException */ public function set($cache_value): bool { $this->judge(); $result = Db::table($this->table) ->insert([ 'cache_name' => $this->cache_name, 'cache_value' => $cache_value, 'cache_expire' => Times::dateRear("Y-m-d H:i:s", $this->cache_expire) ]); return $result ? true : false; } /** * 获取 * @return mixed * @throws DtaException */ public function get() { $this->judge(); return Db::table($this->table) ->where('cache_name', $this->cache_name) ->whereTime('cache_expire', '>=', time()) ->order('cache_expire desc') ->value('cache_value', ''); } /** * 删除 * @return bool * @throws DtaException * @throws \think\db\exception\DbException */ public function delete(): bool { $this->judge(); $result = Db::table($this->table) ->where('cache_name', $this->cache_name) ->delete(); return $result ? true : false; } /** * 更新 * @param $cache_value * @return bool * @throws DtaException * @throws \think\db\exception\DbException */ public function update($cache_value): bool { $this->judge(); $result = Db::table($this->table) ->where('cache_name', $this->cache_name) ->update([ 'cache_value' => $cache_value, 'cache_expire' => Times::dateRear("Y-m-d H:i:s", $this->cache_expire) ]); return $result ? true : false; } /** * 自增 * @param int $int * @return bool * @throws DtaException * @throws \think\db\exception\DbException */ public function inc(int $int = 1): bool { $cache_value = (int)$this->get(); $result = Db::table($this->table) ->where('cache_name', $this->cache_name) ->update([ 'cache_value' => $cache_value + $int ]); return $result ? true : false; } /** * 自减 * @param int $int * @return bool * @throws DtaException * @throws \think\db\exception\DbException */ public function dec(int $int = 1): bool { $cache_value = (int)$this->get(); $result = Db::table($this->table) ->where('cache_name', $this->cache_name) ->update([ 'cache_value' => $cache_value - $int ]); return $result ? true : false; } /** * @throws DtaException */ private function judge(): void { if (empty($this->cache_name)) { throw new DtaException("名称未配置"); } } }