Cache list, count with filter param golang, using struct, hashkey
we hash your request object to md5 hashing and add a prefix count or list. data storage to redis using get/set with expire time.(default by 2 mins)
go get github.com/teng231/dumbcache
- LIST
func ListData() {
d := &DumbCache{}
err := d.Connect("localhost:6379", "", 0, 5*time.Second, 5*time.Minute)
if err != nil {
log.Print(1, err)
}
data := []*Partner{}
err = d.List(&PartnerRequest{Id: 10, Limit: 2}, &data, func() (interface{}, error) {
list, err := db.ListPartners(req)
return list, err
})
if err != nil {
log.Print(err)
}
log.Print(data)
}
- Count
func CountData(t *testing.T) {
d := &DumbCache{}
err := d.Connect("localhost:6379", "", 0, 5*time.Second, 5*time.Minute)
if err != nil {
log.Print(1, err)
}
var c int64 = 0
err = d.Count(&PartnerRequest{Id: 10, Limit: 3}, &c, func() (int64, error) {
count, err := db.CountPartners(req)
return count, err
})
if err != nil {
log.Print(err)
}
log.Print(c)
}
- CalcInt
func CalcData(t *testing.T) {
d := &DumbCache{}
err := d.Connect("localhost:6379", "", 0, 5*time.Second, 5*time.Minute)
if err != nil {
log.Print(1, err)
}
var c int64 = 0
err = d.CalcInt(&PartnerRequest{Id: 10, Limit: 3}, &c, func() (int64, error) {
count, err := db.SumPartners(req)
return count, err
})
if err != nil {
log.Print(err)
}
log.Print(c)
}
- Expire
func Expire() {
d := &DumbCache{}
err := d.Connect("localhost:6379", "", 0, 5*time.Second, 5*time.Minute)
if err != nil {
log.Print(1, err)
}
var c int64 = 0
err = d.Expire(&PartnerRequest{Id: 10, Limit: 3})
if err != nil {
log.Print(err)
}
log.Print(c)
}