cache_name = $cache_name; return $this; } /** * 过期时间 * @param int $cache_expire * @return $this */ public function expire(int $cache_expire) { $this->cache_expire = $cache_expire; return $this; } /** * 设置 * @param $cache_value * @return int|string * @throws DtaException */ public function set($cache_value) { $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) ]); if (empty($result)) { return false; } return true; } /** * 获取 * @return string * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException|DtaException */ public function get() { $this->judge(); $cache = Db::table($this->table) ->where('cache_name', $this->cache_name) ->order('id desc') ->whereTime('cache_expire', '>', time()) ->find(); return isset($cache['cache_value']) ? $cache['cache_value'] : ''; } /** * 删除 * @return int * @throws DbException|DtaException */ public function delete() { $this->judge(); $result = Db::table($this->table) ->where('cache_name', $this->cache_name) ->delete(); if (empty($result)) { return false; } return true; } /** * 更新 * @param $cache_value * @return int * @throws DbException|DtaException */ public function update($cache_value) { $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) ]); if (empty($result)) { return false; } return true; } /** * 自增 * @param int $int * @return int * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException|DtaException */ public function inc(int $int = 1) { $cache_value = (int)$this->get(); $result = Db::table($this->table) ->where('cache_name', $this->cache_name) ->update([ 'cache_value' => $cache_value + $int ]); if (empty($result)) { return false; } return true; } /** * 自减 * @param int $int * @return int * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException|DtaException */ public function dec(int $int = 1) { $cache_value = (int)$this->get(); $result = Db::table($this->table) ->where('cache_name', $this->cache_name) ->update([ 'cache_value' => $cache_value - $int ]); if (empty($result)) { return false; } return true; } /** * @throws DtaException */ private function judge() { if (empty($this->cache_name)) { throw new DtaException("名称未配置"); } } }