Skip to content

Commit

Permalink
预哈希所有头像以解决服务重启后缓存页面刷新时头像加载失败的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
movsb committed Apr 28, 2024
1 parent 366c66d commit 6716890
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
14 changes: 7 additions & 7 deletions service/avatar.go
Expand Up @@ -101,7 +101,7 @@ func (s *Service) GetAvatar(in *protocols.GetAvatarRequest) {

// 删除可能有隐私的头部字段。
// TODO:内部缓存,只正向代理 body。
for k := range knownHeaders {
for _, k := range knownHeaders {
if v := resp.Header.Get(k); v != "" {
in.SetHeader(k, v)
}
Expand All @@ -112,10 +112,10 @@ func (s *Service) GetAvatar(in *protocols.GetAvatarRequest) {
io.Copy(in.W, resp.Body)
}

var knownHeaders = map[string]bool{
`Content-Length`: true,
`Content-Type`: true,
`Last-Modified`: true,
`Expires`: true,
`Cache-Control`: true,
var knownHeaders = []string{
`Content-Length`,
`Content-Type`,
`Last-Modified`,
`Expires`,
`Cache-Control`,
}
12 changes: 12 additions & 0 deletions service/comment.go
Expand Up @@ -40,6 +40,18 @@ func (s *Service) avatar(email string) int {
return s.avatarCache.ID(email)
}

// 像二狗说的那样,服务启动时缓存所有的头像哈希值,
// 否则缓存的页面图片在服务重启后刷新时会加载失败。
// https://qwq.me/p/249/1#comment-506
// NOTE: ORM 不支持 distinct,所以没写。
func (s *Service) cacheAllEmailAvatars() {
var comments models.Comments
s.tdb.Select(`email`).MustFind(&comments)
for _, c := range comments {
_ = s.avatarCache.ID(c.Email)
}
}

// GetComment ...
// TODO perm check
// TODO remove email & user
Expand Down
3 changes: 3 additions & 0 deletions service/main.go
Expand Up @@ -68,8 +68,11 @@ func NewService(cfg *config.Config, db *sql.DB, auther *auth.Auth) *Service {
AdminEmail: s.cfg.Comment.Emails[0], // TODO 如果没配置,则不启用此功能
Config: &s.cfg.Comment,
}

s.cmtntf.Init()

s.cacheAllEmailAvatars()

server := grpc.NewServer(
grpc_middleware.WithUnaryServerChain(
grpc_recovery.UnaryServerInterceptor(grpc_recovery.WithRecoveryHandler(exceptionRecoveryHandler)),
Expand Down

0 comments on commit 6716890

Please sign in to comment.