Skip to content
This repository was archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.
This repository was archived by the owner on Sep 7, 2021. It is now read-only.

mysql 同時使用 緩存 和 Limit 出現 [cacheFind] cache no hit #902

@zuiwuchang

Description

@zuiwuchang

在 mysql 下 如果同時 使用 緩存 並且在Find時 使用 Limit 且 Limit傳入了第二個參數 start 會 出現 [xorm] [warn] 2018/04/18 15:34:18.631109 [cacheFind] cache no hit:XXXX
出錯時 我的代碼將 beans 定義為了 數組指針 var beans []*InviteCode 後來發現 如果定義為 數組 var beans []InviteCode 可以正常工作
但 如果不使用 緩存 兩者都可以正常工作 我想這可能是個bug 希望 大神能修復它

我出現 [cacheFind] cache no hit 時的完整代碼如下

package main

import (
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"log"
	"time"
)

type InviteCode struct {
	ID      int64     `xorm:"pk autoincr 'id'"`
	Code    string    `xorm:"unique"`
	Created time.Time `xorm:"created"`
}

func main() {
	// connet mysql
	engine, e := xorm.NewEngine(
		"mysql",
		ConnetStr,
	)
	if e != nil {
		log.Fatalln(e)
	}
	defer engine.Close()
	engine.ShowSQL(true)
	e = engine.Ping()
	if e != nil {
		log.Fatalln(e)
	}

	// init cache
	cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
	engine.SetDefaultCacher(cacher)

	// select
	for i := 0; i < 8; i++ {
		var beans []*InviteCode
		e = engine.Limit(10, 10).Find(&beans)
		if e != nil {
			log.Fatalln(e)
			return
		}
		log.Println(i, "---", beans)
	}
	log.Println("success")
}

下面是 log

[xorm] [info]  2018/04/18 15:34:18.629523 PING DATABASE mysql
[xorm] [info]  2018/04/18 15:34:18.630264 [SQL] SELECT `id` FROM `invite_code` LIMIT 10 OFFSET 10
[xorm] [info]  2018/04/18 15:34:18.630523 [SQL] SELECT `id`, `code`, `created` FROM `invite_code` WHERE `id` IN (?,?,?,?,?,?,?) []interface {}{1, 6, 3, 13, 15, 10, 11}
2018/04/18 15:34:18 0 --- [0xc420102780 0xc4201028a0 0xc420102810 0xc420102a50 0xc420102ae0 0xc420102930 0xc4201029c0]
[xorm] [info]  2018/04/18 15:34:18.630915 [SQL] SELECT `id`, `code`, `created` FROM `invite_code` WHERE `id` IN (?,?,?,?,?,?,?) LIMIT 10 OFFSET 10 []interface {}{1, 6, 3, 13, 15, 10, 11}
[xorm] [warn]  2018/04/18 15:34:18.631109 [cacheFind] cache no hit:invite_code[1] [<nil> <nil> <nil> <nil> <nil> <nil> <nil>]
[xorm] [warn]  2018/04/18 15:34:18.631117 [cacheFind] cache no hit:invite_code[6] [<nil> <nil> <nil> <nil> <nil> <nil> <nil>]
[xorm] [warn]  2018/04/18 15:34:18.631120 [cacheFind] cache no hit:invite_code[3] [<nil> <nil> <nil> <nil> <nil> <nil> <nil>]
[xorm] [warn]  2018/04/18 15:34:18.631124 [cacheFind] cache no hit:invite_code[13] [<nil> <nil> <nil> <nil> <nil> <nil> <nil>]
[xorm] [warn]  2018/04/18 15:34:18.631127 [cacheFind] cache no hit:invite_code[15] [<nil> <nil> <nil> <nil> <nil> <nil> <nil>]
[xorm] [warn]  2018/04/18 15:34:18.631130 [cacheFind] cache no hit:invite_code[10] [<nil> <nil> <nil> <nil> <nil> <nil> <nil>]
[xorm] [warn]  2018/04/18 15:34:18.631134 [cacheFind] cache no hit:invite_code[11] [<nil> <nil> <nil> <nil> <nil> <nil> <nil>]
2018/04/18 15:34:18 1 --- []
[xorm] [info]  2018/04/18 15:34:18.631259 [SQL] SELECT `id`, `code`, `created` FROM `invite_code` WHERE `id` IN (?,?,?,?,?,?,?) LIMIT 10 OFFSET 10 []interface {}{1, 6, 3, 13, 15, 10, 11}
[xorm] [warn]  2018/04/18 15:34:18.631401 [cacheFind] cache no hit:invite_code[1] [<nil> <nil> <nil> <nil> <nil> <nil> <nil>]
[xorm] [warn]  2018/04/18 15:34:18.631407 [cacheFind] cache no hit:invite_code[6] [<nil> <nil> <nil> <nil> <nil> <nil> <nil>]
[xorm] [warn]  2018/04/18 15:34:18.631411 [cacheFind] cache no hit:invite_code[3] [<nil> <nil> <nil> <nil> <nil> <nil> <nil>]
[xorm] [warn]  2018/04/18 15:34:18.631414 [cacheFind] cache no hit:invite_code[13] [<nil> <nil> <nil> <nil> <nil> <nil> <nil>]
... 後面都是 同樣的 [cacheFind] cache no hit:invite_code

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions