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.
87 lines
1.9 KiB
Go
87 lines
1.9 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 ProjectUser struct {
|
|
Cache
|
|
}
|
|
|
|
func NewProjectUser(ctx context.Context) *ProjectUser {
|
|
c := new(ProjectUser)
|
|
c.init(ctx, &model.ProjectUser{})
|
|
|
|
return c
|
|
}
|
|
|
|
// FindById 获取数据
|
|
func (c ProjectUser) FindById(id int64) (*model.ProjectUser, error) {
|
|
data, err := c.get(c.pkKey(id), func() ([]byte, error) {
|
|
m := dao.Q.ProjectUser
|
|
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 project *model.ProjectUser
|
|
err = jsonm.Unmarshal(data, &project)
|
|
|
|
return project, err
|
|
}
|
|
|
|
// FindByUniqId 获取数据
|
|
func (c ProjectUser) FindByUniqId(projectId int64, userId string) (*model.ProjectUser, error) {
|
|
data, err := c.get(c.uniqId(projectId, userId), func() ([]byte, error) {
|
|
m := dao.Q.ProjectUser
|
|
data, err := m.WithContext(c.ctx).Where(m.ProjectID.Eq(projectId), m.UserID.Eq(userId)).Take()
|
|
if err = dbcheck.DbError(err); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return jsonm.MarshalCkZero(data)
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var project *model.ProjectUser
|
|
err = jsonm.Unmarshal(data, &project)
|
|
|
|
return project, err
|
|
}
|
|
|
|
func (c ProjectUser) pkKey(id int64) string {
|
|
return fmt.Sprintf("%s:%d", c.Prefix(), id)
|
|
}
|
|
|
|
func (c ProjectUser) uniqId(projectId int64, userId string) string {
|
|
return fmt.Sprintf("%s:project_id:%d:user_id:%s", c.Prefix(), projectId, userId)
|
|
}
|
|
|
|
// Clear 清理缓存
|
|
func (c ProjectUser) Clear(projects ...*model.ProjectUser) error {
|
|
var key []string
|
|
for _, v := range projects {
|
|
key = append(key, c.uniqId(v.ProjectID, v.UserID), c.pkKey(v.ID))
|
|
}
|
|
|
|
if len(key) > 0 {
|
|
return app.Redis.Cache.Del(c.ctx, key...).Err()
|
|
}
|
|
|
|
return nil
|
|
}
|