-
Notifications
You must be signed in to change notification settings - Fork 239
feat: redis cache #2288
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
TimoGlastra
merged 10 commits into
openwallet-foundation:main
from
berendsliedrecht:cache-with-redis
May 17, 2025
Merged
feat: redis cache #2288
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0c3f382
feat: redis cache
berendsliedrecht c4168ed
docs(changeset): - Added a new package to use `redis` for caching in …
berendsliedrecht b46f195
chore: cache mediation and mediation routing records
berendsliedrecht c4bc2bb
Update packages/redis-cache-nodejs/package.json
TimoGlastra 1a01dac
address feedback
TimoGlastra c88c081
update folder
TimoGlastra 60c1d89
fix bug where cached storage service was always used
TimoGlastra 5c0ffee
fix name
TimoGlastra 9f1cd5c
fix tests
TimoGlastra f4d95a1
fix a lot of tests
TimoGlastra 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"@credo-ts/redis-cache-nodejs": patch | ||
"@credo-ts/didcomm": patch | ||
"@credo-ts/core": patch | ||
--- | ||
|
||
- Added a new package to use `redis` for caching in Node.js | ||
- Add a new open `useCache` to a record, which allows to CRUD the cache before calling the storage service | ||
- This is only set to `true` on the `connectionRecord` is it is used a couple times in a row a lot of the time | ||
TimoGlastra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
TimoGlastra marked this conversation as resolved.
Show resolved
Hide resolved
|
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
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { AgentContext } from '../../agent' | ||
import { InjectionSymbols } from '../../constants' | ||
import { inject, injectable } from '../../plugins' | ||
import { BaseRecord } from '../../storage/BaseRecord' | ||
import { BaseRecordConstructor, Query, QueryOptions, StorageService } from '../../storage/StorageService' | ||
import { JsonTransformer } from '../../utils' | ||
import { CacheModuleConfig } from './CacheModuleConfig' | ||
|
||
@injectable() | ||
// biome-ignore lint/suspicious/noExplicitAny: | ||
export class CachedStorageService<T extends BaseRecord<any, any, any>> implements StorageService<T> { | ||
public constructor(@inject(InjectionSymbols.StorageService) private storageService: StorageService<T>) {} | ||
|
||
private cache(agentContext: AgentContext) { | ||
return agentContext.resolve(CacheModuleConfig).cache | ||
} | ||
|
||
private getCacheKey(options: { type: string; id: string }) { | ||
return `${options.type}:${options.id}` | ||
} | ||
|
||
public async save(agentContext: AgentContext, record: T): Promise<void> { | ||
if (record.allowCache) { | ||
await this.cache(agentContext).set(agentContext, this.getCacheKey(record), record.toJSON()) | ||
} | ||
|
||
return await this.storageService.save(agentContext, record) | ||
} | ||
|
||
public async update(agentContext: AgentContext, record: T): Promise<void> { | ||
if (record.allowCache) { | ||
await this.cache(agentContext).set(agentContext, this.getCacheKey(record), record.toJSON()) | ||
} | ||
|
||
return await this.storageService.update(agentContext, record) | ||
} | ||
|
||
public async delete(agentContext: AgentContext, record: T): Promise<void> { | ||
if (record.allowCache) { | ||
await this.cache(agentContext).remove(agentContext, this.getCacheKey(record)) | ||
} | ||
return await this.storageService.delete(agentContext, record) | ||
} | ||
|
||
public async deleteById( | ||
agentContext: AgentContext, | ||
recordClass: BaseRecordConstructor<T>, | ||
id: string | ||
): Promise<void> { | ||
if (recordClass.allowCache) { | ||
await this.cache(agentContext).remove(agentContext, this.getCacheKey({ ...recordClass, id })) | ||
} | ||
return await this.storageService.deleteById(agentContext, recordClass, id) | ||
} | ||
|
||
public async getById(agentContext: AgentContext, recordClass: BaseRecordConstructor<T>, id: string): Promise<T> { | ||
if (recordClass.allowCache) { | ||
const cachedValue = await this.cache(agentContext).get<T>( | ||
agentContext, | ||
this.getCacheKey({ type: recordClass.type, id }) | ||
) | ||
|
||
if (cachedValue) return JsonTransformer.fromJSON<T>(cachedValue, recordClass) | ||
} | ||
|
||
const record = await this.storageService.getById(agentContext, recordClass, id) | ||
|
||
if (recordClass.allowCache) { | ||
await this.cache(agentContext).set( | ||
agentContext, | ||
this.getCacheKey({ type: recordClass.type, id }), | ||
record.toJSON() | ||
) | ||
} | ||
|
||
return record | ||
} | ||
|
||
// TODO: not in caching interface, yet | ||
public async getAll(agentContext: AgentContext, recordClass: BaseRecordConstructor<T>): Promise<T[]> { | ||
return await this.storageService.getAll(agentContext, recordClass) | ||
} | ||
|
||
// TODO: not in caching interface, yet | ||
public async findByQuery( | ||
agentContext: AgentContext, | ||
recordClass: BaseRecordConstructor<T>, | ||
query: Query<T>, | ||
queryOptions?: QueryOptions | ||
): Promise<T[]> { | ||
return await this.storageService.findByQuery(agentContext, recordClass, query, queryOptions) | ||
} | ||
} |
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
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
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.