From a3910b7afd0952164f9e0b78febf34850e8d25aa Mon Sep 17 00:00:00 2001 From: liugang Date: Fri, 1 Nov 2024 10:03:55 +0800 Subject: [PATCH] + [refac] add method to get the data of the specified table and id from the cache. --- lib/cache/cache.class.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/cache/cache.class.php b/lib/cache/cache.class.php index ea2ef01036..47f41251f5 100644 --- a/lib/cache/cache.class.php +++ b/lib/cache/cache.class.php @@ -531,6 +531,33 @@ class cache return $objects; } + /** + * 从缓存中获取指定表指定 id 的数据。 + * Get the data of the specified table and id from the cache. + * + * @param string $table 表名。 + * @param int|string $id 主键值。 + * @access public + * @return object|false + */ + public function fetch(string $table, int|string $id): object|bool + { + if(!$this->checkTable($table)) return $this->log("The {$table} table is not set in the cache configuration", __FILE__, __LINE__); + + if(empty($table)) return $this->log('The table name is empty', __FILE__, __LINE__); + if(empty($id)) return $this->log('The id is empty', __FILE__, __LINE__); + + $this->setTable($table); + + $code = $this->getTableCode(); + $key = $this->getRawCacheKey($code, $id); + $object = $this->cache->get($key); + if($object) return $object; + + $objects = $this->initTableCache(); + return isset($objects[$id]) ? $objects[$id] : false; + } + /** * 从缓存中获取指定表的所有数据。 * Get all data of the specified table from the cache.