You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.1 KiB
43 lines
1.1 KiB
package dao
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"{{.ModPrefix}}{{.Name}}/internal/model"
|
|
"github.com/bilibili/kratos/pkg/cache/memcache"
|
|
"github.com/bilibili/kratos/pkg/conf/paladin"
|
|
"github.com/bilibili/kratos/pkg/log"
|
|
)
|
|
|
|
//go:generate kratos tool genmc
|
|
type _mc interface {
|
|
// mc: -key=keyArt -type=get
|
|
CacheArticle(c context.Context, id int64) (*model.Article, error)
|
|
// mc: -key=keyArt -expire=d.demoExpire
|
|
AddCacheArticle(c context.Context, id int64, art *model.Article) (err error)
|
|
// mc: -key=keyArt
|
|
DeleteArticleCache(c context.Context, id int64) (err error)
|
|
}
|
|
|
|
func NewMC() (mc *memcache.Memcache, err error) {
|
|
var cfg struct {
|
|
Client *memcache.Config
|
|
}
|
|
if err = paladin.Get("memcache.toml").UnmarshalTOML(&cfg); err != nil {
|
|
return
|
|
}
|
|
mc = memcache.New(cfg.Client)
|
|
return
|
|
}
|
|
|
|
func (d *dao) PingMC(ctx context.Context) (err error) {
|
|
if err = d.mc.Set(ctx, &memcache.Item{Key: "ping", Value: []byte("pong"), Expiration: 0}); err != nil {
|
|
log.Error("conn.Set(PING) error(%v)", err)
|
|
}
|
|
return
|
|
}
|
|
|
|
func keyArt(id int64) string {
|
|
return fmt.Sprintf("art_%d", id)
|
|
}
|
|
|