CacheService.php 490 B

123456789101112131415161718192021
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Cache;
  4. class CacheService
  5. {
  6. function getOrExecute(String $key, $func){
  7. $results = Cache::get($key);
  8. if(!$results){
  9. if(!$func||gettype($func)!='object') throw new \Exception('执行函数类型错误');
  10. $results = $func();
  11. if(!$results)return null;
  12. Cache::put($key, $results, config('cache.expirations.default'));
  13. }
  14. return $results;
  15. }
  16. }