From d776afb2e4def12dcccb325d1e271c9aa347e13e Mon Sep 17 00:00:00 2001 From: sentriz Date: Tue, 27 Aug 2024 14:55:42 +0100 Subject: [PATCH] Memoise NewTable a e --- table.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/table.go b/table.go index 4cdff43..64a5769 100644 --- a/table.go +++ b/table.go @@ -1,13 +1,30 @@ package crud import ( + "reflect" + "sync" + "github.com/azer/crud/v2/meta" "github.com/azer/crud/v2/sql" ) +var ( + tableCache = map[reflect.Type]*Table{} + tableCacheMu sync.RWMutex +) + // Create an internal representation of a database table, including its fields from given // struct record func NewTable(any interface{}) (*Table, error) { + anyT := reflect.TypeOf(any) + + tableCacheMu.RLock() + if t, ok := tableCache[anyT]; ok { + tableCacheMu.RUnlock() + return t, nil + } + tableCacheMu.RUnlock() + if meta.IsSlice(any) { any = meta.CreateElement(any).Interface() } @@ -21,11 +38,17 @@ func NewTable(any interface{}) (*Table, error) { name, sqlName := ReadTableName(any) - return &Table{ + t := &Table{ Name: name, SQLName: sqlName, Fields: fields, - }, nil + } + + tableCacheMu.Lock() + tableCache[anyT] = t + tableCacheMu.Unlock() + + return t, nil } type Table struct {