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 NotifyURL struct { Cache } func NewNotifyURL(ctx context.Context) *NotifyURL { c := new(NotifyURL) c.init(ctx, &model.NotifyURL{}) return c } // FindById id 获取数据 func (c NotifyURL) FindById(id int64) (*model.NotifyURL, error) { data, err := c.get(c.idKey(id), func() ([]byte, error) { m := dao.Q.NotifyURL data, err := m.WithContext(c.ctx).Where(m.ID.Eq(id)).Take() if err = dbcheck.DbError(err); err != nil { return nil, err } return jsonm.MarshalCkZero(data) }) if err != nil { return nil, err } var info *model.NotifyURL err = jsonm.Unmarshal(data, &info) return info, err } func (c NotifyURL) idKey(id int64) string { return fmt.Sprintf("%s:%d", c.Prefix(), id) } // Clear 清理缓存 func (c NotifyURL) Clear(urls ...*model.NotifyURL) error { var key []string for _, v := range urls { key = append(key, c.idKey(v.ID)) } if len(key) > 0 { return app.Redis.Cache.Del(c.ctx, key...).Err() } return nil }