Skip to content

Commit a827495

Browse files
Yacccxiezhaodongjinzhu
authored
Preparestmt use LRU Map instead default map (#7435)
* 支持lru淘汰preparestmt cache * 支持lru淘汰preparestmt cache * 支持lru淘汰preparestmt cache * 只使用lru * 只使用lru * 只使用lru * 只使用lru * 只使用lru * 只使用lru * 只使用lru * 只使用lru * 只使用lru * change const export * Add stmt_store * refact prepare stmt store * Rename lru store * change const export * ADD UT * format code and add session level prepare stmt config * code format according to golinter ci * ADD UT --------- Co-authored-by: xiezhaodong <[email protected]> Co-authored-by: Jinzhu <[email protected]>
1 parent 489a563 commit a827495

File tree

7 files changed

+1365
-213
lines changed

7 files changed

+1365
-213
lines changed

gorm.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ type Config struct {
3434
DryRun bool
3535
// PrepareStmt executes the given query in cached statement
3636
PrepareStmt bool
37+
// PrepareStmt cache support LRU expired,
38+
// default maxsize=int64 Max value and ttl=1h
39+
PrepareStmtMaxSize int
40+
PrepareStmtTTL time.Duration
41+
3742
// DisableAutomaticPing
3843
DisableAutomaticPing bool
3944
// DisableForeignKeyConstraintWhenMigrating
@@ -105,6 +110,8 @@ type DB struct {
105110
type Session struct {
106111
DryRun bool
107112
PrepareStmt bool
113+
PrepareStmtMaxSize int
114+
PrepareStmtTTL time.Duration
108115
NewDB bool
109116
Initialized bool
110117
SkipHooks bool
@@ -197,7 +204,7 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) {
197204
}
198205

199206
if config.PrepareStmt {
200-
preparedStmt := NewPreparedStmtDB(db.ConnPool)
207+
preparedStmt := NewPreparedStmtDB(db.ConnPool, config.PrepareStmtMaxSize, config.PrepareStmtTTL)
201208
db.cacheStore.Store(preparedStmtDBKey, preparedStmt)
202209
db.ConnPool = preparedStmt
203210
}
@@ -268,7 +275,7 @@ func (db *DB) Session(config *Session) *DB {
268275
if v, ok := db.cacheStore.Load(preparedStmtDBKey); ok {
269276
preparedStmt = v.(*PreparedStmtDB)
270277
} else {
271-
preparedStmt = NewPreparedStmtDB(db.ConnPool)
278+
preparedStmt = NewPreparedStmtDB(db.ConnPool, config.PrepareStmtMaxSize, config.PrepareStmtTTL)
272279
db.cacheStore.Store(preparedStmtDBKey, preparedStmt)
273280
}
274281

0 commit comments

Comments
 (0)