支持lru缓存

This commit is contained in:
xiezhaodong 2025-04-14 20:27:45 +08:00
parent ecac72ad8b
commit bc26d3bda6

View File

@ -101,6 +101,7 @@ func (sdb *PreparedStmtDB) Reset() {
func (db *PreparedStmtDB) prepare(ctx context.Context, conn ConnPool, isTransaction bool, query string) (Stmt, error) {
db.Mux.RLock()
if db.Stmts != nil {
if stmt, ok := db.Stmts.Get(query); ok && (!stmt.Transaction || isTransaction) {
db.Mux.RUnlock()
// wait for other goroutines prepared
@ -111,9 +112,11 @@ func (db *PreparedStmtDB) prepare(ctx context.Context, conn ConnPool, isTransact
return *stmt, nil
}
}
db.Mux.RUnlock()
db.Mux.Lock()
if db.Stmts != nil {
// double check
if stmt, ok := db.Stmts.Get(query); ok && (!stmt.Transaction || isTransaction) {
db.Mux.Unlock()
@ -125,6 +128,7 @@ func (db *PreparedStmtDB) prepare(ctx context.Context, conn ConnPool, isTransact
return *stmt, nil
}
}
// check db.Stmts first to avoid Segmentation Fault(setting value to nil map)
// which cause by calling Close and executing SQL concurrently
if db.Stmts == nil {