[server] Refresh fileCount only if trash or usage changed
This commit is contained in:
@@ -14,6 +14,8 @@ import (
|
||||
// Avoid adding any direct dependencies to the other controller.
|
||||
type Controller struct {
|
||||
FileRepo *repo.FileRepository
|
||||
UsageRepo *repo.UsageRepository
|
||||
TrashRepo *repo.TrashRepository
|
||||
StoreBonusRepo *storagebonus.Repository
|
||||
UserCache *cache.UserCache
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@ package usercache
|
||||
|
||||
import (
|
||||
"github.com/ente-io/museum/ente"
|
||||
"github.com/ente-io/museum/ente/cache"
|
||||
"github.com/ente-io/stacktrace"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (c *Controller) GetUserFileCountWithCache(userID int64, app ente.App) (int64, error) {
|
||||
@@ -10,18 +12,35 @@ func (c *Controller) GetUserFileCountWithCache(userID int64, app ente.App) (int6
|
||||
if count, ok := c.UserCache.GetFileCount(userID, app); ok {
|
||||
// Cache hit, update the cache asynchronously
|
||||
go func() {
|
||||
_, _ = c.getUserCountAndUpdateCache(userID, app)
|
||||
_, _ = c.getUserCountAndUpdateCache(userID, app, count)
|
||||
}()
|
||||
return count, nil
|
||||
return count.Count, nil
|
||||
}
|
||||
return c.getUserCountAndUpdateCache(userID, app)
|
||||
return c.getUserCountAndUpdateCache(userID, app, nil)
|
||||
}
|
||||
|
||||
func (c *Controller) getUserCountAndUpdateCache(userID int64, app ente.App) (int64, error) {
|
||||
func (c *Controller) getUserCountAndUpdateCache(userID int64, app ente.App, oldCache *cache.FileCountCache) (int64, error) {
|
||||
usage, err := c.UsageRepo.GetUsage(userID)
|
||||
if err != nil {
|
||||
return 0, stacktrace.Propagate(err, "")
|
||||
}
|
||||
trashUpdatedAt, err := c.TrashRepo.GetTrashUpdatedAt(userID)
|
||||
if err != nil {
|
||||
return 0, stacktrace.Propagate(err, "")
|
||||
}
|
||||
if oldCache != nil && oldCache.Usage == usage && oldCache.TrashUpdatedAt == trashUpdatedAt {
|
||||
logrus.Debugf("Cache hit for user %d", userID)
|
||||
return oldCache.Count, nil
|
||||
}
|
||||
count, err := c.FileRepo.GetFileCountForUser(userID, app)
|
||||
if err != nil {
|
||||
return 0, stacktrace.Propagate(err, "")
|
||||
}
|
||||
c.UserCache.SetFileCount(userID, count, app)
|
||||
cntCache := &cache.FileCountCache{
|
||||
Count: count,
|
||||
Usage: usage,
|
||||
TrashUpdatedAt: trashUpdatedAt,
|
||||
}
|
||||
c.UserCache.SetFileCount(userID, cntCache, app)
|
||||
return count, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user