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.
65 lines
1.5 KiB
Go
65 lines
1.5 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 FissionUserAttrTotal struct {
|
|
Cache
|
|
}
|
|
|
|
func NewFissionUserAttrTotal(ctx context.Context) *FissionUserAttrTotal {
|
|
c := new(FissionUserAttrTotal)
|
|
c.init(ctx, &model.FissionUserAttrTotal{})
|
|
|
|
return c
|
|
}
|
|
|
|
// FindByUniqIdx 通过唯一索引获取数据
|
|
func (c FissionUserAttrTotal) FindByUniqIdx(campaignId int64, attr, val string) (int64, error) {
|
|
|
|
key := c.IdxKey("campaign_id", campaignId, "attr", attr, "val", val)
|
|
id, err := c.getIdxInt64(key, func() ([]byte, error) {
|
|
m := dao.Q.FissionUserAttrTotal
|
|
data, err := m.WithContext(c.ctx).Where(m.CampaignID.Eq(campaignId), m.Attr.Eq(attr), m.Val.Eq(val)).Take()
|
|
if err = dbcheck.DbError(err); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if data == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
return jsonm.MarshalCkZero(data.ID)
|
|
})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return id, err
|
|
}
|
|
|
|
func (c FissionUserAttrTotal) pkKey(id int64) string {
|
|
return fmt.Sprintf("%s:%d", c.Prefix(), id)
|
|
}
|
|
|
|
// Clear 清理缓存
|
|
func (c FissionUserAttrTotal) Clear(vals ...*model.FissionUserAttrTotal) error {
|
|
var key []string
|
|
for _, v := range vals {
|
|
key = append(key, c.pkKey(v.ID), c.IdxKey("campaign_id", v.CampaignID, "attr", v.Attr, "val", v.Val))
|
|
}
|
|
|
|
if len(key) > 0 {
|
|
return app.Redis.Cache.Del(c.ctx, key...).Err()
|
|
}
|
|
|
|
return nil
|
|
}
|