@credo-ts/core
Version:
<p align="center"> <br /> <img alt="Credo Logo" src="https://github.com/openwallet-foundation/credo-ts/blob/c7886cb8377ceb8ee4efe8d264211e561a75072d/images/credo-logo.png" height="250px" /> </p> <h1 align="center"><b>Credo - Core</b></h1
72 lines (71 loc) • 3.08 kB
JavaScript
import { InjectionSymbols } from "../../constants.mjs";
import { JsonTransformer } from "../../utils/JsonTransformer.mjs";
import "../../utils/index.mjs";
import { inject, injectable } from "../../plugins/index.mjs";
import __decorateMetadata from "../../_virtual/_@oxc-project_runtime@0.151.0/helpers/esm/decorateMetadata.mjs";
import __decorate from "../../_virtual/_@oxc-project_runtime@0.151.0/helpers/esm/decorate.mjs";
import "../../agent/index.mjs";
import "../../storage/BaseRecord.mjs";
import { CacheModuleConfig } from "./CacheModuleConfig.mjs";
import __decorateParam from "../../_virtual/_@oxc-project_runtime@0.151.0/helpers/esm/decorateParam.mjs";
//#region src/modules/cache/CachedStorageService.ts
let CachedStorageService = class CachedStorageService {
constructor(storageService) {
this.storageService = storageService;
this.supportsCursorPagination = false;
}
cache(agentContext) {
return agentContext.resolve(CacheModuleConfig).cache;
}
getCacheKey(options) {
return `${options.type}:${options.id}`;
}
async save(agentContext, record) {
if (record.allowCache) await this.cache(agentContext).set(agentContext, this.getCacheKey(record), record.toJSON());
return await this.storageService.save(agentContext, record);
}
async update(agentContext, record) {
if (record.allowCache) await this.cache(agentContext).set(agentContext, this.getCacheKey(record), record.toJSON());
return await this.storageService.update(agentContext, record);
}
async delete(agentContext, record) {
if (record.allowCache) await this.cache(agentContext).remove(agentContext, this.getCacheKey(record));
return await this.storageService.delete(agentContext, record);
}
async deleteById(agentContext, recordClass, id) {
if (recordClass.allowCache) await this.cache(agentContext).remove(agentContext, this.getCacheKey({
...recordClass,
id
}));
return await this.storageService.deleteById(agentContext, recordClass, id);
}
async getById(agentContext, recordClass, id) {
if (recordClass.allowCache) {
const cachedValue = await this.cache(agentContext).get(agentContext, this.getCacheKey({
type: recordClass.type,
id
}));
if (cachedValue) return JsonTransformer.fromJSON(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;
}
async getAll(agentContext, recordClass) {
return await this.storageService.getAll(agentContext, recordClass);
}
async findByQuery(agentContext, recordClass, query, queryOptions) {
return await this.storageService.findByQuery(agentContext, recordClass, query, queryOptions);
}
};
CachedStorageService = __decorate([
injectable(),
__decorateParam(0, inject(InjectionSymbols.StorageService)),
__decorateMetadata("design:paramtypes", [Object])
], CachedStorageService);
//#endregion
export { CachedStorageService };
//# sourceMappingURL=CachedStorageService.mjs.map