A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

60 行
944 B

  1. package writefreely
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. const (
  7. postsCacheTime = 4 * time.Second
  8. )
  9. type (
  10. postsCacheItem struct {
  11. Expire time.Time
  12. Posts *[]PublicPost
  13. ready chan struct{}
  14. }
  15. AuthCache struct {
  16. Alias, Pass, Token string
  17. BadPasses map[string]bool
  18. expire time.Time
  19. }
  20. )
  21. var (
  22. userPostsCache = struct {
  23. sync.RWMutex
  24. users map[int64]postsCacheItem
  25. }{
  26. users: map[int64]postsCacheItem{},
  27. }
  28. )
  29. func CachePosts(userID int64, p *[]PublicPost) {
  30. close(userPostsCache.users[userID].ready)
  31. userPostsCache.Lock()
  32. userPostsCache.users[userID] = postsCacheItem{
  33. Expire: time.Now().Add(postsCacheTime),
  34. Posts: p,
  35. }
  36. userPostsCache.Unlock()
  37. }
  38. func GetPostsCache(userID int64) *[]PublicPost {
  39. userPostsCache.RLock()
  40. pci, ok := userPostsCache.users[userID]
  41. userPostsCache.RUnlock()
  42. if !ok {
  43. return nil
  44. }
  45. if pci.Expire.Before(time.Now()) {
  46. // Cache is expired
  47. return nil
  48. }
  49. return pci.Posts
  50. }