A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

70 lines
1.2 KiB

  1. /*
  2. * Copyright © 2018 Musing Studio LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package writefreely
  11. import (
  12. "sync"
  13. "time"
  14. )
  15. const (
  16. postsCacheTime = 4 * time.Second
  17. )
  18. type (
  19. postsCacheItem struct {
  20. Expire time.Time
  21. Posts *[]PublicPost
  22. ready chan struct{}
  23. }
  24. AuthCache struct {
  25. Alias, Pass, Token string
  26. BadPasses map[string]bool
  27. expire time.Time
  28. }
  29. )
  30. var (
  31. userPostsCache = struct {
  32. sync.RWMutex
  33. users map[int64]postsCacheItem
  34. }{
  35. users: map[int64]postsCacheItem{},
  36. }
  37. )
  38. func CachePosts(userID int64, p *[]PublicPost) {
  39. close(userPostsCache.users[userID].ready)
  40. userPostsCache.Lock()
  41. userPostsCache.users[userID] = postsCacheItem{
  42. Expire: time.Now().Add(postsCacheTime),
  43. Posts: p,
  44. }
  45. userPostsCache.Unlock()
  46. }
  47. func GetPostsCache(userID int64) *[]PublicPost {
  48. userPostsCache.RLock()
  49. pci, ok := userPostsCache.users[userID]
  50. userPostsCache.RUnlock()
  51. if !ok {
  52. return nil
  53. }
  54. if pci.Expire.Before(time.Now()) {
  55. // Cache is expired
  56. return nil
  57. }
  58. return pci.Posts
  59. }