Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 有没有办法取消mongoose find执行并从redis返回数据?_Typescript_Mongoose_Redis_Nestjs - Fatal编程技术网

Typescript 有没有办法取消mongoose find执行并从redis返回数据?

Typescript 有没有办法取消mongoose find执行并从redis返回数据?,typescript,mongoose,redis,nestjs,Typescript,Mongoose,Redis,Nestjs,我试图在nest.js中实现Redis缓存和mongoose,我正在寻找一种方法 在执行find或findOne并从redis返回数据之前,首先检查redis缓存 否则执行查询,将结果保存在redis中并返回结果。 我没有按照nest.js的建议实现的原因 我也在使用Apollo服务器进行GraphQL @Injectable() export class MyService { async getItem(where): Promise<ItemModel> {

我试图在nest.js中实现Redis缓存和mongoose,我正在寻找一种方法 在执行find或findOne并从redis返回数据之前,首先检查redis缓存 否则执行查询,将结果保存在redis中并返回结果。 我没有按照nest.js的建议实现的原因 我也在使用Apollo服务器进行GraphQL

@Injectable()
export class MyService {
    async getItem(where): Promise<ItemModel> {
        const fromCache = await this.cacheService.getValue('itemId');
        if(!!fromCache){
            return JSON.parse(fromCache);
        } else {
            const response = await this.ItemModel.find(where);
            this.cacheService.setValue('itemId', JSON.stringify(response));
            return response
        }
    }
}
@Injectable()
导出类MyService{
异步getItem(其中):承诺{
const fromCache=wait this.cacheService.getValue('itemId');
if(!!fromCache){
返回JSON.parse(fromCache);
}否则{
const response=wait this.ItemModel.find(where);
this.cacheService.setValue('itemId',JSON.stringify(response));
返回响应
}
}
}
我想把这段代码移到一个地方,这样我就不必这么做了 因为我有多个服务,所以对代码中的每个查询重复此代码。 我知道mongoose中间件可以对查询运行pre和post函数 但我只是不知道如何使用它来完成这个任务

以下是我正在使用的版本:

  • NestJSV7
  • mongoose v5.10.0
您可以创建一个将逻辑移动到以下位置:

export const UseCache = (cacheKey:string) => (_target: any, _field: string, descriptor: TypedPropertyDescriptor<any>) => {
    const originalMethod = descriptor.value;
    // note: important to use non-arrow function here to preserve this-context
    descriptor.value     = async function(...args: any[]) {
        const fromCache = await this.cacheService.getValue(cacheKey);
        if(!!fromCache){
            return JSON.parse(fromCache);
        }
        const result = await originalMethod.apply(this, args);
        await this.cacheService.setValue(cacheKey, JSON.stringify(result));
        return result;
    };
}
export-const-UseCache=(cacheKey:string)=>(\u-target:any,\u-field:string,descriptor:TypedPropertyDescriptor)=>{
const originalMethod=descriptor.value;
//注意:此处使用非箭头函数以保留此上下文非常重要
descriptor.value=异步函数(…args:any[]){
const fromCache=wait this.cacheService.getValue(cacheKey);
if(!!fromCache){
返回JSON.parse(fromCache);
}
const result=await originalMethod.apply(此参数为args);
等待this.cacheService.setValue(cacheKey,JSON.stringify(result));
返回结果;
};
}
然后将其用于:

@Injectable()
export class MyService {   

    constructor(private readonly cacheService:CacheService) { .. }

    @UseCache('itemId')
    async getItem(where): Promise<ItemModel> {        
        return this.ItemModel.find(where);
    }

    @UseCache('anotherCacheKey')
    async anotherMethodWithCache(): Promise<any> {        
         // ...            
    }
}
@Injectable()
导出类MyService{
构造函数(专用只读缓存服务:缓存服务){..}
@UseCache('itemId'))
异步getItem(其中):承诺{
返回此.ItemModel.find(其中);
}
@UseCache('anotherCacheKey')
异步anotherMethodWithCache():承诺{
// ...            
}
}

请详细说明由于Apollo而无法使用默认缓存设置的原因。我看不出你为什么不能一起使用它们。就本机NestJS功能而言,拦截器可能是以可重用方式实现此类模式的最佳选择。NestJS文档中解释的默认缓存在控制器级别工作,我想实现的是缓存和mongoose,这样每次在mongo数据库中插入/更新新记录时,我都可以清除缓存。在文档中,说明缓存不能与GraphQL一起正常工作[“在GraphQL应用程序中,拦截器分别为每个字段解析程序执行。因此,CacheModule(使用拦截器缓存响应)将不能正常工作。”]