0
使用Laravel雄辩的型号,我怎么能在访问它更新一个对象的数据,如果它一直> 30日以来,这是最后一次updateDate?Laravel雄辩更新每隔30天
我有,我想,以确保数据是半上最新与另一个API,但想缓存中的数据在我的数据库,而不是调用他们的API每个页面负载。
有每个模型加载时间Laravel调用,在那里我可以检查是否已满30天,打电话来加载新数据的API,然后将其保存的任何功能?
使用Laravel雄辩的型号,我怎么能在访问它更新一个对象的数据,如果它一直> 30日以来,这是最后一次updateDate?Laravel雄辩更新每隔30天
我有,我想,以确保数据是半上最新与另一个API,但想缓存中的数据在我的数据库,而不是调用他们的API每个页面负载。
有每个模型加载时间Laravel调用,在那里我可以检查是否已满30天,打电话来加载新数据的API,然后将其保存的任何功能?
目前尚不清楚如何通过API使用的数据,但你会发现Laravel的缓存功能非常方便。具体为remember()
方法。
use Illuminate\Support\Facades\Cache;
// Put the result returned from the closure in the cache for 30 days
// under the key 'apidata' and serve this data off of cache if it already there
$apidata = Cache::remember('apidata', 60*24*30, function() {
// talk to your API
// and return the data from this closure
return $result;
});
可以再让一步,敷在服务类,使您的生活更轻松。沿
namespace App;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Cache;
class ApiData
{
protected $client;
public function __construct(Client $client)
{
$this->client = $client;
}
public function all()
{
return Cache::remember('apidata', 60*24*30, function() {
$response = $this->client->get('http://httpbin.org/get');
return $response->getBody()->getContents();
});
}
}
东西线然后用它
// Get an instance off of IoC container
$api = app('App\ApiData');
$apidata = $api->all();
没有以任何方式帮助吗? – peterm
cron作业每个月30天? –