-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http): Cleanup for HTTP cache (#28381)
Co-authored-by: Rhys Arkins <[email protected]>
- Loading branch information
Showing
7 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { DateTime, Settings } from 'luxon'; | ||
import { GlobalConfig } from '../../../config/global'; | ||
import { cleanupHttpCache } from './http-cache'; | ||
|
||
describe('util/cache/repository/http-cache', () => { | ||
beforeEach(() => { | ||
const now = DateTime.fromISO('2024-04-12T12:00:00.000Z').valueOf(); | ||
Settings.now = () => now; | ||
GlobalConfig.reset(); | ||
}); | ||
|
||
it('should not throw if cache is not a valid HttpCache', () => { | ||
expect(() => cleanupHttpCache('not a valid cache')).not.toThrow(); | ||
}); | ||
|
||
it('should remove expired items from the cache', () => { | ||
const now = DateTime.now(); | ||
const expiredItemTimestamp = now.minus({ days: 91 }).toISO(); | ||
const cache = { | ||
httpCache: { | ||
'http://example.com/foo': { | ||
timestamp: expiredItemTimestamp, | ||
etag: 'abc', | ||
lastModified: 'Mon, 01 Jan 2024 00:00:00 GMT', | ||
httpResponse: {}, | ||
}, | ||
'http://example.com/bar': { | ||
timestamp: now.toISO(), | ||
etag: 'abc', | ||
lastModified: 'Mon, 01 Jan 2024 00:00:00 GMT', | ||
httpResponse: {}, | ||
}, | ||
}, | ||
}; | ||
|
||
cleanupHttpCache(cache); | ||
|
||
expect(cache).toEqual({ | ||
httpCache: { | ||
'http://example.com/bar': { | ||
timestamp: now.toISO(), | ||
etag: 'abc', | ||
httpResponse: {}, | ||
lastModified: 'Mon, 01 Jan 2024 00:00:00 GMT', | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should remove all items if ttlDays is not configured', () => { | ||
GlobalConfig.set({ httpCacheTtlDays: 0 }); | ||
|
||
const now = DateTime.now(); | ||
const cache = { | ||
httpCache: { | ||
'http://example.com/foo': { | ||
timestamp: now.toISO(), | ||
etag: 'abc', | ||
lastModified: 'Mon, 01 Jan 2024 00:00:00 GMT', | ||
httpResponse: {}, | ||
}, | ||
'http://example.com/bar': { | ||
timestamp: now.toISO(), | ||
etag: 'abc', | ||
lastModified: 'Mon, 01 Jan 2024 00:00:00 GMT', | ||
httpResponse: {}, | ||
}, | ||
}, | ||
}; | ||
|
||
cleanupHttpCache(cache); | ||
|
||
expect(cache).toEqual({}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import is from '@sindresorhus/is'; | ||
import { DateTime } from 'luxon'; | ||
import { GlobalConfig } from '../../../config/global'; | ||
import { logger } from '../../../logger'; | ||
import { HttpCacheSchema } from '../../http/cache/schema'; | ||
|
||
export function cleanupHttpCache(cacheData: unknown): void { | ||
if (!is.plainObject(cacheData) || !is.plainObject(cacheData['httpCache'])) { | ||
logger.warn('cleanupHttpCache: invalid cache data'); | ||
return; | ||
} | ||
const httpCache = cacheData['httpCache']; | ||
|
||
const ttlDays = GlobalConfig.get('httpCacheTtlDays', 90); | ||
if (ttlDays === 0) { | ||
logger.trace('cleanupHttpCache: zero value received, removing the cache'); | ||
delete cacheData['httpCache']; | ||
return; | ||
} | ||
|
||
const now = DateTime.now(); | ||
for (const [url, item] of Object.entries(httpCache)) { | ||
const parsed = HttpCacheSchema.safeParse(item); | ||
if (parsed.success && parsed.data) { | ||
const item = parsed.data; | ||
const expiry = DateTime.fromISO(item.timestamp).plus({ days: ttlDays }); | ||
if (expiry < now) { | ||
logger.debug(`http cache: removing expired cache for ${url}`); | ||
delete httpCache[url]; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters