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.
kratos/tool/kratos-gen-project/templates/grpc/internal/dao/mc.go.tmpl

49 lines
1.2 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"
)
5 years ago
//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, cf func(), err error) {
var (
cfg memcache.Config
ct paladin.TOML
)
if err = paladin.Get("memcache.toml").Unmarshal(&ct); err != nil {
return
}
if err = ct.Get("Client").UnmarshalTOML(&cfg); err != nil {
return
}
mc = memcache.New(&cfg)
cf = func() {mc.Close()}
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)
}