我正在使用 google-api-php-client (API v3) 在页面上显示来自我的 Google 日历的事件列表。它正在工作,但是我想在磁盘上本地缓存结果,这样我就不必在每次加载页面时都进行 API 调用(日历变化不大)。是否可以缓存结果?
有一个名为 Google_FileCache.php 的文件,它有一个 Google_FileCache() 类,看起来它可以做我想要的,但我根本找不到任何文档,有人用过吗?
到目前为止,这是我的代码:
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
session_start();
$CLIENT_ID = '{my client ID}';
$SERVICE_ACCOUNT_NAME = '{my service account name}';
$KEY_FILE = '{my p12 key file}';
$client = new Google_Client();
$client->setApplicationName("Public Calendar");
$client->setUseObjects(true);
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
$key = file_get_contents($KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
$SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar', "https://www.googleapis.com/auth/calendar.readonly"),
$key)
);
$client->setClientId($CLIENT_ID);
$service = new Google_CalendarService($client);
if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
}
$rightNow = date('c');
$params = array('singleEvents' => 'true', 'orderBy' => 'startTime', 'timeMin' => $rightNow);
$events = $service->events->listEvents('primary', $params);
foreach ($events->getItems() as $event) {
echo '<p>'.$event->getSummary().'</p>';
}