CacheService.php 562 B

12345678910111213141516171819202122
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Cache;
  4. class CacheService
  5. {
  6. function getOrExecute(String $key, $func, $expiration=null){
  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. if(!$expiration) $expiration=config('cache.expirations.default');
  13. Cache::put($key, $results, $expiration);
  14. }
  15. return $results;
  16. }
  17. }