-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from credebl/fix/add-cache-tenant-record-patch
fix:added cache tenant record patch
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
patches/@credo-ts+tenants+0.5.3+001+cache-tenant-record-patch.patch
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,56 @@ | ||
diff --git a/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.d.ts b/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.d.ts | ||
index 91bb8f4..b4dae61 100644 | ||
--- a/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.d.ts | ||
+++ b/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.d.ts | ||
@@ -1,5 +1,5 @@ | ||
import type { TenantRecord } from '../repository'; | ||
-import type { AgentContextProvider, UpdateAssistantUpdateOptions } from '@credo-ts/core'; | ||
+import type { AgentContextProvider, UpdateAssistantUpdateOptions , CacheModule, InMemoryLruCache } from '@credo-ts/core'; | ||
import { AgentContext, EventEmitter, Logger } from '@credo-ts/core'; | ||
import { TenantRecordService } from '../services'; | ||
import { TenantSessionCoordinator } from './TenantSessionCoordinator'; | ||
@@ -9,7 +9,9 @@ export declare class TenantAgentContextProvider implements AgentContextProvider | ||
private eventEmitter; | ||
private logger; | ||
private tenantSessionCoordinator; | ||
- constructor(tenantRecordService: TenantRecordService, rootAgentContext: AgentContext, eventEmitter: EventEmitter, tenantSessionCoordinator: TenantSessionCoordinator, logger: Logger); | ||
+ private cacheModule; | ||
+ private inMemoryLruCache; | ||
+ constructor(tenantRecordService: TenantRecordService, rootAgentContext: AgentContext, eventEmitter: EventEmitter, tenantSessionCoordinator: TenantSessionCoordinator, logger: Logger, cache: InMemoryLruCache); | ||
getAgentContextForContextCorrelationId(contextCorrelationId: string): Promise<AgentContext>; | ||
getContextForInboundMessage(inboundMessage: unknown, options?: { | ||
contextCorrelationId?: string; | ||
diff --git a/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.js b/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.js | ||
index d491d4e..d60ec79 100644 | ||
--- a/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.js | ||
+++ b/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.js | ||
@@ -24,16 +24,28 @@ let TenantAgentContextProvider = class TenantAgentContextProvider { | ||
this.eventEmitter = eventEmitter; | ||
this.tenantSessionCoordinator = tenantSessionCoordinator; | ||
this.logger = logger; | ||
+ this.cache = new core_1.CacheModule({ | ||
+ cache: new core_1.InMemoryLruCache({ limit: 100 }), | ||
+ }); | ||
// Start listener for newly created routing keys, so we can register a mapping for each new key for the tenant | ||
this.listenForRoutingKeyCreatedEvents(); | ||
} | ||
async getAgentContextForContextCorrelationId(contextCorrelationId) { | ||
+ this.logger.debug('debug ========= Inside getAgentContextForContextCorrelationId') | ||
// It could be that the root agent context is requested, in that case we return the root agent context | ||
if (contextCorrelationId === this.rootAgentContext.contextCorrelationId) { | ||
return this.rootAgentContext; | ||
} | ||
// TODO: maybe we can look at not having to retrieve the tenant record if there's already a context available. | ||
- const tenantRecord = await this.tenantRecordService.getTenantById(this.rootAgentContext, contextCorrelationId); | ||
+ this.logger.debug('debug ========= Get tenantRecord from cache') | ||
+ let tenantRecord = await this.cache.config.cache.get(this.rootAgentContext, `contextCorrelationId-${contextCorrelationId}`) | ||
+ if(!tenantRecord) { | ||
+ // TODO: maybe we can look at not having to retrieve the tenant record if there's already a context available. | ||
+ this.logger.debug('debug ========= TenantRecord not found in cache') | ||
+ tenantRecord = await this.tenantRecordService.getTenantById(this.rootAgentContext, contextCorrelationId) | ||
+ await this.cache.config.cache.set(this.rootAgentContext,`contextCorrelationId-${contextCorrelationId}`,tenantRecord) | ||
+ this.logger.debug(`debug ========= Cached tenant agent context for tenant '${contextCorrelationId}'`) | ||
+ } | ||
const shouldUpdate = !(0, core_1.isStorageUpToDate)(tenantRecord.storageVersion); | ||
// If the tenant storage is not up to date, and autoUpdate is disabled we throw an error | ||
if (shouldUpdate && !this.rootAgentContext.config.autoUpdateStorageOnStartup) { |