mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 23:52:49 +08:00
改进 traits\model\adv 的延迟更新数据方法 和 app类对于 空操作的处理
This commit is contained in:
@@ -122,7 +122,9 @@ class App
|
|||||||
// 操作不存在
|
// 操作不存在
|
||||||
if (method_exists($instance, '_empty')) {
|
if (method_exists($instance, '_empty')) {
|
||||||
$method = new \ReflectionMethod($instance, '_empty');
|
$method = new \ReflectionMethod($instance, '_empty');
|
||||||
$method->invokeArgs($instance, [$action, '']);
|
$data = $method->invokeArgs($instance, [$action, '']);
|
||||||
|
// 返回数据
|
||||||
|
Response::returnData($data, $config['default_return_type'], $config['response_exit']);
|
||||||
} else {
|
} else {
|
||||||
throw new Exception('method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists ', 10002);
|
throw new Exception('method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists ', 10002);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ class Url
|
|||||||
$suffix = substr($suffix, 0, $pos);
|
$suffix = substr($suffix, 0, $pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0 === strpos($suffix,'.') ? $suffix : (empty($suffix)? $suffix: '.'.$suffix);
|
return (empty($suffix) || 0 === strpos($suffix, '.')) ? $suffix : '.' . $suffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据路由名称和参数生成URL地址
|
// 根据路由名称和参数生成URL地址
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace traits\model;
|
namespace traits\model;
|
||||||
|
|
||||||
|
use think\Cache;
|
||||||
use think\Lang;
|
use think\Lang;
|
||||||
use think\Loader;
|
use think\Loader;
|
||||||
|
|
||||||
@@ -69,26 +70,58 @@ trait Adv
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字段值增长
|
* 字段值(延迟)增长
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $field 字段名
|
* @param string $field 字段名
|
||||||
* @param integer $step 增长值
|
* @param integer $step 增长值
|
||||||
|
* @param integer $lazyTime 延时时间(s)
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function setInc($field, $step = 1)
|
public function setInc($field, $step = 1, $lazyTime = 0)
|
||||||
{
|
{
|
||||||
|
$condition = $this->options['where'];
|
||||||
|
if (empty($condition)) {
|
||||||
|
// 没有条件不做任何更新
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ($lazyTime > 0) {
|
||||||
|
// 延迟写入
|
||||||
|
$guid = md5($this->name . '_' . $field . '_' . serialize($condition));
|
||||||
|
$step = $this->lazyWrite($guid, $step, $lazyTime);
|
||||||
|
if (empty($step)) {
|
||||||
|
return true; // 等待下次写入
|
||||||
|
} elseif ($step < 0) {
|
||||||
|
$step = '-' . $step;
|
||||||
|
}
|
||||||
|
}
|
||||||
return $this->setField($field, ['exp', $field . '+' . $step]);
|
return $this->setField($field, ['exp', $field . '+' . $step]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字段值减少
|
* 字段值(延迟)减少
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $field 字段名
|
* @param string $field 字段名
|
||||||
* @param integer $step 减少值
|
* @param integer $step 减少值
|
||||||
|
* @param integer $lazyTime 延时时间(s)
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function setDec($field, $step = 1)
|
public function setDec($field, $step = 1, $lazyTime = 0)
|
||||||
{
|
{
|
||||||
|
$condition = $this->options['where'];
|
||||||
|
if (empty($condition)) {
|
||||||
|
// 没有条件不做任何更新
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ($lazyTime > 0) {
|
||||||
|
// 延迟写入
|
||||||
|
$guid = md5($this->name . '_' . $field . '_' . serialize($condition));
|
||||||
|
$step = $this->lazyWrite($guid, -$step, $lazyTime);
|
||||||
|
if (empty($step)) {
|
||||||
|
return true; // 等待下次写入
|
||||||
|
} elseif ($step > 0) {
|
||||||
|
$step = '-' . $step;
|
||||||
|
}
|
||||||
|
}
|
||||||
return $this->setField($field, ['exp', $field . '-' . $step]);
|
return $this->setField($field, ['exp', $field . '-' . $step]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,6 +137,15 @@ trait Adv
|
|||||||
$options['field'] = $field;
|
$options['field'] = $field;
|
||||||
$options = $this->_parseOptions($options);
|
$options = $this->_parseOptions($options);
|
||||||
$field = trim($field);
|
$field = trim($field);
|
||||||
|
// 判断查询缓存
|
||||||
|
if (isset($options['cache'])) {
|
||||||
|
$cache = $options['cache'];
|
||||||
|
$key = is_string($cache['key']) ? $cache['key'] : md5($sepa . serialize($options));
|
||||||
|
$data = Cache::get($key);
|
||||||
|
if (false !== $data) {
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (strpos($field, ',')) {
|
if (strpos($field, ',')) {
|
||||||
// 多字段
|
// 多字段
|
||||||
if (!isset($options['limit'])) {
|
if (!isset($options['limit'])) {
|
||||||
@@ -111,19 +153,26 @@ trait Adv
|
|||||||
}
|
}
|
||||||
$resultSet = $this->db->select($options);
|
$resultSet = $this->db->select($options);
|
||||||
if (!empty($resultSet)) {
|
if (!empty($resultSet)) {
|
||||||
|
if (is_string($resultSet)) {
|
||||||
|
return $resultSet;
|
||||||
|
}
|
||||||
|
$_field = explode(',', $field);
|
||||||
$field = array_keys($resultSet[0]);
|
$field = array_keys($resultSet[0]);
|
||||||
$key = array_shift($field);
|
$key1 = array_shift($field);
|
||||||
$key2 = array_shift($field);
|
$key2 = array_shift($field);
|
||||||
$cols = [];
|
$cols = array();
|
||||||
$count = count(explode(',', $field));
|
$count = count($_field);
|
||||||
foreach ($resultSet as $result) {
|
foreach ($resultSet as $result) {
|
||||||
$name = $result[$key];
|
$name = $result[$key1];
|
||||||
if (2 == $count) {
|
if (2 == $count) {
|
||||||
$cols[$name] = $result[$key2];
|
$cols[$name] = $result[$key2];
|
||||||
} else {
|
} else {
|
||||||
$cols[$name] = is_string($sepa) ? implode($sepa, $result) : $result;
|
$cols[$name] = is_string($sepa) ? implode($sepa, array_slice($result, 1)) : $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (isset($cache)) {
|
||||||
|
Cache::set($key, $cols, $cache['expire']);
|
||||||
|
}
|
||||||
return $cols;
|
return $cols;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -135,72 +184,28 @@ trait Adv
|
|||||||
}
|
}
|
||||||
$result = $this->db->select($options);
|
$result = $this->db->select($options);
|
||||||
if (!empty($result)) {
|
if (!empty($result)) {
|
||||||
|
if (is_string($result)) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
if (true !== $sepa && 1 == $options['limit']) {
|
if (true !== $sepa && 1 == $options['limit']) {
|
||||||
return reset($result[0]);
|
$data = reset($result[0]);
|
||||||
|
if (isset($cache)) {
|
||||||
|
Cache::set($key, $data, $cache['expire']);
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
}
|
}
|
||||||
foreach ($result as $val) {
|
foreach ($result as $val) {
|
||||||
$array[] = $val[$field];
|
$array[] = $val[$field];
|
||||||
}
|
}
|
||||||
|
if (isset($cache)) {
|
||||||
|
Cache::set($key, $array, $cache['expire']);
|
||||||
|
}
|
||||||
return $array;
|
return $array;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 字段值延迟增长
|
|
||||||
* @access public
|
|
||||||
* @param string $field 字段名
|
|
||||||
* @param integer $step 增长值
|
|
||||||
* @param integer $lazyTime 延时时间(s)
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
public function setLazyInc($field, $step = 1, $lazyTime = 0)
|
|
||||||
{
|
|
||||||
$condition = $this->options['where'];
|
|
||||||
if (empty($condition)) {
|
|
||||||
// 没有条件不做任何更新
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if ($lazyTime > 0) {
|
|
||||||
// 延迟写入
|
|
||||||
$guid = md5($this->name . '_' . $field . '_' . serialize($condition));
|
|
||||||
$step = $this->lazyWrite($guid, $step, $lazyTime);
|
|
||||||
if (false === $step) {
|
|
||||||
// 等待下次写入
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $this->setField($field, ['exp', $field . '+' . $step]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 字段值延迟减少
|
|
||||||
* @access public
|
|
||||||
* @param string $field 字段名
|
|
||||||
* @param integer $step 减少值
|
|
||||||
* @param integer $lazyTime 延时时间(s)
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
public function setLazyDec($field, $step = 1, $lazyTime = 0)
|
|
||||||
{
|
|
||||||
$condition = $this->options['where'];
|
|
||||||
if (empty($condition)) {
|
|
||||||
// 没有条件不做任何更新
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if ($lazyTime > 0) {
|
|
||||||
// 延迟写入
|
|
||||||
$guid = md5($this->name . '_' . $field . '_' . serialize($condition));
|
|
||||||
$step = $this->lazyWrite($guid, $step, $lazyTime);
|
|
||||||
if (false === $step) {
|
|
||||||
// 等待下次写入
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $this->setField($field, ['exp', $field . '-' . $step]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 延时更新检查 返回false表示需要延时
|
* 延时更新检查 返回false表示需要延时
|
||||||
* 否则返回实际写入的数值
|
* 否则返回实际写入的数值
|
||||||
@@ -212,23 +217,23 @@ trait Adv
|
|||||||
*/
|
*/
|
||||||
protected function lazyWrite($guid, $step, $lazyTime)
|
protected function lazyWrite($guid, $step, $lazyTime)
|
||||||
{
|
{
|
||||||
if (false !== ($value = F($guid))) {
|
if (false !== ($value = Cache::get($guid))) {
|
||||||
// 存在缓存写入数据
|
// 存在缓存写入数据
|
||||||
if (time() > S($guid . '_time') + $lazyTime) {
|
if (NOW_TIME > Cache::get($guid . '_time') + $lazyTime) {
|
||||||
// 延时更新时间到了,删除缓存数据 并实际写入数据库
|
// 延时更新时间到了,删除缓存数据 并实际写入数据库
|
||||||
S($guid, null);
|
Cache::rm($guid);
|
||||||
S($guid . '_time', null);
|
Cache::rm($guid . '_time');
|
||||||
return $value + $step;
|
return $value + $step;
|
||||||
} else {
|
} else {
|
||||||
// 追加数据到缓存
|
// 追加数据到缓存
|
||||||
S($guid, $value + $step);
|
Cache::set($guid, $value + $step);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 没有缓存数据
|
// 没有缓存数据
|
||||||
S($guid, $step);
|
Cache::set($guid, $step);
|
||||||
// 计时开始
|
// 计时开始
|
||||||
S($guid . '_time', time());
|
Cache::set($guid . '_time', NOW_TIME);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user