refactor: 重构服务层和仓库层
This commit is contained in:
@@ -120,6 +120,37 @@ func (s *textureService) GetByID(ctx context.Context, id int64) (*model.Texture,
|
||||
return texture2, nil
|
||||
}
|
||||
|
||||
func (s *textureService) GetByHash(ctx context.Context, hash string) (*model.Texture, error) {
|
||||
// 尝试从缓存获取
|
||||
cacheKey := s.cacheKeys.TextureByHash(hash)
|
||||
var texture model.Texture
|
||||
if err := s.cache.Get(ctx, cacheKey, &texture); err == nil {
|
||||
if texture.Status == -1 {
|
||||
return nil, errors.New("材质已删除")
|
||||
}
|
||||
return &texture, nil
|
||||
}
|
||||
|
||||
// 缓存未命中,从数据库查询
|
||||
texture2, err := s.textureRepo.FindByHash(hash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if texture2 == nil {
|
||||
return nil, ErrTextureNotFound
|
||||
}
|
||||
if texture2.Status == -1 {
|
||||
return nil, errors.New("材质已删除")
|
||||
}
|
||||
|
||||
// 存入缓存(异步,5分钟过期)
|
||||
go func() {
|
||||
_ = s.cache.Set(context.Background(), cacheKey, texture2, 5*time.Minute)
|
||||
}()
|
||||
|
||||
return texture2, nil
|
||||
}
|
||||
|
||||
func (s *textureService) GetByUserID(ctx context.Context, uploaderID int64, page, pageSize int) ([]*model.Texture, int64, error) {
|
||||
page, pageSize = NormalizePagination(page, pageSize)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user