增加MySQL事务处理

This commit is contained in:
TOP糯米 2019-09-01 21:48:47 +08:00
parent fb82505a07
commit 43b509b0ea
5 changed files with 47 additions and 6 deletions

View File

@ -373,6 +373,16 @@ class Database
return $result;
}
/**
* MySQL事务
* @param $action
* @return mixed
*/
public function transaction($action)
{
return self::$driver->transaction($action);
}
/**
* 获取最后执行的SQL语句
*

View File

@ -303,6 +303,16 @@ class Model
return $this->getDb()->query($query);
}
/**
* MySQL事务
* @param $action
* @return mixed
*/
public function transaction($action)
{
return $this->getDb()->transaction($action);
}
/**
* 获取最后一次执行的SQL
*

View File

@ -70,10 +70,14 @@ class Redis implements CacheIfs
*/
public function get($key = null, $callable = null)
{
$value = $this->redis->get($key);
$status = $this->exists($key);
$value = $status == 0 ? false : $this->redis->get($key);
// 如果获取不到结果但是callable存在
if ($value === false && is_callable($callable)) {
return $callable($this);
if ($value === false) {
if (is_callable($callable)) {
return $callable($this);
}
return false;
}
// 判断值是否是json字符串
$jsonDecode = json_decode($value, true);

View File

@ -268,6 +268,25 @@ class MySQLi implements DatabaseIfs
return $result;
}
/**
* MySQL事务
* @param $action
* @return bool
* @throws DatabaseException
*/
public function transaction($action)
{
try {
$this->query('start transaction');
$action();
$this->query('commit');
return true;
} catch (DatabaseException $e) {
$this->query('rollback');
throw new DatabaseException($e->getMessage());
}
}
/**
* 获取执行的最后一条SQL
*

View File

@ -19,9 +19,7 @@ class View implements MiddlewareIfs
$cache = File::instance($config['cacheDir']);
if ($cache->exists($ident)) {
$content = $cache->get($ident);
return Response::instance()->header([
'HTTP/1.1 304 Not Modified'
])->dispatch($content);
return Response::instance()->dispatch($content);
}
}
return true;