count.go 775 B

123456789101112131415161718192021222324252627
  1. package usercache
  2. import (
  3. "github.com/ente-io/museum/ente"
  4. "github.com/ente-io/stacktrace"
  5. )
  6. func (c *Controller) GetUserFileCountWithCache(userID int64, app ente.App) (int64, error) {
  7. // Check if the value is present in the cache
  8. if count, ok := c.UserCache.GetFileCount(userID, app); ok {
  9. // Cache hit, update the cache asynchronously
  10. go func() {
  11. _, _ = c.getUserCountAndUpdateCache(userID, app)
  12. }()
  13. return count, nil
  14. }
  15. return c.getUserCountAndUpdateCache(userID, app)
  16. }
  17. func (c *Controller) getUserCountAndUpdateCache(userID int64, app ente.App) (int64, error) {
  18. count, err := c.FileRepo.GetFileCountForUser(userID, app)
  19. if err != nil {
  20. return 0, stacktrace.Propagate(err, "")
  21. }
  22. c.UserCache.SetFileCount(userID, count, app)
  23. return count, nil
  24. }