57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package cache
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/ente-io/museum/ente"
|
|
"github.com/ente-io/museum/ente/storagebonus"
|
|
"sync"
|
|
)
|
|
|
|
// UserCache struct holds can be used to fileCount various entities for user.
|
|
type UserCache struct {
|
|
mu sync.Mutex
|
|
fileCache map[string]int64
|
|
bonusCache map[int64]*storagebonus.ActiveStorageBonus
|
|
}
|
|
|
|
// NewUserCache creates a new instance of the UserCache struct.
|
|
func NewUserCache() *UserCache {
|
|
return &UserCache{
|
|
fileCache: make(map[string]int64),
|
|
bonusCache: make(map[int64]*storagebonus.ActiveStorageBonus),
|
|
}
|
|
}
|
|
|
|
// SetFileCount updates the fileCount with the given userID and fileCount.
|
|
func (c *UserCache) SetFileCount(userID, fileCount int64, app ente.App) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
c.fileCache[cacheKey(userID, app)] = fileCount
|
|
}
|
|
|
|
func (c *UserCache) SetBonus(userID int64, bonus *storagebonus.ActiveStorageBonus) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
c.bonusCache[userID] = bonus
|
|
}
|
|
|
|
func (c *UserCache) GetBonus(userID int64) (*storagebonus.ActiveStorageBonus, bool) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
bonus, ok := c.bonusCache[userID]
|
|
return bonus, ok
|
|
}
|
|
|
|
// 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.
|
|
func (c *UserCache) GetFileCount(userID int64, app ente.App) (int64, bool) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
count, ok := c.fileCache[cacheKey(userID, app)]
|
|
return count, ok
|
|
}
|
|
|
|
func cacheKey(userID int64, app ente.App) string {
|
|
return fmt.Sprintf("%d-%s", userID, app)
|
|
}
|