<?php namespace App\Model; use Common\Model\RedisModel; use Illuminate\Database\Eloquent\Model; class SelfSampleClassModel extends Model { protected $connection='self'; protected $table='sample_class'; public $timestamps = false; //获取分类列表,给前端用 public function getSampleClassList(){ $Redis = new RedisModel(); $pre = 'Self_sample_class_list_web'; $Cache = json_decode($Redis->get($pre),true); if(!$Cache){//重新生成缓存 $Cache = $this->where('status', '=', 1)->orderBy('sort', 'desc')->select('id','class_name','class_icon','parent_id') ->limit(100)->get(); if(!$Cache) return false; $Cache = $Cache->toArray(); $Redis->set($pre, json_encode($Cache)); $Redis->expire($pre, 60); } return $Cache; } //获取分类信息 public function getSampleClass($class_id){ $Redis = new RedisModel(); $findCache = json_decode($Redis->hget('Self_sample_class_list', $class_id),true); if(!$findCache){ $find = $this->where('id', '=', $class_id)->select('id','class_name','parent_id','sort','class_icon','status')->first(); if(!$find) return false; $find = $find->toArray(); $findCache = $this->setSampleClassToCache($class_id, $find); } return $findCache; } //将分类信息写进redis public function setSampleClassToCache($id, $data){ $field = ['id','class_name','parent_id','sort','class_icon','status']; $data = array_only($data, $field); $Redis = new RedisModel(); $findCache = json_decode($Redis->hget('Self_sample_class_list', $id),true); if(!empty($findCache)) $data = array_merge($findCache, $data); $Redis->hset('Self_sample_class_list', $id, json_encode($data)); return $data; } }