Example #1
0
File: db.go Project: xiaobodu/cham
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
	})
}
Example #2
0
File: db.go Project: xiaobodu/cham
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
}