Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: add cache in db layer #59

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ func getCallerIP(c *gin.Context) string {
// TODO - Need to check if this is the correct way without getting spoofing
if runtimeEnv := utils.LoadDotEnv("RUNTIME_ENV"); runtimeEnv == "aws" {
callerIp := c.Request.Header.Get("X-Original-Forwarded-For")
log.Info().Msgf("Got caller IP from X-Original-Forwarded-For header: %s", callerIp)
log.Trace().Msgf("Got caller IP from X-Original-Forwarded-For header: %s", callerIp)
return callerIp
}
callerIp := c.ClientIP()
log.Info().Msgf("Got caller IP from ClientIP: %s", callerIp)
log.Trace().Msgf("Got caller IP from ClientIP: %s", callerIp)
return callerIp
}

Expand Down
33 changes: 33 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"os"
"sync"
Expand All @@ -25,6 +26,12 @@ type Cache struct {
Redis redis.Client
}

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

jarvis8x7b marked this conversation as resolved.
Show resolved Hide resolved
var (
instance *Cache
once sync.Once
Expand Down Expand Up @@ -109,3 +116,29 @@ func (c *Cache) Shutdown() {
c.Redis.Close()
log.Info().Msg("Successfully closed Redis connection")
}

// GetCache attempts to retrieve and unmarshal data from cache
func (c *Cache) GetCache(data CacheableData, value interface{}) error {
jarvis8x7b marked this conversation as resolved.
Show resolved Hide resolved
cachedData, err := c.Get(data.GetCacheKey())
if err != nil || cachedData == "" {
return fmt.Errorf("cache miss for key: %s", data.GetCacheKey())
}

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

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

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

log.Info().Msgf("Successfully set cache for key: %s", data.GetCacheKey())
return nil
}
83 changes: 80 additions & 3 deletions pkg/orm/dojo_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,58 @@ package orm

import (
"context"
"dojo-api/db"
"errors"
"fmt"
"strconv"
"time"

"dojo-api/db"

"dojo-api/pkg/cache"

"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 @@ -33,6 +77,18 @@ func (s *DojoWorkerORM) CreateDojoWorker(walletAddress string, chainId string) (
}

func (s *DojoWorkerORM) GetDojoWorkerByWalletAddress(walletAddress string) (*db.DojoWorkerModel, error) {
workerCache := NewDojoWorkerCache(WorkerByWalletCacheKey)
workerCache.walletAddress = walletAddress
jarvis8x7b marked this conversation as resolved.
Show resolved Hide resolved

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

// Try to get from cache first
if err := cache.GetCache(workerCache, &worker); err == nil {
jarvis8x7b marked this conversation as resolved.
Show resolved Hide resolved
return worker, nil
}

// Cache miss, fetch from database
s.clientWrapper.BeforeQuery()
defer s.clientWrapper.AfterQuery()

Expand All @@ -47,10 +103,26 @@ func (s *DojoWorkerORM) GetDojoWorkerByWalletAddress(walletAddress string) (*db.
}
return nil, err
}

// Store in cache
if err := cache.SetCache(workerCache, 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)
var count int
cache := cache.GetCacheInstance()

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

// Cache miss, fetch from database
s.clientWrapper.BeforeQuery()
defer s.clientWrapper.AfterQuery()

Expand All @@ -70,10 +142,15 @@ func (s *DojoWorkerORM) GetDojoWorkers() (int, error) {
}

workerCountStr := string(result[0].Count)
workerCountInt, err := strconv.Atoi(workerCountStr)
count, err = strconv.Atoi(workerCountStr)
if err != nil {
return 0, err
}

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

return count, nil
}
72 changes: 69 additions & 3 deletions pkg/orm/subscriptionKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package orm

import (
"context"
"dojo-api/db"
"errors"
"fmt"
"time"

"dojo-api/db"
"dojo-api/pkg/cache"

"github.com/rs/zerolog/log"
)
Expand All @@ -13,14 +17,56 @@ 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) {
a.clientWrapper.BeforeQuery()
defer a.clientWrapper.AfterQuery()
subCache := NewSubscriptionKeyCache(SubKeysByHotkeyCacheKey)
subCache.hotkey = hotkey

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

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

ctx := context.Background()

Expand All @@ -41,6 +87,11 @@ func (a *SubscriptionKeyORM) GetSubscriptionKeysByMinerHotkey(hotkey string) ([]
return nil, err
}

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

return apiKeys, nil
codebender37 marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -88,6 +139,16 @@ func (a *SubscriptionKeyORM) DisableSubscriptionKeyByHotkey(hotkey string, subsc
}

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

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

// Try to get from cache first
if err := cache.GetCache(subCache, &foundSubscriptionKey); err == nil {
return foundSubscriptionKey, nil
}
a.clientWrapper.BeforeQuery()
defer a.clientWrapper.AfterQuery()

Expand All @@ -107,5 +168,10 @@ func (a *SubscriptionKeyORM) GetSubscriptionByKey(subScriptionKey string) (*db.S
return nil, err
}

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

return foundSubscriptionKey, nil
}
Loading