1111use Kirby \Toolkit \Collection ;
1212use Kirby \Toolkit \Obj ;
1313use Kirby \Toolkit \Str ;
14+ use Scottboms \Mastodon \Helpers \CacheKey ;
1415
1516use 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
0 commit comments