作者 liyuhang

gx

... ... @@ -84,11 +84,8 @@ class Common
* @method
*/
public static function set_user_cache($data = [],$table,$id,$type = 'B'){
$user_cache = config('cache.user_is_cache');
if(!empty($user_cache) && $user_cache['is_cache'] == true){
$key = 'cache_'.$table.'_'.$id.'_type';
Cache::store('file')->set($key,$data);
}
Cache::store('file')->set($key,$data,3600);
return true;
}
... ... @@ -98,8 +95,12 @@ class Common
* @author :liyuhang
* @method
*/
public static function del_user_cache($table,$id,$type = 'B'){
public static function del_user_cache($table,$id = '',$type = 'B'){
if (!empty($id)){
$key = 'cache_'.$table.'_'.$id.'_type';
}else{
$key = 'cache_'.$table.'_*';
}
return Cache::store('file')->pull($key);
}
}
... ...
... ... @@ -4,9 +4,6 @@ namespace App\Models;
use App\Helper\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
class Base extends Model
{
protected $table = '';
... ... @@ -107,11 +104,22 @@ class Base extends Model
*/
public function read($condition,$files = ['*'])
{
//读取缓存
$cache = config('cache.user_is_cache');
if(($cache['is_cache'] == true) && isset($condition['id'])){
$info = Common::get_user_cache($this->getTable(),$condition['id']);
if(!empty($info)){
return $info;
}
}
$query = $this->formatQuery($condition);
$info = $query->select($files)->first();
if (empty($info)) {
return false;
}
if(($cache['is_cache'] == true) && isset($condition['id'])){
Common::set_user_cache($info,$this->getTable(),$condition['id']);
}
$info = $info->toArray();
return $info;
}
... ... @@ -136,6 +144,11 @@ class Base extends Model
$query = $this->formatQuery($condition);
$data['updated_at'] = date('Y-m-d H:i:s');
$rs = $query->update($data);
//更新缓存
$cache = config('cache.user_is_cache');
if(($cache['is_cache'] == true) && isset($condition['id'])) {
Common::del_user_cache($this->getTable(), $condition['id']);
}
return $rs;
}
/**
... ... @@ -146,8 +159,7 @@ class Base extends Model
*/
public function del($condition){
$query = $this->formatQuery($condition);
$rs = $query->delete();
return $rs;
return $query->delete();
}
/**
* @name :参数处理查询
... ...