Skip to content

Commit 104dfc0

Browse files
committed
Initial work on improving cache helpers.
1 parent ff26626 commit 104dfc0

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

classes/Feed.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Kirby\Toolkit\Collection;
1212
use Kirby\Toolkit\Obj;
1313
use Kirby\Toolkit\Str;
14+
use Scottboms\Mastodon\Helpers\CacheKey;
1415

1516
use function option;
1617

@@ -56,7 +57,7 @@ private function __construct(array $options = []) {
5657
protected function getUserId(): ?string
5758
{
5859
$cache = kirby()->cache('scottboms.mastodon');
59-
$cacheKey = 'userid-' . $this->options['username'] . '@' . $this->options['server'];
60+
$cacheKey = CacheKey::forUserId($this->options['username'], $this->options['server']);
6061

6162
// return cached id if available
6263
if ($userId = $cache->get($cacheKey)) {
@@ -100,7 +101,7 @@ public static function build(array $options = []): self
100101
public function getAccountInfo(): array
101102
{
102103
$cache = kirby()->cache('scottboms.mastodon');
103-
$cacheKey = 'account-info-' . $this->options['username'] . '@' . $this->options['server'];
104+
$cacheKey = CacheKey::forAccountInfo($this->options['username'], $this->options['server']);
104105

105106
// try to return cached account info
106107
if ($cached = $cache->get($cacheKey)) {
@@ -167,13 +168,19 @@ public function buildFeedUrl(): string
167168
return $feedUrl;
168169
}
169170

170-
public static function getFeed($feedUrl): array|string
171+
public static function getFeed($feedUrl, array $options = []): array|string
171172
{
172173
// fetch and decode the json data
173174
// see: https://getkirby.com/docs/reference/objects/http/remote/get
174175
// and https://getkirby.com/docs/reference/objects/http/remote/request
175-
$cacheKey = static::resolveCacheKey();
176-
$cache = kirby()->cache('scottboms.mastodon');
176+
177+
$instance = static::build($options);
178+
$cache = kirby()->cache('scottboms.mastodon');
179+
180+
$server = $instance->options['server'];
181+
$userId = $instance->options['userid'];
182+
183+
$cacheKey = CacheKey::forFeed($server, $userId);
177184

178185
// try to get cached feed
179186
if ($cached = $cache->get($cacheKey)) {
@@ -187,7 +194,7 @@ public static function getFeed($feedUrl): array|string
187194
$json_data = $request->json();
188195

189196
// store in cache, e.g., for 15 minutes
190-
$ttl = option('scottboms.mastodon.cachettl', 900); // 15 min default
197+
$ttl = $instance->options['cachettl'] ?? 900;
191198
$cache->set($cacheKey, $json_data, $ttl);
192199

193200
return $json_data;
@@ -201,11 +208,16 @@ public static function getFeed($feedUrl): array|string
201208
/*
202209
* @var Bool
203210
*/
204-
public static function clearFeedCache(): bool
211+
public static function clearFeedCache(array $options = []): bool
205212
{
206-
$cacheKey = static::resolveCacheKey();
213+
$instance = static::build($options);
207214
$cache = kirby()->cache('scottboms.mastodon');
208215

216+
$server = $instance->options['server'];
217+
$userId = $instance->options['userid'];
218+
219+
$cacheKey = CacheKey::forFeed($server, $userId);
220+
209221
return $cache->remove($cacheKey);
210222
}
211223

index.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
**/
1313

1414
load([
15-
'Scottboms\Mastodon\Feed' => __DIR__ . '/classes/Feed.php'
15+
'Scottboms\Mastodon\Feed' => __DIR__ . '/classes/Feed.php',
16+
'Scottboms\Mastodon\Helpers\CacheKey' => __DIR__ . '/src/Helpers/CacheKey.php',
1617
]);
1718

1819
use Scottboms\Mastodon\Feed;
@@ -47,6 +48,11 @@
4748
'api' => [
4849
'routes' => require __DIR__ . '/lib/routes.php'
4950
],
51+
'autoload' => [
52+
'psr-4' => [
53+
'Scottboms\\Mastodon\\' => __DIR__ . '/src',
54+
]
55+
],
5056
'areas' => [
5157
'mastodon-feed' => function ($kirby) {
5258
return [

src/Helpers/CacheKey.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Scottboms\Mastodon\Helpers;
4+
5+
use Exception;
6+
7+
class CacheKey
8+
{
9+
public static function forFeed(string $server, string $userId): string
10+
{
11+
if (empty($server) || empty($userId)) {
12+
throw new Exception('Cannot resolve feed cache key: missing server or userId.');
13+
}
14+
15+
return "mastodon-feed-{$server}-{$userId}";
16+
}
17+
18+
public static function forUserId(string $username, string $server): string
19+
{
20+
if (empty($username) || empty($server)) {
21+
throw new Exception('Cannot resolve userid cache key: missing username or server.');
22+
}
23+
24+
return "userid-{$username}@{$server}";
25+
}
26+
27+
public static function forAccountInfo(string $username, string $server): string
28+
{
29+
if (empty($username) || empty($server)) {
30+
throw new Exception('Cannot resolve account info cache key: missing username or server.');
31+
}
32+
33+
return "account-info-{$username}@{$server}";
34+
}
35+
}

0 commit comments

Comments
 (0)