cache system improved
This commit is contained in:
parent
365812143b
commit
768b5bacdb
63
session.go
63
session.go
|
@ -422,29 +422,32 @@ func (session *Session) cacheGet(bean interface{}, sql string, args ...interface
|
||||||
fmt.Printf("-----Cached SQL: %v.\n", newsql)
|
fmt.Printf("-----Cached SQL: %v.\n", newsql)
|
||||||
}
|
}
|
||||||
|
|
||||||
structValue := reflect.Indirect(reflect.ValueOf(bean))
|
|
||||||
//fmt.Println("xxxxxxx", ids)
|
//fmt.Println("xxxxxxx", ids)
|
||||||
if len(ids) > 0 {
|
if len(ids) > 0 {
|
||||||
|
//structValue := reflect.Indirect(reflect.ValueOf(bean))
|
||||||
|
structValue := reflect.Indirect(reflect.ValueOf(bean))
|
||||||
id := ids[0]
|
id := ids[0]
|
||||||
tableName := session.Statement.TableName()
|
tableName := session.Statement.TableName()
|
||||||
bean = GetCacheId(cacher, tableName, id)
|
cacheBean := GetCacheId(cacher, tableName, id)
|
||||||
if bean == nil {
|
if cacheBean == nil {
|
||||||
fmt.Printf("----Object Id %v no cached.\n", id)
|
fmt.Printf("----Object Id %v no cached.\n", id)
|
||||||
newSession := session.Engine.NewSession()
|
newSession := session.Engine.NewSession()
|
||||||
defer newSession.Close()
|
defer newSession.Close()
|
||||||
bean = reflect.New(structValue.Type()).Interface()
|
cacheBean = reflect.New(structValue.Type()).Interface()
|
||||||
has, err = newSession.Id(id).NoCache().Get(bean)
|
has, err = newSession.Id(id).NoCache().Get(cacheBean)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return has, err
|
return has, err
|
||||||
}
|
}
|
||||||
//fmt.Println(bean)
|
//fmt.Println(bean)
|
||||||
PutCacheId(cacher, tableName, id, bean)
|
PutCacheId(cacher, tableName, id, cacheBean)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("-----Cached Object: %v\n", bean)
|
fmt.Printf("-----Cached Object: %v\n", cacheBean)
|
||||||
has = true
|
has = true
|
||||||
}
|
}
|
||||||
|
//fmt.Println(cacheBean, reflect.ValueOf(cacheBean))
|
||||||
|
//fmt.Println(structValue.Addr())
|
||||||
|
structValue.Set(reflect.Indirect(reflect.ValueOf(cacheBean)))
|
||||||
|
|
||||||
structValue.Set(reflect.ValueOf(bean).Elem())
|
|
||||||
return has, nil
|
return has, nil
|
||||||
}
|
}
|
||||||
return false, nil
|
return false, nil
|
||||||
|
@ -474,7 +477,7 @@ func (session *Session) cacheFind(t reflect.Type, sql string, rowsSlicePtr inter
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// 查询数目太大,采用缓存将不是一个很好的方式。
|
// 查询数目太大,采用缓存将不是一个很好的方式。
|
||||||
if len(resultsSlice) > 20 {
|
if len(resultsSlice) > 100 {
|
||||||
return ErrCacheFailed
|
return ErrCacheFailed
|
||||||
}
|
}
|
||||||
ids = make([]int64, 0)
|
ids = make([]int64, 0)
|
||||||
|
@ -506,46 +509,42 @@ func (session *Session) cacheFind(t reflect.Type, sql string, rowsSlicePtr inter
|
||||||
var idxes []int = make([]int, 0)
|
var idxes []int = make([]int, 0)
|
||||||
var ides []interface{} = make([]interface{}, 0)
|
var ides []interface{} = make([]interface{}, 0)
|
||||||
var temps []interface{} = make([]interface{}, len(ids))
|
var temps []interface{} = make([]interface{}, len(ids))
|
||||||
|
tableName := session.Statement.TableName()
|
||||||
for idx, id := range ids {
|
for idx, id := range ids {
|
||||||
tableName := session.Statement.TableName()
|
|
||||||
bean := GetCacheId(cacher, tableName, id)
|
bean := GetCacheId(cacher, tableName, id)
|
||||||
if bean == nil {
|
if bean == nil {
|
||||||
fmt.Printf("----Object Id %v no cached.\n", id)
|
fmt.Printf("----Object Id %v no cached.\n", id)
|
||||||
idxes = append(idxes, idx)
|
idxes = append(idxes, idx)
|
||||||
ides = append(ides, id)
|
ides = append(ides, id)
|
||||||
/*newSession := session.Engine.NewSession()
|
|
||||||
defer newSession.Close()
|
|
||||||
bean = reflect.New(t).Interface()
|
|
||||||
_, err = newSession.Id(id).In(, ...).NoCache().Get(bean)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
PutCacheId(cacher, tableName, id, bean)*/
|
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("-----Cached Object: %v\n", bean)
|
fmt.Printf("-----Cached Object: %v\n", bean)
|
||||||
temps[idx] = bean
|
temps[idx] = bean
|
||||||
}
|
}
|
||||||
|
|
||||||
//sliceValue.Set(reflect.Append(sliceValue, reflect.ValueOf(bean).Elem()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
newSession := session.Engine.NewSession()
|
if len(ides) > 0 {
|
||||||
defer newSession.Close()
|
newSession := session.Engine.NewSession()
|
||||||
|
defer newSession.Close()
|
||||||
|
|
||||||
beans := reflect.New(sliceValue.Type()).Interface()
|
beans := reflect.New(sliceValue.Type()).Interface()
|
||||||
err = newSession.In("(id)", ides...).OrderBy(session.Statement.OrderStr).NoCache().Find(beans)
|
err = newSession.In("(id)", ides...).OrderBy(session.Statement.OrderStr).NoCache().Find(beans)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
vs := reflect.Indirect(reflect.ValueOf(beans))
|
||||||
|
for i := 0; i < vs.Len(); i++ {
|
||||||
|
bean := vs.Index(i).Addr().Interface()
|
||||||
|
temps[idxes[i]] = bean
|
||||||
|
PutCacheId(cacher, tableName, ides[i].(int64), bean)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vs := reflect.Indirect(reflect.ValueOf(beans))
|
for j := 0; j < len(temps); j++ {
|
||||||
for i := 0; i < vs.Len(); i++ {
|
bean := temps[j]
|
||||||
temps[idxes[i]] = vs.Index(i).Interface()
|
sliceValue.Set(reflect.Append(sliceValue, reflect.Indirect(reflect.ValueOf(bean))))
|
||||||
}
|
}
|
||||||
|
|
||||||
//sliceValue.SetPointer(x)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue