Files
simpleorm/cache.go
T
gdulai 01336bb911
Go Tests / test (push) Failing after 6s
Implement DML func and related tests
2026-05-04 21:33:29 +02:00

46 lines
713 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.Type.Name()] = table
}
func (o *OrmCache) Get(typeName string) (*Table, bool) {
o.mu.RLock()
defer o.mu.RUnlock()
table, ok := o.data[typeName]
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
}