func (q *QueryCache) Register(m database.Model, maxEntries int) { name := m.TableName() if _, ok := q.queryset[name]; ok { panic("duplicate queryset table:" + name) } q.queryset[name] = lru.New(maxEntries, func(key lru.Key, value lru.Value) { q.expires <- value }) }
func (q *QueryCache) GetPk(m database.Model) (database.Model, interface{}, error) { name := m.TableName() if _, ok := q.queryset[name]; !ok { panic("please Register first table:" + name) } _, value := database.GetPkValue(m) cache := q.queryset[name] v, ok := cache.Get(value) if !ok { err := q.Db.GetPk(m) if err != nil { return nil, nil, err } cache.Add(value, m) return m, value, nil } vv := v.(database.Model) return vv, value, nil }