1
0
mirror of https://github.com/dwaxweiler/connector-mobilizon synced 2025-06-05 21:59:25 +02:00

implement cache

This commit is contained in:
Daniel Waxweiler
2023-05-21 18:21:33 +02:00
parent 0a95cbdac3
commit 6a5f9981a0
4 changed files with 35 additions and 31 deletions

View File

@ -13,6 +13,7 @@
require_once __DIR__ . '/includes/exceptions/GeneralException.php';
require_once __DIR__ . '/includes/exceptions/GroupNotFoundException.php';
require_once __DIR__ . '/includes/Constants.php';
require_once __DIR__ . '/includes/EventsCache.php';
require_once __DIR__ . '/includes/Settings.php';
require_once __DIR__ . '/includes/DateTimeWrapper.php';
require_once __DIR__ . '/includes/Formatter.php';

View File

@ -0,0 +1,19 @@
<?php
namespace MobilizonConnector;
final class EventsCache {
private static $MAX_AGE_IN_S = 120;
public static function set(array $parameters, mixed $data): void {
// md5 is used as key must be 172 characters or fewer in length.
$key = md5(json_encode($parameters));
set_transient($key, $data, self::$MAX_AGE_IN_S);
}
public static function get(array $parameters): mixed {
$key = md5(json_encode($parameters));
$data = get_transient($key);
return $data;
}
}

View File

@ -47,20 +47,17 @@ final class GraphQlClient {
}
GRAPHQL;
$cachedEvents = EventsCache::get(['url' => $url, 'query' => $query, 'limit' => $limit]);
if ($cachedEvents !== false) {
return $cachedEvents;
}
$endpoint = $url . '/api';
// const dataInCache = SessionCache.get(sessionStorage, {
// url,
// query,
// variables: { limit },
// })
// if (dataInCache !== null) {
// return Promise.resolve(dataInCache)
// }
$data = self::query($endpoint, $query, ['limit' => $limit]);
self::checkData($data);
// SessionCache.add(sessionStorage, { url, query, variables: { limit } }, data)
$events = $data['data']['events']['elements'];
EventsCache::set(['url' => $url, 'query' => $query, 'limit' => $limit], $events);
return $events;
}
@ -86,32 +83,19 @@ final class GraphQlClient {
}
GRAPHQL;
$endpoint = $url . '/api';
// const afterDatetime = DateTimeWrapper.getCurrentDatetimeAsString()
// const dataInCache = SessionCache.get(sessionStorage, {
// url,
// query,
// variables: { afterDatetime, groupName, limit },
// })
// if (dataInCache !== null) {
// return Promise.resolve(dataInCache)
// }
$afterDatetime = date(\DateTime::ISO8601);
$cachedEvents = EventsCache::get(['url' => $url, 'query' => $query, 'afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit]);
if ($cachedEvents !== false) {
return $cachedEvents;
}
$endpoint = $url . '/api';
$data = self::query($endpoint, $query, ['afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit]);
self::checkData($data);
// return request(url, query, { afterDatetime, groupName, limit }).then(
// (data) => {
// SessionCache.add(
// sessionStorage,
// { url, query, variables: { afterDatetime, groupName, limit } },
// data
// )
// return Promise.resolve(data)
// }
// )
$events = $data['data']['group']['organizedEvents']['elements'];
EventsCache::set(['url' => $url, 'query' => $query, 'afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit], $events);
return $events;
}

View File

@ -17,7 +17,7 @@ License: <wordpress-license>
Features
- Display events as Gutenberg block, as widget and as shortcut
- Display events' title, date, and location, if available
- Cache requests' responses for 2 minutes in the browser's `sessionStorage`
- Cache requests' responses for 2 minutes in the database
- Configure number of events to show per block, per widget and per shortcut
- Optionally filter events by a specific group per block, per widget and per shortcut
- Set the URL of the Mobilizon instance in the settings