-
Notifications
You must be signed in to change notification settings - Fork 431
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
Preferences caching #2792
base: msieben/read-after-write-caching
Are you sure you want to change the base?
Preferences caching #2792
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,13 +18,19 @@ export default function (server: Server, ctx: AppContext) { | |
throw new InvalidRequestError('Preference is missing a $type') | ||
} | ||
} | ||
|
||
await ctx.actorStore.transact(requester, async (actorTxn) => { | ||
await actorTxn.pref.putPreferences( | ||
checkedPreferences, | ||
'app.bsky', | ||
auth.credentials.scope, | ||
) | ||
}) | ||
|
||
await ctx.preferencesCache?.set( | ||
requester, | ||
JSON.stringify(checkedPreferences), | ||
) | ||
Comment on lines
+30
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this could cache different contents depending on the user's scope. Maybe it would be more straightforward to control the cache contents entirely on the read path as a read-through cache, and turn this into an invalidation. |
||
}, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { SimpleStore } from '@atproto-labs/simple-store' | ||
import Redis from 'ioredis' | ||
|
||
import { redisLogger } from '../logger' | ||
|
||
export class RedisSimpleStore implements SimpleStore<string, string> { | ||
/** | ||
* @param redis - Redis client | ||
* @param maxTTL - Maximum age of a cached revision in milliseconds | ||
*/ | ||
constructor( | ||
protected readonly redis: Redis, | ||
protected readonly maxTTL: number, | ||
protected readonly storeName: string, | ||
) { | ||
// Redis expects the expiration time in milliseconds | ||
if (!Number.isFinite(this.maxTTL) || this.maxTTL <= 0) { | ||
throw new TypeError('maxAge must be a positive number') | ||
} | ||
} | ||
|
||
protected key(did: string): string { | ||
return `${this.storeName}:${did}` | ||
} | ||
|
||
async get(did: string): Promise<string | undefined> { | ||
try { | ||
const value = await this.redis.get(this.key(did)) | ||
return value || undefined | ||
} catch (err) { | ||
redisLogger.error({ err, did }, `error getting ${this.storeName}`) | ||
return undefined | ||
} | ||
} | ||
|
||
async set(did: string, value: string): Promise<void> { | ||
try { | ||
await this.redis.set(this.key(did), value, 'PX', this.maxTTL) | ||
} catch (err) { | ||
redisLogger.error({ err, did, value }, `error setting ${this.storeName}`) | ||
} | ||
} | ||
|
||
async del(did: string): Promise<void> { | ||
try { | ||
await this.redis.del(this.key(did)) | ||
} catch (err) { | ||
redisLogger.error({ err, did }, `error deleting ${this.storeName}`) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -120,8 +120,9 @@ export const readEnv = (): ServerEnvironment => { | |||||
// fetch | ||||||
fetchDisableSsrfProtection: envBool('PDS_DISABLE_SSRF_PROTECTION'), | ||||||
|
||||||
// Repo revision cache | ||||||
// caching | ||||||
repoRevCacheMaxTTL: envInt('PDS_REPO_REV_CACHE_MAX_TTL'), // milliseconds (use 0 to disable) | ||||||
preferencesCacheMaxTTL: envInt('PDS_REPO_REV_CACHE_MAX_TTL'), // milliseconds (use 0 to disable) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -239,6 +240,7 @@ export type ServerEnvironment = { | |||||
// fetch | ||||||
fetchDisableSsrfProtection?: boolean | ||||||
|
||||||
// Repo revision cache | ||||||
// Caching | ||||||
repoRevCacheMaxTTL?: number | ||||||
preferencesCacheMaxTTL?: number | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still want to apply the scope here.