cache
cache copied to clipboard
panic: interface conversion
I got panic when try to get cache value with multiple runs.
panic: interface conversion: interface {} is []interface {}, not main.Users
Code to reproduce. Try this code 2 times.
package main
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/gookit/cache"
)
type User struct {
Name string
}
type Users []*User
const key = "user1"
func main() {
if cache.Has(key) {
u, ok := cache.Get("user1").(Users)
if !ok {
panic("can not assert cache data")
}
fmt.Printf("%#v\n", u[0])
}
u := Users{&User{Name: "TestUser"}}
cache.Set(key, u, 1*time.Hour)
if cache.Has(key) {
fmt.Println("cache written")
u2, ok := cache.Get("user1").(Users)
if !ok || u[0] != u2[0] {
fmt.Println("cache data is not OK")
} else {
fmt.Println("cache is ok")
}
}
}
func init() {
cache.Register(cache.DvrFile, cache.NewFileCache(filepath.Join(os.TempDir(), "tcache-dir")))
}
First time:
cache written
cache is ok
Second time:
panic: can not assert cache data
goroutine 1 [running]:
main.main()
/run/media/unikum/UNIKUM-STORAGE/private/prog/go/tcache/main.go:24 +0x28a
exit status 2