123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- <?php
- namespace App\Model;
- use Cache;
- use Closure;
- use DateTime;
- use Illuminate\Pagination\Paginator;
- /**
- * 数据库模型 缓存读取
- *
- * Class DBCache
- * @package App\Model
- */
- class DBCache
- {
- protected $table = null;
- protected $attributes = [];
- protected $builder = null;
- private $__cache = true; //启用缓存(默认启用)
- private $__cacheKeyname = null; //缓存名称(默认自动生成)
- private $__cacheMinutes = 1; //缓存时间(分钟, 默认1分钟)
- private $__removeCache = false; //删除缓存
- private $__join = [];
- private $__take = [];
- private $__where = [];
- private $__whereRaw = [];
- private $__whereIn = [];
- private $__select = [];
- private $__selectRaw = [];
- private $__orderBy = [];
- private $__orderByRaw = [];
- private $__inRandomOrder = false;
- public function __construct($tabel = null, array $attributes = [])
- {
- $this->initParameter();
- if ($tabel) {
- $this->table = $tabel;
- }
- if ($attributes) {
- $this->attributes = $attributes;
- }
- }
- public function setTable($table)
- {
- $this->table = $table;
- return $this;
- }
- public function setAttributes($attributes)
- {
- $this->attributes = $attributes;
- return $this;
- }
- /**
- * 是否缓存
- *
- * @param bool $isCache
- * @return $this
- */
- public function cache($isCache = true)
- {
- $this->__cache = $isCache ? true : false;
- return $this;
- }
- public function noCache()
- {
- $this->__cache = false;
- return $this;
- }
- /**
- * 缓存名称
- *
- * @param $name
- * @return $this
- */
- public function cacheKeyname($name)
- {
- $this->__cacheKeyname = $name ?: null;
- return $this;
- }
- /**
- * 缓存时间(分钟)
- *
- * @param $Minutes
- * @return $this
- */
- public function cacheMinutes($Minutes)
- {
- if ($Minutes === 'year') {
- $Minutes = 365 * 24 * 60;
- }elseif ($Minutes === 'month') {
- $Minutes = 30 * 24 * 60;
- }elseif ($Minutes === 'day') {
- $Minutes = 24 * 60;
- }elseif ($Minutes === 'hour') {
- $Minutes = 60;
- }
- if ($Minutes instanceof DateTime) {
- $this->__cacheMinutes = $Minutes;
- }else{
- $this->__cacheMinutes = max($Minutes, 1);
- }
- return $this;
- }
- /**
- * 删除缓存
- */
- public function removeCache()
- {
- $this->__removeCache = true;
- return $this;
- }
- public function forgetCache($cacheKey)
- {
- $this->__removeCache = false;
- return Cache::forget($cacheKey);
- }
- /* ******************************************************************************** */
- /* ******************************************************************************** */
- public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false)
- {
- $this->__join[] = [
- $table,
- $first,
- $operator,
- $second,
- $type,
- $where
- ];
- return $this;
- }
- public function take($var)
- {
- $this->__take[] = $var;
- return $this;
- }
- public function where($column, $operator = null, $value = null, $boolean = 'and')
- {
- $this->__where[] = [
- $column,
- $operator,
- $value,
- $boolean
- ];
- return $this;
- }
- public function whereRaw($sql, $bindings = [], $boolean = 'and')
- {
- if ($sql === null) {
- return $this;
- }
- $this->__whereRaw[] = [
- $sql,
- $bindings,
- $boolean
- ];
- return $this;
- }
- public function whereIn($column, $values, $boolean = 'and', $not = false)
- {
- $this->__whereIn[] = [
- $column,
- $values,
- $boolean,
- $not
- ];
- return $this;
- }
- public function whereNotIn($column, $values, $boolean = 'and')
- {
- return $this->whereIn($column, $values, $boolean, true);
- }
- public function select($var = ['*'])
- {
- $this->__select[] = is_array($var) ? $var : func_get_args();
- return $this;
- }
- public function selectRaw($expression, array $bindings = [])
- {
- $this->__selectRaw[] = [
- $expression,
- $bindings
- ];
- return $this;
- }
- public function orderBy($column, $direction = 'asc')
- {
- $this->__orderBy[] = [
- $column,
- $direction
- ];
- return $this;
- }
- public function orderByDesc($column)
- {
- return $this->orderBy($column, 'desc');
- }
- public function orderByRaw($sql, $bindings = [])
- {
- $this->__orderByRaw[] = [
- $sql,
- $bindings
- ];
- return $this;
- }
- public function inRandomOrder($isRand = true)
- {
- $this->__inRandomOrder = $isRand?true:false;
- return $this;
- }
- /* ******************************************************************************** */
- /* ******************************************************************************** */
- /* ******************************************************************************** */
- /**
- * 初始化参数
- */
- private function initParameter() {
- $this->__cache = true;
- $this->__cacheKeyname = null;
- $this->__cacheMinutes = 1;
- $this->__removeCache = false;
- //
- $this->__join = [];
- $this->__take = [];
- $this->__where = [];
- $this->__whereRaw = [];
- $this->__whereIn = [];
- $this->__select = [];
- $this->__selectRaw = [];
- $this->__orderBy = [];
- $this->__orderByRaw = [];
- $this->__inRandomOrder = false;
- }
- /**
- * 获取缓存标识
- * @param string $type 查询方式
- * @param string $attach 附加标识
- * @return string
- */
- private function identify($type, $attach = '') {
- if ($this->__cacheKeyname) {
- return $this->__cacheKeyname;
- }
- //
- $identify = $this->addEncode($this->attributes);
- $identify.= $this->addEncode($this->__join);
- $identify.= $this->addEncode($this->__take);
- $identify.= $this->addEncode($this->__where);
- $identify.= $this->addEncode($this->__whereRaw);
- $identify.= $this->addEncode($this->__whereIn);
- $identify.= $this->addEncode($this->__select);
- $identify.= $this->addEncode($this->__selectRaw);
- $identify.= $this->addEncode($this->__orderBy);
- $identify.= $this->addEncode($this->__orderByRaw);
- $identify.= $this->addEncode($this->__inRandomOrder);
- $identify.= $this->addEncode($attach);
- //
- return 'DBCache:' . $this->table . ':' . $type . '::' . md5($identify);
- }
- private function addEncode($value)
- {
- $encodeName = '';
- if (is_array($value)) {
- sort($value);
- foreach ($value AS $key => $val) {
- $encodeName .= ':' . $key . '=' . $this->addEncode($val) . ';';
- }
- } elseif ($value instanceof Closure) {
- $join = new DBClause();
- call_user_func($value, $join);
- $encodeName .= ':' . $join->toString() . ';';
- } elseif (is_string($value) || is_numeric($value)) {
- $encodeName .= ':' . $value . ';';
- } else {
- $encodeName .= ':' . var_export($value, true) . ';';
- }
- return $encodeName;
- }
- /**
- * 创建对象
- * @return $this|DOModel|null
- */
- private function builder() {
- if ($this->builder === null) {
- $this->builder = new DOModel($this->table, $this->attributes);
- }
- $builder = $this->builder;
- //
- foreach ($this->__join AS $item) {
- $builder = $builder->join($item[0], $item[1], $item[2], $item[3], $item[4], $item[5]);
- }
- foreach ($this->__take AS $item) {
- $builder = $builder->take($item);
- }
- foreach ($this->__where AS $item) {
- $builder = $builder->where($item[0], $item[1], $item[2], $item[3]);
- }
- foreach ($this->__whereRaw AS $item) {
- $builder = $builder->whereRaw($item[0], $item[1], $item[2]);
- }
- foreach ($this->__whereIn AS $item) {
- $builder = $builder->whereIn($item[0], $item[1], $item[2], $item[3]);
- }
- foreach ($this->__select AS $item) {
- $builder = $builder->select($item);
- }
- foreach ($this->__selectRaw AS $item) {
- $builder = $builder->selectRaw($item[0], $item[1]);
- }
- foreach ($this->__orderBy AS $item) {
- $builder = $builder->orderBy($item[0], $item[1]);
- }
- foreach ($this->__orderByRaw AS $item) {
- $builder = $builder->orderByRaw($item[0], $item[1]);
- }
- if ($this->__inRandomOrder) {
- $builder = $builder->inRandomOrder();
- }
- //
- $this->initParameter();
- return $builder;
- }
- /* ******************************************************************************** */
- /* ******************************************************************************** */
- /* ******************************************************************************** */
- /**
- * 静态方法
- *
- * @param string $tabel
- * @return DBCache
- */
- public static function table($tabel = '')
- {
- return new DBCache($tabel);
- }
- /**
- * 获取一条数据
- *
- * @param array $columns
- * @return mixed
- */
- public function first($columns = ['*'])
- {
- if ($this->__cache) {
- $cacheKey = $this->identify('first', $columns);
- if ($this->__removeCache) {
- return $this->forgetCache($cacheKey);
- }
- $result = Cache::remember($cacheKey, $this->__cacheMinutes, function() use ($columns) {
- return $this->builder()->first($columns);
- });
- }else{
- $result = $this->builder()->first($columns);
- }
- return isset($result->original)?$result->original:null;
- }
- /**
- * 获取所有数据
- *
- * @param array $columns
- * @return array|bool
- */
- public function get($columns = ['*'])
- {
- if ($this->__cache) {
- $cacheKey = $this->identify('get', $columns);
- if ($this->__removeCache) {
- return $this->forgetCache($cacheKey);
- }
- $result = Cache::remember($cacheKey, $this->__cacheMinutes, function() use ($columns) {
- return $this->builder()->get($columns);
- });
- }else{
- $result = $this->builder()->get($columns);
- }
- //
- $array = [];
- foreach ($result AS $key=>$item) {
- $array[$key] = isset($item->original)?$item->original:null;
- }
- return $array;
- }
- /**
- * 查询结果的计数
- *
- * @param string $columns
- * @return int|bool
- */
- public function count($columns = '*')
- {
- if ($this->__cache) {
- $cacheKey = $this->identify('count', $columns);
- if ($this->__removeCache) {
- return $this->forgetCache($cacheKey);
- }
- $result = Cache::remember($cacheKey, $this->__cacheMinutes, function() use ($columns) {
- return $this->builder()->count($columns);
- });
- }else{
- $result = $this->builder()->count($columns);
- }
- return intval($result);
- }
- /**
- * 从查询的第一个结果中获得一个列的值
- *
- * @param $column
- * @return mixed
- */
- public function value($column)
- {
- if ($this->__cache) {
- $cacheKey = $this->identify('value', $column);
- if ($this->__removeCache) {
- return $this->forgetCache($cacheKey);
- }
- $result = Cache::remember($cacheKey, $this->__cacheMinutes, function() use ($column) {
- $value = $this->builder()->value($column);
- return $value ? $value : '';
- });
- }else{
- $result = $this->builder()->value($column);
- }
- return $result;
- }
- /**
- * 统计输出
- *
- * @param $column
- * @return bool|mixed
- */
- public function sum($column)
- {
- if ($this->__cache) {
- $cacheKey = $this->identify('sum', $column);
- if ($this->__removeCache) {
- return $this->forgetCache($cacheKey);
- }
- $result = Cache::remember($cacheKey, $this->__cacheMinutes, function() use ($column) {
- $sum = $this->builder()->sum($column);
- return $sum ? $sum : 0;
- });
- }else{
- $result = $this->builder()->sum($column);
- }
- return $result;
- }
- /**
- * 获取分页数据
- * @param null $perPage
- * @param array $columns
- * @param string $pageName
- * @param null $page
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|mixed
- */
- public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
- {
- if ($this->__cache) {
- $attach = $this->addEncode($perPage);
- $attach.= $this->addEncode($columns);
- $attach.= $this->addEncode($pageName);
- $attach.= $this->addEncode(Paginator::resolveCurrentPage($pageName));
- $cacheKey = $this->identify('paginate', $attach);
- if ($this->__removeCache) {
- return $this->forgetCache($cacheKey);
- }
- $result = Cache::remember($cacheKey, $this->__cacheMinutes, function() use ($perPage, $columns, $pageName, $page) {
- return $this->builder()->paginate($perPage, $columns, $pageName, $page);
- });
- }else{
- $result = $this->builder()->paginate($perPage, $columns, $pageName, $page);
- }
- $array = [];
- foreach ($result AS $key=>$item) {
- $array[$key] = isset($item->original)?$item->original:null;
- }
- return [
- "currentPage" => $result->currentPage(),
- "firstItem" => $result->firstItem(),
- "hasMorePages" => $result->hasMorePages(),
- "lastItem" => $result->lastItem(),
- "lastPage" => $result->lastPage(),
- "nextPageUrl" => $result->nextPageUrl(),
- "previousPageUrl" => $result->previousPageUrl(),
- "perPage" => $result->perPage(),
- "total" => $result->total(),
- "lists" => $array,
- ];
- }
- }
|