bug fixed
This commit is contained in:
parent
affc64cde3
commit
29a72f9adb
22
cache.go
22
cache.go
|
@ -133,8 +133,8 @@ func decodeIds(s string) []int64 {
|
|||
return res
|
||||
}
|
||||
|
||||
func GetCacheSql(m Cacher, sql string) ([]int64, error) {
|
||||
bytes := m.Get(sql)
|
||||
func getCacheSql(m Cacher, sql string, args ...interface{}) ([]int64, error) {
|
||||
bytes := m.Get(genSqlKey(sql, args))
|
||||
if bytes == nil {
|
||||
return nil, errors.New("Not Exist")
|
||||
}
|
||||
|
@ -142,31 +142,35 @@ func GetCacheSql(m Cacher, sql string) ([]int64, error) {
|
|||
return objs, nil
|
||||
}
|
||||
|
||||
func PutCacheSql(m Cacher, sql string, ids []int64) error {
|
||||
func putCacheSql(m Cacher, ids []int64, sql string, args ...interface{}) error {
|
||||
bytes := encodeIds(ids)
|
||||
m.Put(sql, bytes)
|
||||
m.Put(genSqlKey(sql, args), bytes)
|
||||
return nil
|
||||
}
|
||||
|
||||
func DelCacheSql(m Cacher, sql string) error {
|
||||
m.Del(sql)
|
||||
func delCacheSql(m Cacher, sql string, args ...interface{}) error {
|
||||
m.Del(genSqlKey(sql, args))
|
||||
return nil
|
||||
}
|
||||
|
||||
func genSqlKey(sql string, args ...interface{}) string {
|
||||
return fmt.Sprintf("%v-%v", sql, args)
|
||||
}
|
||||
|
||||
func genId(prefix string, id int64) string {
|
||||
return fmt.Sprintf("%v-%v", prefix, id)
|
||||
}
|
||||
|
||||
func GetCacheId(m Cacher, prefix string, id int64) interface{} {
|
||||
func getCacheId(m Cacher, prefix string, id int64) interface{} {
|
||||
return m.Get(genId(prefix, id))
|
||||
}
|
||||
|
||||
func PutCacheId(m Cacher, prefix string, id int64, bean interface{}) error {
|
||||
func putCacheId(m Cacher, prefix string, id int64, bean interface{}) error {
|
||||
m.Put(genId(prefix, id), bean)
|
||||
return nil
|
||||
}
|
||||
|
||||
func DelCacheId(m Cacher, prefix string, id int64) error {
|
||||
func delCacheId(m Cacher, prefix string, id int64) error {
|
||||
m.Del(genId(prefix, id))
|
||||
//TODO: should delete id from select
|
||||
return nil
|
||||
|
|
|
@ -59,7 +59,8 @@ func test(engine *xorm.Engine) {
|
|||
} else if x+j < 16 {
|
||||
_, err = engine.Insert(&User{Name: "xlw"})
|
||||
} else if x+j < 32 {
|
||||
_, err = engine.Id(1).Delete(u)
|
||||
//_, err = engine.Id(1).Delete(u)
|
||||
_, err = engine.Delete(u)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
|
|
@ -37,6 +37,8 @@ type IdFilter struct {
|
|||
|
||||
func (i *IdFilter) Do(sql string, session *Session) string {
|
||||
if session.Statement.RefTable != nil && session.Statement.RefTable.PrimaryKey != "" {
|
||||
sql = strings.Replace(sql, "`(id)`", session.Engine.Quote(session.Statement.RefTable.PrimaryKey), -1)
|
||||
sql = strings.Replace(sql, session.Engine.Quote("(id)"), session.Engine.Quote(session.Statement.RefTable.PrimaryKey), -1)
|
||||
return strings.Replace(sql, "(id)", session.Engine.Quote(session.Statement.RefTable.PrimaryKey), -1)
|
||||
}
|
||||
return sql
|
||||
|
|
30
session.go
30
session.go
|
@ -391,7 +391,7 @@ func (session *Session) cacheGet(bean interface{}, sql string, args ...interface
|
|||
}
|
||||
|
||||
cacher := session.Statement.RefTable.Cacher
|
||||
ids, err := GetCacheSql(cacher, newsql)
|
||||
ids, err := getCacheSql(cacher, newsql, args)
|
||||
if err != nil {
|
||||
//fmt.Println(err)
|
||||
resultsSlice, err := session.query(newsql, args...)
|
||||
|
@ -414,7 +414,7 @@ func (session *Session) cacheGet(bean interface{}, sql string, args ...interface
|
|||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
err = PutCacheSql(cacher, newsql, ids)
|
||||
err = putCacheSql(cacher, ids, newsql, args)
|
||||
if err != nil {
|
||||
//fmt.Println(err)
|
||||
return false, err
|
||||
|
@ -429,7 +429,7 @@ func (session *Session) cacheGet(bean interface{}, sql string, args ...interface
|
|||
structValue := reflect.Indirect(reflect.ValueOf(bean))
|
||||
id := ids[0]
|
||||
tableName := session.Statement.TableName()
|
||||
cacheBean := GetCacheId(cacher, tableName, id)
|
||||
cacheBean := getCacheId(cacher, tableName, id)
|
||||
if cacheBean == nil {
|
||||
//fmt.Printf("----Object Id %v no cached.\n", id)
|
||||
newSession := session.Engine.NewSession()
|
||||
|
@ -440,7 +440,7 @@ func (session *Session) cacheGet(bean interface{}, sql string, args ...interface
|
|||
return has, err
|
||||
}
|
||||
//fmt.Println(bean)
|
||||
PutCacheId(cacher, tableName, id, cacheBean)
|
||||
putCacheId(cacher, tableName, id, cacheBean)
|
||||
} else {
|
||||
//fmt.Printf("-----Cached Object: %v\n", cacheBean)
|
||||
has = true
|
||||
|
@ -470,7 +470,7 @@ func (session *Session) cacheFind(t reflect.Type, sql string, rowsSlicePtr inter
|
|||
|
||||
table := session.Statement.RefTable
|
||||
cacher := table.Cacher
|
||||
ids, err := GetCacheSql(cacher, newsql)
|
||||
ids, err := getCacheSql(cacher, newsql)
|
||||
if err != nil {
|
||||
//fmt.Println(err)
|
||||
resultsSlice, err := session.query(newsql, args...)
|
||||
|
@ -497,7 +497,7 @@ func (session *Session) cacheFind(t reflect.Type, sql string, rowsSlicePtr inter
|
|||
ids = append(ids, id)
|
||||
}
|
||||
}
|
||||
err = PutCacheSql(cacher, newsql, ids)
|
||||
err = putCacheSql(cacher, ids, newsql, args)
|
||||
if err != nil {
|
||||
//fmt.Println(err)
|
||||
return err
|
||||
|
@ -513,7 +513,7 @@ func (session *Session) cacheFind(t reflect.Type, sql string, rowsSlicePtr inter
|
|||
var temps []interface{} = make([]interface{}, len(ids))
|
||||
tableName := session.Statement.TableName()
|
||||
for idx, id := range ids {
|
||||
bean := GetCacheId(cacher, tableName, id)
|
||||
bean := getCacheId(cacher, tableName, id)
|
||||
if bean == nil {
|
||||
//fmt.Printf("----Object Id %v no cached.\n", id)
|
||||
idxes = append(idxes, idx)
|
||||
|
@ -538,7 +538,7 @@ func (session *Session) cacheFind(t reflect.Type, sql string, rowsSlicePtr inter
|
|||
for i := 0; i < vs.Len(); i++ {
|
||||
bean := vs.Index(i).Addr().Interface()
|
||||
temps[idxes[i]] = bean
|
||||
PutCacheId(cacher, tableName, ides[i].(int64), bean)
|
||||
putCacheId(cacher, tableName, ides[i].(int64), bean)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1331,7 +1331,7 @@ func (session *Session) cacheUpdate(sql string, args ...interface{}) error {
|
|||
}
|
||||
table := session.Statement.RefTable
|
||||
cacher := table.Cacher
|
||||
ids, err := GetCacheSql(cacher, newsql)
|
||||
ids, err := getCacheSql(cacher, newsql)
|
||||
if err != nil {
|
||||
resultsSlice, err := session.query(newsql, args[nStart:]...)
|
||||
if err != nil {
|
||||
|
@ -1354,11 +1354,11 @@ func (session *Session) cacheUpdate(sql string, args ...interface{}) error {
|
|||
}
|
||||
} else {
|
||||
//fmt.Printf("-----Cached SQL: %v.\n", newsql)
|
||||
DelCacheSql(cacher, newsql)
|
||||
delCacheSql(cacher, newsql)
|
||||
}
|
||||
|
||||
for _, id := range ids {
|
||||
if bean := GetCacheId(cacher, session.Statement.TableName(), id); bean != nil {
|
||||
if bean := getCacheId(cacher, session.Statement.TableName(), id); bean != nil {
|
||||
sqls := strings.SplitN(strings.ToLower(sql), "where", 2)
|
||||
if len(sqls) != 2 {
|
||||
return nil
|
||||
|
@ -1385,7 +1385,7 @@ func (session *Session) cacheUpdate(sql string, args ...interface{}) error {
|
|||
}
|
||||
}
|
||||
|
||||
PutCacheId(cacher, session.Statement.TableName(), id, bean)
|
||||
putCacheId(cacher, session.Statement.TableName(), id, bean)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
@ -1495,7 +1495,7 @@ func (session *Session) cacheDelete(sql string, args ...interface{}) error {
|
|||
}
|
||||
|
||||
cacher := session.Statement.RefTable.Cacher
|
||||
ids, err := GetCacheSql(cacher, newsql)
|
||||
ids, err := getCacheSql(cacher, newsql)
|
||||
if err != nil {
|
||||
resultsSlice, err := session.query(newsql, args...)
|
||||
if err != nil {
|
||||
|
@ -1518,11 +1518,11 @@ func (session *Session) cacheDelete(sql string, args ...interface{}) error {
|
|||
}
|
||||
} else {
|
||||
//fmt.Printf("-----Cached SQL: %v.\n", newsql)
|
||||
DelCacheSql(cacher, newsql)
|
||||
delCacheSql(cacher, newsql)
|
||||
}
|
||||
|
||||
for _, id := range ids {
|
||||
DelCacheId(cacher, session.Statement.TableName(), id)
|
||||
delCacheId(cacher, session.Statement.TableName(), id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue