Skip to content

Commit

Permalink
Merge pull request #178 from credebl/fix/add-cache-tenant-record-patch
Browse files Browse the repository at this point in the history
fix:added cache tenant record patch
  • Loading branch information
pallavighule authored Aug 26, 2024
2 parents 6ed8375 + a063528 commit 5720788
Showing 1 changed file with 56 additions and 0 deletions.
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) {

0 comments on commit 5720788

Please sign in to comment.