From f1502178c24c1e53451805404d8589c838722f39 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 2 Oct 2013 11:04:20 +0800 Subject: [PATCH] cache bug fixed --- cache.go | 14 +++++++++++--- session.go | 15 ++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/cache.go b/cache.go index d8bea334..e51f96c2 100644 --- a/cache.go +++ b/cache.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" "sync" + "time" ) type CacheStore interface { @@ -62,12 +63,18 @@ type Cacher interface { } type idNode struct { - tbName string - id int64 + tbName string + id int64 + lastVisit time.Time +} + +type sqlNode struct { + sql string + lastVisit time.Time } func newNode(tbName string, id int64) *idNode { - return &idNode{tbName, id} + return &idNode{tbName, id, time.Now()} } // LRUCacher implements Cacher according to LRU algorithm @@ -79,6 +86,7 @@ type LRUCacher struct { store CacheStore Max int mutex sync.Mutex + expired int } func NewLRUCacher(store CacheStore, max int) *LRUCacher { diff --git a/session.go b/session.go index 380b4c34..a304f38b 100644 --- a/session.go +++ b/session.go @@ -414,8 +414,7 @@ func (statement *Statement) convertIdSql(sql string) string { if statement.RefTable != nil { col := statement.RefTable.PKColumn() if col != nil { - sql = strings.ToLower(sql) - sqls := strings.SplitN(sql, "from", 2) + sqls := splitNNoCase(sql, "from", 2) if len(sqls) != 2 { return "" } @@ -452,7 +451,7 @@ func (session *Session) cacheGet(bean interface{}, sql string, args ...interface data := resultsSlice[0] var id int64 if v, ok := data[session.Statement.RefTable.PrimaryKey]; !ok { - return false, errors.New("no id") + return false, ErrCacheFailed } else { id, err = strconv.ParseInt(string(v), 10, 64) if err != nil { @@ -1425,13 +1424,13 @@ func (statement *Statement) convertUpdateSql(sql string) (string, string) { if statement.RefTable == nil || statement.RefTable.PrimaryKey == "" { return "", "" } - idx := strings.Index(strings.ToLower(sql), "where") - sqls := strings.SplitN(sql, sql[idx:idx+5], 2) + sqls := splitNNoCase(sql, "where", 2) if len(sqls) != 2 { return "", "" } var whereStr = sqls[1] + //TODO: for postgres only, if any other database? if strings.Contains(sqls[1], "$") { dollers := strings.Split(sqls[1], "$") @@ -1513,10 +1512,10 @@ func (session *Session) cacheUpdate(sql string, args ...interface{}) error { ids = append(ids, id) } } - } else { + } /*else { session.Engine.LogDebug("[xorm:cacheUpdate] del cached sql:", tableName, newsql, args) cacher.DelIds(tableName, genSqlKey(newsql, args)) - } + }*/ for _, id := range ids { 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) } } + session.Engine.LogDebug("[xorm:cacheUpdate] clear cached table sql:", tableName) + cacher.ClearIds(tableName) return nil }