1+ import { dev } from "$app/environment" ;
12import { GITHUB_TOKEN , KV_REST_API_TOKEN , KV_REST_API_URL } from "$env/static/private" ;
23import type {
34 CommentAuthorAssociation ,
@@ -8,6 +9,7 @@ import { Octokit } from "octokit";
89import parseChangelog from "$lib/changelog-parser" ;
910import type { Repository } from "$lib/repositories" ;
1011import type { Issues , Pulls } from "$lib/types" ;
12+ import { CacheHandler , type RedisJson } from "./cache-handler" ;
1113
1214/**
1315 * A strict version of Extract.
@@ -159,7 +161,7 @@ const DEPRECATIONS_TTL = 60 * 60 * 24 * 2; // 2 days
159161 * with an additional caching mechanism.
160162 */
161163export class GitHubCache {
162- readonly #redis: Redis ;
164+ readonly #cache: CacheHandler ;
163165 readonly #octokit: Octokit ;
164166
165167 /**
@@ -171,10 +173,13 @@ export class GitHubCache {
171173 * @constructor
172174 */
173175 constructor ( redisUrl : string , redisToken : string , githubToken : string ) {
174- this . #redis = new Redis ( {
175- url : redisUrl ,
176- token : redisToken
177- } ) ;
176+ this . #cache = new CacheHandler (
177+ new Redis ( {
178+ url : redisUrl ,
179+ token : redisToken
180+ } ) ,
181+ dev
182+ ) ;
178183
179184 this . #octokit = new Octokit ( {
180185 auth : githubToken
@@ -238,7 +243,7 @@ export class GitHubCache {
238243 * @returns a currying promise than handles everything needed for requests
239244 * @private
240245 */
241- #processCached< RType extends Parameters < InstanceType < typeof Redis > [ "json" ] [ "set" ] > [ 2 ] > ( ) {
246+ #processCached< RType extends RedisJson > ( ) {
242247 /**
243248 * Inner currying function to circumvent unsupported partial inference
244249 *
@@ -255,7 +260,7 @@ export class GitHubCache {
255260 transformer : ( from : Awaited < PromiseType > ) => RType | Promise < RType > ,
256261 ttl : number | ( ( value : RType ) => number | undefined ) | undefined = undefined
257262 ) : Promise < RType > => {
258- const cachedValue = await this . #redis . json . get < RType > ( cacheKey ) ;
263+ const cachedValue = await this . #cache . get < RType > ( cacheKey ) ;
259264 if ( cachedValue ) {
260265 console . log ( `Cache hit for ${ cacheKey } ` ) ;
261266 return cachedValue ;
@@ -265,17 +270,15 @@ export class GitHubCache {
265270
266271 const newValue = await transformer ( await promise ( ) ) ;
267272
268- await this . #redis . json . set ( cacheKey , "$" , newValue ) ;
273+ let ttlResult : number | undefined = undefined ;
269274 if ( ttl !== undefined ) {
270275 if ( typeof ttl === "function" ) {
271- const ttlResult = ttl ( newValue ) ;
272- if ( ttlResult !== undefined ) {
273- await this . #redis. expire ( cacheKey , ttlResult ) ;
274- }
276+ ttlResult = ttl ( newValue ) ;
275277 } else {
276- await this . #redis . expire ( cacheKey , ttl ) ;
278+ ttlResult = ttl ;
277279 }
278280 }
281+ await this . #cache. set ( cacheKey , newValue , ttlResult ) ;
279282
280283 return newValue ;
281284 } ;
0 commit comments