* [bug#57811,1h] fix the inconsistency issue between cache data and database.
This commit is contained in:
Vendored
+35
-2
@@ -68,6 +68,15 @@ class cache
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
/**
|
||||
* 缓存状态。
|
||||
* Cache status.
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $status = 'enabled';
|
||||
|
||||
/**
|
||||
* 缓存命名空间。
|
||||
* Cache namespace.
|
||||
@@ -177,6 +186,19 @@ class cache
|
||||
if($driver == self::DRIVER_FILE) return $this->cache = new $className($namespace, $lifetime, $app->getCacheRoot());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置缓存状态。
|
||||
* Set cache status.
|
||||
*
|
||||
* @param string $status 缓存状态。enabled: 启用缓存;disabled: 禁用缓存。
|
||||
* @access private
|
||||
* @return void
|
||||
*/
|
||||
private function setStatus(string $status)
|
||||
{
|
||||
$this->status = $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置缓存命名空间。
|
||||
* Set cache namespace.
|
||||
@@ -599,8 +621,9 @@ class cache
|
||||
* @access public
|
||||
* @return object|false
|
||||
*/
|
||||
public function fetch(string $table, int|string $id): object|bool
|
||||
public function fetch(string $table, int|string $id): object|null
|
||||
{
|
||||
if($this->status == 'disabled') return null;
|
||||
if(!$this->checkTable($table)) return $this->log("Table {$table} is not set in the cache configuration", __FILE__, __LINE__);
|
||||
|
||||
if(empty($table)) return $this->log('The table name is empty', __FILE__, __LINE__);
|
||||
@@ -628,6 +651,7 @@ class cache
|
||||
*/
|
||||
public function fetchAll(string $table, array $objectIdList = []): array
|
||||
{
|
||||
if($this->status == 'disabled') return [];
|
||||
if(!$this->checkTable($table)) return $this->log("Table {$table} is not set in the cache configuration", __FILE__, __LINE__);
|
||||
|
||||
if(empty($table)) return $this->log('The table name is empty', __FILE__, __LINE__);
|
||||
@@ -751,6 +775,7 @@ class cache
|
||||
*/
|
||||
public function get()
|
||||
{
|
||||
if($this->status == 'disabled') return null;
|
||||
if(empty($this->key)) return $this->log('The key is empty', __FILE__, __LINE__);
|
||||
return $this->cache->get($this->key);
|
||||
}
|
||||
@@ -765,6 +790,7 @@ class cache
|
||||
*/
|
||||
public function getByKey(string $key)
|
||||
{
|
||||
if($this->status == 'disabled') return null;
|
||||
if(empty($key)) return $this->log('The key is empty', __FILE__, __LINE__);
|
||||
|
||||
return $this->cache->get($key);
|
||||
@@ -780,6 +806,7 @@ class cache
|
||||
*/
|
||||
public function save($value)
|
||||
{
|
||||
if($this->status == 'disabled') return false;
|
||||
if(empty($this->key)) return $this->log('The key is empty', __FILE__, __LINE__);
|
||||
return $this->cache->set($this->key, $value);
|
||||
}
|
||||
@@ -796,6 +823,7 @@ class cache
|
||||
*/
|
||||
public function saveByKey(string $key, $value, int $ttl = 0)
|
||||
{
|
||||
if($this->status == 'disabled') return false;
|
||||
if(empty($key)) return $this->log('The key is empty', __FILE__, __LINE__);
|
||||
|
||||
return $this->cache->set($key, $value, $ttl);
|
||||
@@ -812,6 +840,7 @@ class cache
|
||||
*/
|
||||
public function saveByLabel(string $label, $value)
|
||||
{
|
||||
if($this->status == 'disabled') return false;
|
||||
if(empty($label)) return $this->log('The label is empty', __FILE__, __LINE__);
|
||||
if(empty($this->labels[$label])) return $this->log("Label {$label} does not exist", __FILE__, __LINE__);
|
||||
|
||||
@@ -848,6 +877,7 @@ class cache
|
||||
{
|
||||
$this->reset();
|
||||
|
||||
if($this->status == 'disabled') return;
|
||||
if(!$this->checkTable($table)) return;
|
||||
|
||||
if(empty($table)) return $this->log('Table name is required.', __FILE__, __LINE__);
|
||||
@@ -901,6 +931,7 @@ class cache
|
||||
*/
|
||||
public function sync()
|
||||
{
|
||||
if($this->status == 'disabled') return;
|
||||
if(!$this->checkTable()) return;
|
||||
|
||||
if(empty($this->table)) return $this->log('Table name is required.', __FILE__, __LINE__);
|
||||
@@ -922,7 +953,9 @@ class cache
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
return $this->cache->clear();
|
||||
$this->setStatus('disabled');
|
||||
$this->cache->clear();
|
||||
$this->setStatus('normal');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Vendored
+2
-2
@@ -74,7 +74,7 @@ class cache extends control
|
||||
$this->loadModel('setting')->setItems('system.common.cache', $cache);
|
||||
$this->setting->setItems('system.common.redis', $redis);
|
||||
|
||||
$this->cache->clear($cache->enable);
|
||||
$this->cache->clear();
|
||||
|
||||
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => true));
|
||||
}
|
||||
@@ -99,7 +99,7 @@ class cache extends control
|
||||
*/
|
||||
public function flush()
|
||||
{
|
||||
$this->cache->clear($this->config->cache->enable);
|
||||
$this->cache->clear();
|
||||
return $this->send(array('result' => 'success', 'message' => $this->lang->cache->clearSuccess, 'load' => true));
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+1
-7
@@ -18,14 +18,8 @@ class cacheModel extends model
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function clear($needStart = true)
|
||||
public function clear()
|
||||
{
|
||||
/* 多应用共享时采用遍历删除的方式,所以需要先关闭缓存,清空之后再打开。When multiple applications share the cache, the cache needs to be closed first, cleared, and then opened. */
|
||||
$needStop = $this->config->cache->enable && $this->config->cache->scope == 'shared';
|
||||
if($needStop) $this->loadModel('setting')->setItem('system.common.cache.enable', 0);
|
||||
|
||||
$this->mao->clearCache();
|
||||
|
||||
if($needStop && $needStart) $this->loadModel('setting')->setItem('system.common.cache.enable', 1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user