5

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:*`]);
Kent Wood
  • 1,392
  • 2
  • 14
  • 30

1 Answers1

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.

hardyRocks
  • 307
  • 3
  • 10