You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4 KiB
Go

package cache
import (
"context"
"joybuywork/internal/app"
"joybuywork/internal/plugins/jsonm"
"joybuywork/internal/repository/dao"
"joybuywork/internal/repository/dbcheck"
"joybuywork/internal/repository/model"
"fmt"
)
type CampaignChild struct {
Cache
}
func NewCampaignChild(ctx context.Context) *CampaignChild {
c := new(CampaignChild)
c.init(ctx, &model.FissionCampaignChild{})
return c
}
// FindByUniqIdx 通过唯一索引获取数据
func (c CampaignChild) FindByUniqIdx(pcpnId int64, childType int8) (*model.FissionCampaignChild, error) {
data, err := c.get(c.uniqKey(pcpnId, childType), func() ([]byte, error) {
m := dao.Q.FissionCampaignChild
data, err := m.WithContext(c.ctx).Where(m.CampaignID.Eq(pcpnId), m.Type.Eq(childType)).Take()
if err = dbcheck.DbError(err); err != nil {
return nil, err
}
return jsonm.MarshalCkZero(data)
})
if err != nil {
return nil, err
}
var info *model.FissionCampaignChild
err = jsonm.Unmarshal(data, &info)
return info, err
}
func (c CampaignChild) uniqKey(pcpnId int64, childType int8) string {
return fmt.Sprintf("%s:%d_%d", c.Prefix(), pcpnId, childType)
}
// Clear 清理缓存
func (c CampaignChild) Clear(vals ...*model.FissionCampaignChild) error {
var key []string
for _, v := range vals {
key = append(key, c.uniqKey(v.CampaignID, v.Type))
}
if len(key) > 0 {
return app.Redis.Cache.Del(c.ctx, key...).Err()
}
return nil
}