mirror of
https://github.com/dwaxweiler/connector-mobilizon
synced 2025-06-05 21:59:25 +02:00
implement cache
This commit is contained in:
@ -13,6 +13,7 @@
|
|||||||
require_once __DIR__ . '/includes/exceptions/GeneralException.php';
|
require_once __DIR__ . '/includes/exceptions/GeneralException.php';
|
||||||
require_once __DIR__ . '/includes/exceptions/GroupNotFoundException.php';
|
require_once __DIR__ . '/includes/exceptions/GroupNotFoundException.php';
|
||||||
require_once __DIR__ . '/includes/Constants.php';
|
require_once __DIR__ . '/includes/Constants.php';
|
||||||
|
require_once __DIR__ . '/includes/EventsCache.php';
|
||||||
require_once __DIR__ . '/includes/Settings.php';
|
require_once __DIR__ . '/includes/Settings.php';
|
||||||
require_once __DIR__ . '/includes/DateTimeWrapper.php';
|
require_once __DIR__ . '/includes/DateTimeWrapper.php';
|
||||||
require_once __DIR__ . '/includes/Formatter.php';
|
require_once __DIR__ . '/includes/Formatter.php';
|
||||||
|
19
source/includes/EventsCache.php
Normal file
19
source/includes/EventsCache.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -47,20 +47,17 @@ final class GraphQlClient {
|
|||||||
}
|
}
|
||||||
GRAPHQL;
|
GRAPHQL;
|
||||||
|
|
||||||
|
$cachedEvents = EventsCache::get(['url' => $url, 'query' => $query, 'limit' => $limit]);
|
||||||
|
if ($cachedEvents !== false) {
|
||||||
|
return $cachedEvents;
|
||||||
|
}
|
||||||
|
|
||||||
$endpoint = $url . '/api';
|
$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]);
|
$data = self::query($endpoint, $query, ['limit' => $limit]);
|
||||||
self::checkData($data);
|
self::checkData($data);
|
||||||
|
|
||||||
// SessionCache.add(sessionStorage, { url, query, variables: { limit } }, data)
|
|
||||||
$events = $data['data']['events']['elements'];
|
$events = $data['data']['events']['elements'];
|
||||||
|
EventsCache::set(['url' => $url, 'query' => $query, 'limit' => $limit], $events);
|
||||||
return $events;
|
return $events;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,32 +83,19 @@ final class GraphQlClient {
|
|||||||
}
|
}
|
||||||
GRAPHQL;
|
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);
|
$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]);
|
$data = self::query($endpoint, $query, ['afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit]);
|
||||||
self::checkData($data);
|
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'];
|
$events = $data['data']['group']['organizedEvents']['elements'];
|
||||||
|
EventsCache::set(['url' => $url, 'query' => $query, 'afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit], $events);
|
||||||
return $events;
|
return $events;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ License: <wordpress-license>
|
|||||||
Features
|
Features
|
||||||
- Display events as Gutenberg block, as widget and as shortcut
|
- Display events as Gutenberg block, as widget and as shortcut
|
||||||
- Display events' title, date, and location, if available
|
- 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
|
- 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
|
- Optionally filter events by a specific group per block, per widget and per shortcut
|
||||||
- Set the URL of the Mobilizon instance in the settings
|
- Set the URL of the Mobilizon instance in the settings
|
||||||
|
Reference in New Issue
Block a user