46 lines
712 B
Go
46 lines
712 B
Go
package simpleorm
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type OrmCache struct {
|
|
mu sync.RWMutex
|
|
data map[string]*Table
|
|
}
|
|
|
|
func NewOrmCache(tables []*Table) *OrmCache {
|
|
cache := &OrmCache{
|
|
data: make(map[string]*Table),
|
|
}
|
|
for _, table := range tables {
|
|
cache.add(table)
|
|
}
|
|
|
|
return cache
|
|
}
|
|
|
|
func (o *OrmCache) add(table *Table) {
|
|
o.mu.Lock()
|
|
defer o.mu.Unlock()
|
|
o.data[table.TypeName] = table
|
|
}
|
|
|
|
func (o *OrmCache) Get(tableName string) (*Table, bool) {
|
|
o.mu.RLock()
|
|
defer o.mu.RUnlock()
|
|
table, ok := o.data[tableName]
|
|
return table, ok
|
|
}
|
|
|
|
func (o *OrmCache) GetAll() []*Table {
|
|
o.mu.RLock()
|
|
defer o.mu.RUnlock()
|
|
var result []*Table
|
|
|
|
for _, v := range o.data {
|
|
result = append(result, v)
|
|
}
|
|
return result
|
|
}
|