Skip to content

Commit

Permalink
feat: add caching
Browse files Browse the repository at this point in the history
  • Loading branch information
genaris committed Feb 5, 2024
1 parent aa07578 commit 9bb27e7
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 3 deletions.
117 changes: 115 additions & 2 deletions src/anoncreds/DidWebAnonCredsRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,48 @@ import {
RegisterSchemaOptions,
RegisterSchemaReturn,
} from '@credo-ts/anoncreds'
import { AgentContext, DidsApi } from '@credo-ts/core'
import { AgentContext, CacheModuleConfig, DidsApi } from '@credo-ts/core'
import { parse } from 'did-resolver'
import { parseUrl } from 'query-string'
import { AnonCredsResourceResolutionResult } from './AnonCredsResourceResolutionResult'
import { calculateResourceId, verifyResourceId } from './utils'

export interface CacheSettings {
allowCaching: boolean
cacheDurationInSeconds: number
}

export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
public readonly methodName = 'web'

public readonly supportedIdentifier = /^did:web:[_a-z0-9.%A-]*/

private cacheSettings: CacheSettings

public constructor(options: { cacheOptions?: CacheSettings }) {
this.cacheSettings = options.cacheOptions ?? { allowCaching: true, cacheDurationInSeconds: 300 }
}

public async getSchema(agentContext: AgentContext, schemaId: string): Promise<GetSchemaReturn> {
const cacheKey = `anoncreds:schema:${schemaId}`

if (this.cacheSettings.allowCaching) {
const cache = agentContext.dependencyManager.resolve(CacheModuleConfig).cache

const cachedObject = await cache.get<GetSchemaReturn>(agentContext, cacheKey)

if (cachedObject) {
return {
schema: cachedObject.schema,
schemaId,
schemaMetadata: cachedObject.schemaMetadata,
resolutionMetadata: {
...cachedObject.resolutionMetadata,
servedFromCache: true,
},
}
}
}
try {
const { response, resourceId } = await this.parseIdAndFetchResource(agentContext, schemaId)

Expand All @@ -40,6 +70,20 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
throw new Error('Wrong resource Id')
}

if (this.cacheSettings.allowCaching) {
const cache = agentContext.dependencyManager.resolve(CacheModuleConfig).cache
await cache.set(
agentContext,
cacheKey,
{
resolutionMetadata: {},
schema,
schemaMetadata,
},
this.cacheSettings.cacheDurationInSeconds
)
}

return {
schemaId,
schema,
Expand All @@ -55,7 +99,7 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
}
}
} catch (error) {
console.log(`error: ${error}`)
agentContext.config.logger.debug(`error: ${error}`)
return {
resolutionMetadata: { error: 'invalid' },
schemaMetadata: {},
Expand Down Expand Up @@ -83,6 +127,27 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
agentContext: AgentContext,
credentialDefinitionId: string
): Promise<GetCredentialDefinitionReturn> {
const cacheKey = `anoncreds:credentialDefinition:${credentialDefinitionId}`

if (this.cacheSettings.allowCaching) {
const cache = agentContext.dependencyManager.resolve(CacheModuleConfig).cache

const cachedObject = await cache.get<GetCredentialDefinitionReturn>(agentContext, cacheKey)

if (cachedObject) {
return {
credentialDefinition: cachedObject.credentialDefinition,
credentialDefinitionId,
credentialDefinitionMetadata: cachedObject.credentialDefinitionMetadata,
resolutionMetadata: {
...cachedObject.resolutionMetadata,
servedFromCache: true,
},
}
}
}


try {
const { response, resourceId } = await this.parseIdAndFetchResource(agentContext, credentialDefinitionId)
if (response.status === 200) {
Expand All @@ -94,6 +159,20 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
throw new Error('Wrong resource Id')
}

if (this.cacheSettings.allowCaching) {
const cache = agentContext.dependencyManager.resolve(CacheModuleConfig).cache
await cache.set(
agentContext,
cacheKey,
{
resolutionMetadata: {},
credentialDefinition,
credentialDefinitionMetadata,
},
this.cacheSettings.cacheDurationInSeconds
)
}

return {
credentialDefinitionId,
credentialDefinition,
Expand Down Expand Up @@ -141,6 +220,26 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
agentContext: AgentContext,
revocationRegistryDefinitionId: string
): Promise<GetRevocationRegistryDefinitionReturn> {
const cacheKey = `anoncreds:revocationRegistryDefinition:${revocationRegistryDefinitionId}`

if (this.cacheSettings.allowCaching) {
const cache = agentContext.dependencyManager.resolve(CacheModuleConfig).cache

const cachedObject = await cache.get<GetRevocationRegistryDefinitionReturn>(agentContext, cacheKey)

if (cachedObject) {
return {
revocationRegistryDefinition: cachedObject.revocationRegistryDefinition,
revocationRegistryDefinitionId,
revocationRegistryDefinitionMetadata: cachedObject.revocationRegistryDefinitionMetadata,
resolutionMetadata: {
...cachedObject.resolutionMetadata,
servedFromCache: true,
},
}
}
}

try {
const { response, resourceId } = await this.parseIdAndFetchResource(agentContext, revocationRegistryDefinitionId)
if (response.status === 200) {
Expand All @@ -152,6 +251,20 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
throw new Error('Wrong resource Id')
}

if (this.cacheSettings.allowCaching) {
const cache = agentContext.dependencyManager.resolve(CacheModuleConfig).cache
await cache.set(
agentContext,
cacheKey,
{
resolutionMetadata: {},
revocationRegistryDefinition,
revocationRegistryDefinitionMetadata,
},
this.cacheSettings.cacheDurationInSeconds
)
}

return {
revocationRegistryDefinitionId,
revocationRegistryDefinition,
Expand Down
2 changes: 1 addition & 1 deletion test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe.skip('Schema', () => {

jest.spyOn(didsApiMock, 'resolveDidDocument').mockResolvedValue(JsonTransformer.fromJSON(didDocument1, DidDocument))

const registry = new DidWebAnonCredsRegistry()
const registry = new DidWebAnonCredsRegistry({})

const schemaResponse = await registry.getSchema(
getAgentContext(),
Expand Down

0 comments on commit 9bb27e7

Please sign in to comment.