cache bug fixed

This commit is contained in:
Lunny Xiao 2013-10-02 11:04:20 +08:00
parent 8294b99ea6
commit f1502178c2
2 changed files with 19 additions and 10 deletions

View File

@ -8,6 +8,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time"
) )
type CacheStore interface { type CacheStore interface {
@ -64,10 +65,16 @@ type Cacher interface {
type idNode struct { type idNode struct {
tbName string tbName string
id int64 id int64
lastVisit time.Time
}
type sqlNode struct {
sql string
lastVisit time.Time
} }
func newNode(tbName string, id int64) *idNode { func newNode(tbName string, id int64) *idNode {
return &idNode{tbName, id} return &idNode{tbName, id, time.Now()}
} }
// LRUCacher implements Cacher according to LRU algorithm // LRUCacher implements Cacher according to LRU algorithm
@ -79,6 +86,7 @@ type LRUCacher struct {
store CacheStore store CacheStore
Max int Max int
mutex sync.Mutex mutex sync.Mutex
expired int
} }
func NewLRUCacher(store CacheStore, max int) *LRUCacher { func NewLRUCacher(store CacheStore, max int) *LRUCacher {

View File

@ -414,8 +414,7 @@ func (statement *Statement) convertIdSql(sql string) string {
if statement.RefTable != nil { if statement.RefTable != nil {
col := statement.RefTable.PKColumn() col := statement.RefTable.PKColumn()
if col != nil { if col != nil {
sql = strings.ToLower(sql) sqls := splitNNoCase(sql, "from", 2)
sqls := strings.SplitN(sql, "from", 2)
if len(sqls) != 2 { if len(sqls) != 2 {
return "" return ""
} }
@ -452,7 +451,7 @@ func (session *Session) cacheGet(bean interface{}, sql string, args ...interface
data := resultsSlice[0] data := resultsSlice[0]
var id int64 var id int64
if v, ok := data[session.Statement.RefTable.PrimaryKey]; !ok { if v, ok := data[session.Statement.RefTable.PrimaryKey]; !ok {
return false, errors.New("no id") return false, ErrCacheFailed
} else { } else {
id, err = strconv.ParseInt(string(v), 10, 64) id, err = strconv.ParseInt(string(v), 10, 64)
if err != nil { if err != nil {
@ -1425,13 +1424,13 @@ func (statement *Statement) convertUpdateSql(sql string) (string, string) {
if statement.RefTable == nil || statement.RefTable.PrimaryKey == "" { if statement.RefTable == nil || statement.RefTable.PrimaryKey == "" {
return "", "" return "", ""
} }
idx := strings.Index(strings.ToLower(sql), "where") sqls := splitNNoCase(sql, "where", 2)
sqls := strings.SplitN(sql, sql[idx:idx+5], 2)
if len(sqls) != 2 { if len(sqls) != 2 {
return "", "" return "", ""
} }
var whereStr = sqls[1] var whereStr = sqls[1]
//TODO: for postgres only, if any other database? //TODO: for postgres only, if any other database?
if strings.Contains(sqls[1], "$") { if strings.Contains(sqls[1], "$") {
dollers := strings.Split(sqls[1], "$") dollers := strings.Split(sqls[1], "$")
@ -1513,10 +1512,10 @@ func (session *Session) cacheUpdate(sql string, args ...interface{}) error {
ids = append(ids, id) ids = append(ids, id)
} }
} }
} else { } /*else {
session.Engine.LogDebug("[xorm:cacheUpdate] del cached sql:", tableName, newsql, args) session.Engine.LogDebug("[xorm:cacheUpdate] del cached sql:", tableName, newsql, args)
cacher.DelIds(tableName, genSqlKey(newsql, args)) cacher.DelIds(tableName, genSqlKey(newsql, args))
} }*/
for _, id := range ids { for _, id := range ids {
if bean := cacher.GetBean(tableName, id); bean != nil { if bean := cacher.GetBean(tableName, id); bean != nil {
@ -1554,6 +1553,8 @@ func (session *Session) cacheUpdate(sql string, args ...interface{}) error {
cacher.PutBean(tableName, id, bean) cacher.PutBean(tableName, id, bean)
} }
} }
session.Engine.LogDebug("[xorm:cacheUpdate] clear cached table sql:", tableName)
cacher.ClearIds(tableName)
return nil return nil
} }