【问题标题】:nestjs:how to delete redis cache with prefix in typeormnestjs:如何在 typeorm 中删除带前缀的 redis 缓存
【发布时间】:2019-09-17 07:33:46
【问题描述】:

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

我在list方法中设置了redis缓存,缓存key是list_cache_1,list_cache_2,...

我的问题是,如何删除createupdate 方法中的所有相关缓存,例如

this.connection.queryResultCache.remove([`list_cache:*`]);

【问题讨论】:

    标签: nestjs typeorm redis-cache


    【解决方案1】:

    当您通过 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 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 2011-06-14
      • 2018-07-21
      相关资源
      最近更新 更多