<?php


namespace App\Services;


use Illuminate\Support\Facades\Cache;

class CacheService
{
    function getOrExecute(String $key, $func, $expiration=null){
        $results = Cache::get($key);
        if(!$results){
            if(!$func||gettype($func)!='object') throw new \Exception('执行函数类型错误');
            $results = $func();
            if(!$results)return null;
            if(!$expiration) $expiration=config('cache.expirations.default');
            Cache::put($key, $results, $expiration);
        }
        return $results;
    }
}
