Skip to content

Commit

Permalink
fix: added fixed for PR feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
codebender37 committed Dec 2, 2024
1 parent 589c7a3 commit c5a1a41
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 235 deletions.
71 changes: 58 additions & 13 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,62 @@ type Cache struct {
Redis redis.Client
}

// CacheableData interface for any data that can be cached
type CacheableData interface {
GetCacheKey() string
GetExpiration() time.Duration
}

var (
instance *Cache
once sync.Once
mu sync.Mutex
)

// CacheKey type for type-safe cache keys
type CacheKey string

const (
// Task cache keys
TaskById CacheKey = "task" // Single task by ID
TasksByWorker CacheKey = "task:worker" // List of tasks by worker

// Task Result cache keys
TaskResultByTaskAndWorker CacheKey = "tr:task:worker" // Task result by task ID and worker ID
TaskResultByWorker CacheKey = "tr:worker" // Task results by worker ID

// Worker cache keys
WorkerByWallet CacheKey = "worker:wallet" // Worker by wallet address
WorkerCount CacheKey = "worker:count" // Total worker count

// Subscription cache keys
SubByHotkey CacheKey = "sub:hotkey" // Subscription by hotkey
SubByKey CacheKey = "sub:key" // Subscription by key
)

// CacheConfig defines cache keys and their expiration times
var CacheConfig = map[CacheKey]time.Duration{
TaskById: 5 * time.Minute,
TasksByWorker: 2 * time.Minute,
TaskResultByTaskAndWorker: 10 * time.Minute,
TaskResultByWorker: 10 * time.Minute,
WorkerByWallet: 5 * time.Minute,
WorkerCount: 1 * time.Minute,
SubByHotkey: 5 * time.Minute,
SubByKey: 5 * time.Minute,
}

// GetCacheExpiration returns the expiration time for a given cache key
func GetCacheExpiration(key CacheKey) time.Duration {
if duration, exists := CacheConfig[key]; exists {
return duration
}
return 5 * time.Minute // default expiration
}

// BuildCacheKey builds a cache key with the given prefix and components
func BuildCacheKey(prefix CacheKey, components ...string) string {
key := string(prefix)
for _, component := range components {
key += ":" + component
}
return key
}

func GetCacheInstance() *Cache {
once.Do(func() {
mu.Lock()
Expand Down Expand Up @@ -118,27 +162,28 @@ func (c *Cache) Shutdown() {
}

// GetCache retrieves and unmarshals data from cache using MessagePack
func (c *Cache) GetCache(data CacheableData, value interface{}) error {
cachedData, err := c.Get(data.GetCacheKey())
func (c *Cache) GetCache(key string, value interface{}) error {
cachedData, err := c.Get(key)
if err != nil || cachedData == "" {
return fmt.Errorf("cache miss for key: %s", data.GetCacheKey())
return fmt.Errorf("cache miss for key: %s", key)
}

log.Info().Msgf("Cache hit for key: %s", data.GetCacheKey())
log.Info().Msgf("Cache hit for key: %s", key)
return msgpack.Unmarshal([]byte(cachedData), value)
}

// SetCache marshals and stores data in cache using MessagePack
func (c *Cache) SetCache(data CacheableData, value interface{}) error {
func (c *Cache) SetCache(key string, value interface{}) error {
dataBytes, err := msgpack.Marshal(value)
if err != nil {
return fmt.Errorf("failed to marshal data: %w", err)
}

if err := c.SetWithExpire(data.GetCacheKey(), dataBytes, data.GetExpiration()); err != nil {
expiration := GetCacheExpiration(CacheKey(key))
if err := c.SetWithExpire(key, dataBytes, expiration); err != nil {
return fmt.Errorf("failed to set cache: %w", err)
}

log.Info().Msgf("Successfully set cache for key: %s", data.GetCacheKey())
log.Info().Msgf("Successfully set cache for key: %s", key)
return nil
}
54 changes: 6 additions & 48 deletions pkg/orm/dojo_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"strconv"
"time"

"dojo-api/db"

Expand All @@ -14,46 +13,6 @@ import (
"github.com/rs/zerolog/log"
)

type DojoWorkerCacheKey string

const (
WorkerByWalletCacheKey DojoWorkerCacheKey = "worker_by_wallet" // Short key for worker by wallet
WorkerCountCacheKey DojoWorkerCacheKey = "worker_count" // Short key for worker count
)

type DojoWorkerCache struct {
key DojoWorkerCacheKey
walletAddress string
}

func NewDojoWorkerCache(key DojoWorkerCacheKey) *DojoWorkerCache {
return &DojoWorkerCache{
key: key,
}
}

func (dc *DojoWorkerCache) GetCacheKey() string {
switch dc.key {
case WorkerByWalletCacheKey:
return fmt.Sprintf("%s:%s", dc.key, dc.walletAddress)
case WorkerCountCacheKey:
return string(dc.key)
default:
return fmt.Sprintf("dw:%s", dc.walletAddress)
}
}

func (dc *DojoWorkerCache) GetExpiration() time.Duration {
switch dc.key {
case WorkerByWalletCacheKey:
return 5 * time.Minute
case WorkerCountCacheKey:
return 1 * time.Minute
default:
return 3 * time.Minute
}
}

type DojoWorkerORM struct {
dbClient *db.PrismaClient
clientWrapper *PrismaClientWrapper
Expand All @@ -77,14 +36,13 @@ func (s *DojoWorkerORM) CreateDojoWorker(walletAddress string, chainId string) (
}

func (s *DojoWorkerORM) GetDojoWorkerByWalletAddress(walletAddress string) (*db.DojoWorkerModel, error) {
workerCache := NewDojoWorkerCache(WorkerByWalletCacheKey)
workerCache.walletAddress = walletAddress
cacheKey := cache.BuildCacheKey(cache.WorkerByWallet, walletAddress)

var worker *db.DojoWorkerModel
cache := cache.GetCacheInstance()

// Try to get from cache first
if err := cache.GetCache(workerCache, &worker); err == nil {
if err := cache.GetCache(cacheKey, &worker); err == nil {
return worker, nil
}

Expand All @@ -105,20 +63,20 @@ func (s *DojoWorkerORM) GetDojoWorkerByWalletAddress(walletAddress string) (*db.
}

// Store in cache
if err := cache.SetCache(workerCache, worker); err != nil {
if err := cache.SetCache(cacheKey, worker); err != nil {
log.Warn().Err(err).Msg("Failed to set worker cache")
}

return worker, nil
}

func (s *DojoWorkerORM) GetDojoWorkers() (int, error) {
workerCache := NewDojoWorkerCache(WorkerCountCacheKey)
cacheKey := cache.BuildCacheKey(cache.WorkerCount, "")
var count int
cache := cache.GetCacheInstance()

// Try to get from cache first
if err := cache.GetCache(workerCache, &count); err == nil {
if err := cache.GetCache(cacheKey, &count); err == nil {
return count, nil
}

Expand Down Expand Up @@ -148,7 +106,7 @@ func (s *DojoWorkerORM) GetDojoWorkers() (int, error) {
}

// Store in cache
if err := cache.SetCache(workerCache, count); err != nil {
if err := cache.SetCache(cacheKey, count); err != nil {
log.Warn().Err(err).Msg("Failed to set worker count cache")
}

Expand Down
54 changes: 8 additions & 46 deletions pkg/orm/subscriptionKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package orm
import (
"context"
"errors"
"fmt"
"time"

"dojo-api/db"
"dojo-api/pkg/cache"
Expand All @@ -17,54 +15,19 @@ type SubscriptionKeyORM struct {
clientWrapper *PrismaClientWrapper
}

type SubscriptionKeyCacheKey string

const (
SubKeysByHotkeyCacheKey SubscriptionKeyCacheKey = "sk_by_hotkey"
SubKeyByKeyCacheKey SubscriptionKeyCacheKey = "sk_by_key"
)

type SubscriptionKeyCache struct {
key SubscriptionKeyCacheKey
hotkey string
subKey string
}

func NewSubscriptionKeyCache(key SubscriptionKeyCacheKey) *SubscriptionKeyCache {
return &SubscriptionKeyCache{
key: key,
}
}

func (sc *SubscriptionKeyCache) GetCacheKey() string {
switch sc.key {
case SubKeysByHotkeyCacheKey:
return fmt.Sprintf("%s:%s", sc.key, sc.hotkey)
case SubKeyByKeyCacheKey:
return fmt.Sprintf("%s:%s", sc.key, sc.subKey)
default:
return fmt.Sprintf("sk:%s", sc.subKey)
}
}

func (sc *SubscriptionKeyCache) GetExpiration() time.Duration {
return 5 * time.Minute
}

func NewSubscriptionKeyORM() *SubscriptionKeyORM {
clientWrapper := GetPrismaClient()
return &SubscriptionKeyORM{dbClient: clientWrapper.Client, clientWrapper: clientWrapper}
}

func (a *SubscriptionKeyORM) GetSubscriptionKeysByMinerHotkey(hotkey string) ([]db.SubscriptionKeyModel, error) {
subCache := NewSubscriptionKeyCache(SubKeysByHotkeyCacheKey)
subCache.hotkey = hotkey
cacheKey := cache.BuildCacheKey(cache.SubByHotkey, hotkey)

var subKeys []db.SubscriptionKeyModel
cache := cache.GetCacheInstance()

// Try to get from cache first
if err := cache.GetCache(subCache, &subKeys); err == nil {
if err := cache.GetCache(cacheKey, &subKeys); err == nil {
return subKeys, nil
}

Expand All @@ -88,7 +51,7 @@ func (a *SubscriptionKeyORM) GetSubscriptionKeysByMinerHotkey(hotkey string) ([]
}

// Cache the result
if err := cache.SetCache(subCache, apiKeys); err != nil {
if err := cache.SetCache(cacheKey, apiKeys); err != nil {
log.Error().Err(err).Msgf("Error caching subscription keys")
}

Expand All @@ -107,7 +70,7 @@ func (a *SubscriptionKeyORM) CreateSubscriptionKeyByHotkey(hotkey string, subscr
return nil, err
}

createdApiKey, err := a.dbClient.SubscriptionKey.CreateOne(
createdSubKey, err := a.dbClient.SubscriptionKey.CreateOne(
db.SubscriptionKey.Key.Set(subscriptionKey),
db.SubscriptionKey.MinerUser.Link(
db.MinerUser.ID.Equals(minerUser.ID),
Expand All @@ -118,7 +81,7 @@ func (a *SubscriptionKeyORM) CreateSubscriptionKeyByHotkey(hotkey string, subscr
log.Error().Err(err).Msgf("Error creating subscription key")
return nil, err
}
return createdApiKey, nil
return createdSubKey, nil
}

func (a *SubscriptionKeyORM) DisableSubscriptionKeyByHotkey(hotkey string, subscriptionKey string) (*db.SubscriptionKeyModel, error) {
Expand All @@ -139,14 +102,13 @@ func (a *SubscriptionKeyORM) DisableSubscriptionKeyByHotkey(hotkey string, subsc
}

func (a *SubscriptionKeyORM) GetSubscriptionByKey(subScriptionKey string) (*db.SubscriptionKeyModel, error) {
subCache := NewSubscriptionKeyCache(SubKeyByKeyCacheKey)
subCache.subKey = subScriptionKey
cacheKey := cache.BuildCacheKey(cache.SubByKey, subScriptionKey)

var foundSubscriptionKey *db.SubscriptionKeyModel
cache := cache.GetCacheInstance()

// Try to get from cache first
if err := cache.GetCache(subCache, &foundSubscriptionKey); err == nil {
if err := cache.GetCache(cacheKey, &foundSubscriptionKey); err == nil {
return foundSubscriptionKey, nil
}
a.clientWrapper.BeforeQuery()
Expand All @@ -169,7 +131,7 @@ func (a *SubscriptionKeyORM) GetSubscriptionByKey(subScriptionKey string) (*db.S
}

// Cache the result
if err := cache.SetCache(subCache, foundSubscriptionKey); err != nil {
if err := cache.SetCache(cacheKey, foundSubscriptionKey); err != nil {
log.Error().Err(err).Msgf("Error caching subscription key")
}

Expand Down
Loading

0 comments on commit c5a1a41

Please sign in to comment.