mirror of
https://github.com/ente-io/ente.git
synced 2025-08-13 17:57:31 +00:00
[server] Refresh fileCount only if trash or usage changed
This commit is contained in:
@@ -188,7 +188,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
userCache := cache2.NewUserCache()
|
userCache := cache2.NewUserCache()
|
||||||
userCacheCtrl := &usercache.Controller{UserCache: userCache, FileRepo: fileRepo, StoreBonusRepo: storagBonusRepo}
|
userCacheCtrl := &usercache.Controller{UserCache: userCache, FileRepo: fileRepo,
|
||||||
|
UsageRepo: usageRepo, TrashRepo: trashRepo,
|
||||||
|
StoreBonusRepo: storagBonusRepo}
|
||||||
offerController := offer.NewOfferController(*userRepo, discordController, storagBonusRepo, userCacheCtrl)
|
offerController := offer.NewOfferController(*userRepo, discordController, storagBonusRepo, userCacheCtrl)
|
||||||
plans := billing.GetPlans()
|
plans := billing.GetPlans()
|
||||||
defaultPlan := billing.GetDefaultPlans(plans)
|
defaultPlan := billing.GetDefaultPlans(plans)
|
||||||
|
14
server/ente/cache/user_data_cache.go
vendored
14
server/ente/cache/user_data_cache.go
vendored
@@ -10,20 +10,26 @@ import (
|
|||||||
// UserCache struct holds can be used to fileCount various entities for user.
|
// UserCache struct holds can be used to fileCount various entities for user.
|
||||||
type UserCache struct {
|
type UserCache struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
fileCache map[string]int64
|
fileCache map[string]*FileCountCache
|
||||||
bonusCache map[int64]*storagebonus.ActiveStorageBonus
|
bonusCache map[int64]*storagebonus.ActiveStorageBonus
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FileCountCache struct {
|
||||||
|
Count int64
|
||||||
|
TrashUpdatedAt int64
|
||||||
|
Usage int64
|
||||||
|
}
|
||||||
|
|
||||||
// NewUserCache creates a new instance of the UserCache struct.
|
// NewUserCache creates a new instance of the UserCache struct.
|
||||||
func NewUserCache() *UserCache {
|
func NewUserCache() *UserCache {
|
||||||
return &UserCache{
|
return &UserCache{
|
||||||
fileCache: make(map[string]int64),
|
fileCache: make(map[string]*FileCountCache),
|
||||||
bonusCache: make(map[int64]*storagebonus.ActiveStorageBonus),
|
bonusCache: make(map[int64]*storagebonus.ActiveStorageBonus),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetFileCount updates the fileCount with the given userID and fileCount.
|
// SetFileCount updates the fileCount with the given userID and fileCount.
|
||||||
func (c *UserCache) SetFileCount(userID, fileCount int64, app ente.App) {
|
func (c *UserCache) SetFileCount(userID int64, fileCount *FileCountCache, app ente.App) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
c.fileCache[cacheKey(userID, app)] = fileCount
|
c.fileCache[cacheKey(userID, app)] = fileCount
|
||||||
@@ -44,7 +50,7 @@ func (c *UserCache) GetBonus(userID int64) (*storagebonus.ActiveStorageBonus, bo
|
|||||||
|
|
||||||
// GetFileCount retrieves the file count from the fileCount for the given userID.
|
// GetFileCount retrieves the file count from the fileCount for the given userID.
|
||||||
// It returns the file count and a boolean indicating if the value was found.
|
// It returns the file count and a boolean indicating if the value was found.
|
||||||
func (c *UserCache) GetFileCount(userID int64, app ente.App) (int64, bool) {
|
func (c *UserCache) GetFileCount(userID int64, app ente.App) (*FileCountCache, bool) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
count, ok := c.fileCache[cacheKey(userID, app)]
|
count, ok := c.fileCache[cacheKey(userID, app)]
|
||||||
|
@@ -14,6 +14,8 @@ import (
|
|||||||
// Avoid adding any direct dependencies to the other controller.
|
// Avoid adding any direct dependencies to the other controller.
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
FileRepo *repo.FileRepository
|
FileRepo *repo.FileRepository
|
||||||
|
UsageRepo *repo.UsageRepository
|
||||||
|
TrashRepo *repo.TrashRepository
|
||||||
StoreBonusRepo *storagebonus.Repository
|
StoreBonusRepo *storagebonus.Repository
|
||||||
UserCache *cache.UserCache
|
UserCache *cache.UserCache
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,9 @@ package usercache
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/ente-io/museum/ente"
|
"github.com/ente-io/museum/ente"
|
||||||
|
"github.com/ente-io/museum/ente/cache"
|
||||||
"github.com/ente-io/stacktrace"
|
"github.com/ente-io/stacktrace"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Controller) GetUserFileCountWithCache(userID int64, app ente.App) (int64, error) {
|
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 {
|
if count, ok := c.UserCache.GetFileCount(userID, app); ok {
|
||||||
// Cache hit, update the cache asynchronously
|
// Cache hit, update the cache asynchronously
|
||||||
go func() {
|
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)
|
count, err := c.FileRepo.GetFileCountForUser(userID, app)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, stacktrace.Propagate(err, "")
|
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
|
return count, nil
|
||||||
}
|
}
|
||||||
|
@@ -422,6 +422,16 @@ func (t *TrashRepository) EmptyTrash(ctx context.Context, userID int64, lastUpda
|
|||||||
return t.QueueRepo.InsertItem(ctx, TrashEmptyQueue, itemID)
|
return t.QueueRepo.InsertItem(ctx, TrashEmptyQueue, itemID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TrashRepository) GetTrashUpdatedAt(userID int64) (int64, error) {
|
||||||
|
row := t.DB.QueryRow(`SELECT max(updated_at) FROM trash WHERE user_id = $1`, userID)
|
||||||
|
var updatedAt int64
|
||||||
|
err := row.Scan(&updatedAt)
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
return updatedAt, stacktrace.Propagate(err, "")
|
||||||
|
}
|
||||||
|
|
||||||
func convertRowsToTrash(rows *sql.Rows) ([]ente.Trash, error) {
|
func convertRowsToTrash(rows *sql.Rows) ([]ente.Trash, error) {
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
trashFiles := make([]ente.Trash, 0)
|
trashFiles := make([]ente.Trash, 0)
|
||||||
|
Reference in New Issue
Block a user