Import museum

This commit is contained in:
Neeraj Gupta
2024-03-01 13:37:01 +05:30
parent e9d76688ce
commit 531bb344fe
377 changed files with 37454 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
package usercache
import (
"context"
"github.com/ente-io/museum/ente/cache"
bonus "github.com/ente-io/museum/ente/storagebonus"
"github.com/ente-io/museum/pkg/repo"
"github.com/ente-io/museum/pkg/repo/storagebonus"
"github.com/ente-io/stacktrace"
)
// Controller is the controller for the data cache.
// It contains all the repositories that are used by the controller.
// Avoid adding any direct dependencies to the other controller.
type Controller struct {
FileRepo *repo.FileRepository
StoreBonusRepo *storagebonus.Repository
UserCache *cache.UserCache
}
func (c *Controller) GetActiveStorageBonus(ctx context.Context, userID int64) (*bonus.ActiveStorageBonus, error) {
// Check if the value is present in the cache
if bonus, ok := c.UserCache.GetBonus(userID); ok {
// Cache hit, update the cache asynchronously
go func() {
_, _ = c.getAndCacheActiveStorageBonus(ctx, userID)
}()
return bonus, nil
}
return c.getAndCacheActiveStorageBonus(ctx, userID)
}
func (c *Controller) getAndCacheActiveStorageBonus(ctx context.Context, userID int64) (*bonus.ActiveStorageBonus, error) {
bonus, err := c.StoreBonusRepo.GetActiveStorageBonuses(ctx, userID)
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
c.UserCache.SetBonus(userID, bonus)
return bonus, nil
}