作者 liyuhang

gx

@@ -84,11 +84,8 @@ class Common @@ -84,11 +84,8 @@ class Common
84 * @method 84 * @method
85 */ 85 */
86 public static function set_user_cache($data = [],$table,$id,$type = 'B'){ 86 public static function set_user_cache($data = [],$table,$id,$type = 'B'){
87 - $user_cache = config('cache.user_is_cache');  
88 - if(!empty($user_cache) && $user_cache['is_cache'] == true){  
89 - $key = 'cache_'.$table.'_'.$id.'_type';  
90 - Cache::store('file')->set($key,$data);  
91 - } 87 + $key = 'cache_'.$table.'_'.$id.'_type';
  88 + Cache::store('file')->set($key,$data,3600);
92 return true; 89 return true;
93 } 90 }
94 91
@@ -98,8 +95,12 @@ class Common @@ -98,8 +95,12 @@ class Common
98 * @author :liyuhang 95 * @author :liyuhang
99 * @method 96 * @method
100 */ 97 */
101 - public static function del_user_cache($table,$id,$type = 'B'){  
102 - $key = 'cache_'.$table.'_'.$id.'_type'; 98 + public static function del_user_cache($table,$id = '',$type = 'B'){
  99 + if (!empty($id)){
  100 + $key = 'cache_'.$table.'_'.$id.'_type';
  101 + }else{
  102 + $key = 'cache_'.$table.'_*';
  103 + }
103 return Cache::store('file')->pull($key); 104 return Cache::store('file')->pull($key);
104 } 105 }
105 } 106 }
@@ -4,9 +4,6 @@ namespace App\Models; @@ -4,9 +4,6 @@ namespace App\Models;
4 4
5 use App\Helper\Common; 5 use App\Helper\Common;
6 use Illuminate\Database\Eloquent\Model; 6 use Illuminate\Database\Eloquent\Model;
7 -use Illuminate\Support\Facades\Cache;  
8 -use Illuminate\Support\Facades\DB;  
9 -  
10 class Base extends Model 7 class Base extends Model
11 { 8 {
12 protected $table = ''; 9 protected $table = '';
@@ -107,11 +104,22 @@ class Base extends Model @@ -107,11 +104,22 @@ class Base extends Model
107 */ 104 */
108 public function read($condition,$files = ['*']) 105 public function read($condition,$files = ['*'])
109 { 106 {
  107 + //读取缓存
  108 + $cache = config('cache.user_is_cache');
  109 + if(($cache['is_cache'] == true) && isset($condition['id'])){
  110 + $info = Common::get_user_cache($this->getTable(),$condition['id']);
  111 + if(!empty($info)){
  112 + return $info;
  113 + }
  114 + }
110 $query = $this->formatQuery($condition); 115 $query = $this->formatQuery($condition);
111 $info = $query->select($files)->first(); 116 $info = $query->select($files)->first();
112 if (empty($info)) { 117 if (empty($info)) {
113 return false; 118 return false;
114 } 119 }
  120 + if(($cache['is_cache'] == true) && isset($condition['id'])){
  121 + Common::set_user_cache($info,$this->getTable(),$condition['id']);
  122 + }
115 $info = $info->toArray(); 123 $info = $info->toArray();
116 return $info; 124 return $info;
117 } 125 }
@@ -136,6 +144,11 @@ class Base extends Model @@ -136,6 +144,11 @@ class Base extends Model
136 $query = $this->formatQuery($condition); 144 $query = $this->formatQuery($condition);
137 $data['updated_at'] = date('Y-m-d H:i:s'); 145 $data['updated_at'] = date('Y-m-d H:i:s');
138 $rs = $query->update($data); 146 $rs = $query->update($data);
  147 + //更新缓存
  148 + $cache = config('cache.user_is_cache');
  149 + if(($cache['is_cache'] == true) && isset($condition['id'])) {
  150 + Common::del_user_cache($this->getTable(), $condition['id']);
  151 + }
139 return $rs; 152 return $rs;
140 } 153 }
141 /** 154 /**
@@ -146,8 +159,7 @@ class Base extends Model @@ -146,8 +159,7 @@ class Base extends Model
146 */ 159 */
147 public function del($condition){ 160 public function del($condition){
148 $query = $this->formatQuery($condition); 161 $query = $this->formatQuery($condition);
149 - $rs = $query->delete();  
150 - return $rs; 162 + return $query->delete();
151 } 163 }
152 /** 164 /**
153 * @name :参数处理查询 165 * @name :参数处理查询