fix: should automatically cache if alwaysEnable (#10137)

Closes: #9910
This commit is contained in:
Andrea Mouraud 2024-01-02 09:03:22 +01:00 committed by GitHub
parent 73ee70b331
commit 173910ed79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 2 deletions

View File

@ -158,6 +158,7 @@ export class DbQueryResultCache implements QueryResultCache {
? new MssqlParameter(options.identifier, "nvarchar")
: options.identifier,
})
.cache(false) // disable cache to avoid infinite loops when cache is alwaysEnable
.getRawOne()
} else if (options.query) {
if (this.connection.driver.options.type === "oracle") {
@ -168,6 +169,7 @@ export class DbQueryResultCache implements QueryResultCache {
)}, :query) = 0`,
{ query: options.query },
)
.cache(false) // disable cache to avoid infinite loops when cache is alwaysEnable
.getRawOne()
}
@ -179,6 +181,7 @@ export class DbQueryResultCache implements QueryResultCache {
? new MssqlParameter(options.query, "nvarchar")
: options.query,
})
.cache(false) // disable cache to avoid infinite loops when cache is alwaysEnable
.getRawOne()
}

View File

@ -261,8 +261,9 @@ export class QueryExpressionMap {
/**
* Indicates if query result cache is enabled or not.
* It is undefined by default to avoid overriding the `alwaysEnabled` config
*/
cache: boolean = false
cache?: boolean
/**
* Time in milliseconds in which cache will expire.

View File

@ -3770,7 +3770,7 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
(cacheOptions.alwaysEnabled &&
this.expressionMap.cache !== false) ||
// ...or it's enabled locally explicitly.
this.expressionMap.cache
this.expressionMap.cache === true
let cacheError = false
if (this.connection.queryResultCache && isCachingEnabled) {
try {

View File

@ -0,0 +1,10 @@
import { Entity, PrimaryGeneratedColumn, Column } from "../../../../src"
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number
@Column()
name: string
}

View File

@ -0,0 +1,58 @@
import "reflect-metadata"
import { expect } from "chai"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { DataSource } from "../../../src/data-source/DataSource"
import { User } from "./entity/User"
describe("github issues > #9910 Incorrect behaivor of 'alwaysEnabled: true' after change from issue #9023", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
cache: {
alwaysEnabled: true,
},
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))
it("should automatically cache if alwaysEnabled", () =>
Promise.all(
connections.map(async (connection) => {
if (connection.driver.options.type === "spanner") {
return
}
const user1 = new User()
user1.name = "Foo"
await connection.manager.save(user1)
const users1 = await connection
.createQueryBuilder(User, "user")
.getMany()
expect(users1.length).to.be.equal(1)
const user2 = new User()
user2.name = "Bar"
await connection.manager.save(user2)
// result should be cached and second user should not be retrieved
const users2 = await connection
.createQueryBuilder(User, "user")
.getMany()
expect(users2.length).to.be.equal(1)
// with cache explicitly disabled, the second user should be retrieved
const users3 = await connection
.createQueryBuilder(User, "user")
.cache(false)
.getMany()
expect(users3.length).to.be.equal(2)
}),
))
})