| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- namespace UCore\Helper;
- use Illuminate\Support\Facades\Log;
- /**
- * 缓存助手类
- */
- class Cache
- {
- /**
- * 回调函数的缓存
- *
- * @param $key
- * @param callable $callable
- * @param $param_arr
- * @param int $exp 过期秒数
- * @return mixed
- *
- */
- static public function cacheCall($key2, callable $callable, $param_arr, $exp = 60, $tags = [])
- {
- $key = self::getKey($key2);
- if($exp){
- $old = \Illuminate\Support\Facades\Cache::get($key);
- if (!is_null($old)) {
- return $old;
- }
- }
- $new = call_user_func_array($callable, $param_arr);
- if($exp){
- \Illuminate\Support\Facades\Cache::put($key, $new, $exp);
- }
- if ($tags) {
- CacheTag::key_tags($key, $tags, null);
- }
- return $new;
- }
- /**
- * 设置数据
- *
- * @param $key
- * @param $data
- * @param $exp
- * @param $tags
- * @return void
- */
- static public function put($key, $data, $exp = 60, $tags = [])
- {
- $key = self::getKey($key);
- \Illuminate\Support\Facades\Cache::put($key, $data, $exp);
- if ($tags) {
- CacheTag::key_tags($key, $tags, null);
- }
- }
- /**
- * 获取数据
- *
- * @param $key
- * @param $default
- * @return mixed
- */
- static public function get($key, $default = null)
- {
- $key = self::getKey($key);
- Logger::debug("Cache-get".$key);
- return \Illuminate\Support\Facades\Cache::get($key,$default);
- }
- /**
- * has数据
- * @param $key
- * @return bool
- */
- static public function has($key)
- {
- $key = self::getKey($key);
- return \Illuminate\Support\Facades\Cache::has($key);
- }
- static public function getTags($key)
- {
- $key = self::getKey($key);
- }
- /**
- * 获取键名
- * @param $data
- * @return string
- */
- static public function getKey($data)
- {
- return md5(serialize($data));
- }
- }
|