nestjs:如何在typeorm中删除带前缀的redis缓存

时间:2019-04-28 12:50:42

标签: nestjs typeorm redis-cache

我有一个带有create,list的服务类和update方法来修改实体

我在列表方法中设置了redis缓存,并且缓存键为list_cache_1,list_cache_2,...

我的问题是,如何在createupdate方法(如

)中删除所有相关的缓存
this.connection.queryResultCache.remove([`list_cache:*`]);

1 个答案:

答案 0 :(得分:0)

通过QueryBuilder设置“缓存ID”时:

const users = await connection
    .createQueryBuilder(User, "user")
    .where("user.isAdmin = :isAdmin", { isAdmin: true })
    .cache("list_cache_1", 25000)
    .getMany();

或使用存储库:

const users = await connection
    .getRepository(User)
    .find({
        where: { isAdmin: true },
        cache: {
            id: "list_cache_1",
            milliseconds: 25000
        }
    });

然后,获取连接对象并按如下所示删除

import { getConnection } from "typeorm";

const connection = getConnection();
await connection.queryResultCache.remove([`list_cache_1`]);

但是,我不知道要使用通配符删除list_cache_ *的typeorm方法。