nestjs:how to delete redis cache with prefix in typeorm
Asked Answered
A

1

6

I have a service class with create,list,and update methods to modify entity

I set the redis cache in list methods,and the cache key is list_cache_1,list_cache_2,...

my question is, how to delete all releated caches in create or update methods, like

this.connection.queryResultCache.remove([`list_cache:*`]);
Andy answered 28/4, 2019 at 12:50 Comment(0)
E
1

when you set a "cache id" via QueryBuilder:

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

Or with Repository:

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

then, get the connection object and remove as below

import { getConnection } from "typeorm";

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

however, i'm not aware of a typeorm method to remove list_cache_* with wildcard.

Edlun answered 23/6, 2020 at 0:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.