-
Notifications
You must be signed in to change notification settings - Fork 10
Reapply "Add valkey cache handler to next app (#1210)" (#1215) #1220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f2986b4
Reapply "Add valkey cache handler to next app (#1210)" (#1215)
kalilsn e169a24
Disable cache when unable to connect to redis
kalilsn 5dc5946
Cleanup dockerfile
kalilsn 536080f
Capture sentry exception when redis errors
kalilsn cad3af5
Add redis query to healthcheck endpoint
kalilsn 8304abc
Inline forte-digital cache handler
kalilsn 4346195
Switch to ioredis
kalilsn d9aa8d5
Merge branch 'main' into kalilsn/caching-again
kalilsn 84543b3
Fix bugs caused by differences in hscan api between redis clients
kalilsn e234789
Fix retry strategy calculation
kalilsn d289b0a
Add error logging to cache functions
kalilsn d603548
Make sure fallback behavior is instant when client is reconnecting
kalilsn ec1fffc
Also fix retry logic for lib/redis client
kalilsn 921b6f7
Add timestamps to logs output in ci
kalilsn 8577e11
Use proper healthcheck and set restart policy
kalilsn ebb20d4
Lower health check timeout
kalilsn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,11 +1,96 @@ | ||
| /** | ||
| * Solution taken from here: https://github.com/vercel/next.js/discussions/48324#discussioncomment-10542097 | ||
| * | ||
| * We are reexporting `next.js`'s default cache handler to get around | ||
| * the fixed 2mb limit for cached fetches. | ||
| * We run into this limit somewhat quickly if we fetch more than 100 pubs. | ||
| */ | ||
| // Based on https://github.com/fortedigital/nextjs-cache-handler#full-example | ||
|
|
||
| import FileSystemCache from "next/dist/server/lib/incremental-cache/file-system-cache.js"; | ||
| import { PHASE_PRODUCTION_BUILD } from "next/constants.js"; | ||
| import createBufferStringHandler from "@fortedigital/nextjs-cache-handler/buffer-string-decorator"; | ||
| import { Next15CacheHandler } from "@fortedigital/nextjs-cache-handler/next-15-cache-handler"; | ||
| import createRedisHandler from "@fortedigital/nextjs-cache-handler/redis-strings"; | ||
| import { CacheHandler } from "@neshca/cache-handler"; | ||
| import { createClient } from "redis"; | ||
|
|
||
| export default FileSystemCache; | ||
| // A cache that always misses - intended to let us disable caching when redis is unavailable. Should | ||
| // be replaced if there's a better way to do that. | ||
| const dummyHandler = { | ||
| name: "no-cache", | ||
| get: () => undefined, | ||
| set: () => undefined, | ||
| revalidateTag: () => undefined, | ||
| }; | ||
|
|
||
| // Usual onCreation from @neshca/cache-handler | ||
| CacheHandler.onCreation(() => { | ||
| // Important - It's recommended to use global scope to ensure only one Redis connection is made | ||
| // This ensures only one instance get created | ||
| if (global.cacheHandlerConfig) { | ||
| return global.cacheHandlerConfig; | ||
| } | ||
|
|
||
| // Important - It's recommended to use global scope to ensure only one Redis connection is made | ||
| // This ensures new instances are not created in a race condition | ||
| if (global.cacheHandlerConfigPromise) { | ||
| return global.cacheHandlerConfigPromise; | ||
| } | ||
|
|
||
| // Main promise initializing the handler | ||
| global.cacheHandlerConfigPromise = (async () => { | ||
| console.info("Getting cache handler"); | ||
| /** @type {import("redis").RedisClientType | null} */ | ||
| let redisClient = null; | ||
| if (PHASE_PRODUCTION_BUILD !== process.env.NEXT_PHASE) { | ||
| try { | ||
| redisClient = createClient({ | ||
| url: process.env.VALKEY_URL, | ||
| pingInterval: 10000, | ||
| }); | ||
| redisClient.on("error", (e) => { | ||
| console.warn("Redis error", e); | ||
| console.warn("Disabling caching"); | ||
| global.cacheHandlerConfig = { handlers: [dummyHandler] }; | ||
| global.cacheHandlerConfigPromise = null; | ||
| throw e; | ||
| }); | ||
| } catch (error) { | ||
| console.error("Failed to create Redis client:", error); | ||
| } | ||
| } | ||
|
|
||
| if (redisClient) { | ||
| try { | ||
| console.info("Connecting Redis client..."); | ||
| await redisClient.connect(); | ||
| console.info("Redis client connected."); | ||
| } catch (error) { | ||
| console.warn("Failed to connect Redis client:", error); | ||
| await redisClient | ||
| .disconnect() | ||
| .catch(() => | ||
| console.warn("Failed to quit the Redis client after failing to connect.") | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (!redisClient?.isReady) { | ||
| console.error("Failed to initialize caching layer."); | ||
| global.cacheHandlerConfigPromise = null; | ||
| global.cacheHandlerConfig = { handlers: [dummyHandler] }; | ||
| return global.cacheHandlerConfig; | ||
| } | ||
|
|
||
| const redisCacheHandler = createRedisHandler({ | ||
| client: redisClient, | ||
| keyPrefix: "nextjs:", | ||
| keyExpirationStrategy: "EXAT", | ||
| }); | ||
|
|
||
| global.cacheHandlerConfigPromise = null; | ||
|
|
||
| global.cacheHandlerConfig = { | ||
| handlers: [createBufferStringHandler(redisCacheHandler)], | ||
| }; | ||
|
|
||
| return global.cacheHandlerConfig; | ||
| })(); | ||
|
|
||
| return global.cacheHandlerConfigPromise; | ||
| }); | ||
|
|
||
| export default new Next15CacheHandler(); | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -45,6 +45,8 @@ services: | |
| service: cache | ||
| networks: | ||
| - app-network | ||
| ports: | ||
| - 6379:6379 | ||
|
|
||
| minio-init: | ||
| env_file: | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.