From ecad53b6b1dacd4955bdc3405c88df4dec525932 Mon Sep 17 00:00:00 2001 From: illyum Date: Sat, 26 Oct 2024 18:46:58 -0600 Subject: [PATCH] Change from raw json to player struct --- mcfetch/async_player_fetcher.go | 17 ++++++----- mcfetch/cache.go | 52 ++++++++++++++++++--------------- mcfetch/player_fetcher.go | 25 +++++++++------- 3 files changed, 54 insertions(+), 40 deletions(-) diff --git a/mcfetch/async_player_fetcher.go b/mcfetch/async_player_fetcher.go index 8c483d2..70a2a6f 100644 --- a/mcfetch/async_player_fetcher.go +++ b/mcfetch/async_player_fetcher.go @@ -28,24 +28,27 @@ func NewAsyncPlayerFetcher(playerName string, cache ICache, retries int, retryDe } } -// FetchPlayerData fetches the player data asynchronously -func (pf *AsyncPlayerFetcher) FetchPlayerData(resultChan chan map[string]interface{}, errorChan chan error) { +// FetchPlayerData fetches the player data asynchronously using channels +func (pf *AsyncPlayerFetcher) FetchPlayerData(resultChan chan *FetchedPlayerResult, errorChan chan error) { go func() { cachedData, found := pf.cache.Get(pf.playerName) if found { - resultChan <- cachedData + resultChan <- (*FetchedPlayerResult)(cachedData) return } - var data map[string]interface{} + // If not in cache, make request to Mojang API + var player FetchedPlayerResult for i := 0; i < pf.retries; i++ { resp, err := pf.makeRequest(pf.playerName) if err == nil { defer resp.Body.Close() - if err := json.NewDecoder(resp.Body).Decode(&data); err == nil { - pf.cache.Set(pf.playerName, data) + // Decode the response into FetchedPlayerResult + if err := json.NewDecoder(resp.Body).Decode(&player); err == nil { + // Store the result in the cache and return the data + pf.cache.Set(pf.playerName, (*CacheResult)(&player)) pf.cache.Sync() - resultChan <- data + resultChan <- &player return } } diff --git a/mcfetch/cache.go b/mcfetch/cache.go index 7d52a7f..c63be5a 100644 --- a/mcfetch/cache.go +++ b/mcfetch/cache.go @@ -8,11 +8,16 @@ import ( "sync" ) +type CacheResult struct { + UUID string `json:"id"` + Name string `json:"name"` +} + type ICache interface { Init() Load() - Get(key string) (map[string]interface{}, bool) - Set(key string, data map[string]interface{}) + Get(key string) (*CacheResult, bool) + Set(key string, data *CacheResult) Save() Sync() Purge() @@ -21,31 +26,31 @@ type ICache interface { // MemoryCache implementation type MemoryCache struct { - cache map[string]interface{} + cache map[string]*CacheResult mu sync.RWMutex } // Init initializes the cache (no-op for MemoryCache) func (c *MemoryCache) Init() { - c.cache = make(map[string]interface{}) + c.cache = make(map[string]*CacheResult) } // Load loads the cache (no-op for MemoryCache) func (c *MemoryCache) Load() {} // Get retrieves an item from the cache -func (c *MemoryCache) Get(key string) (map[string]interface{}, bool) { +func (c *MemoryCache) Get(key string) (*CacheResult, bool) { c.mu.RLock() defer c.mu.RUnlock() value, found := c.cache[key] if !found { return nil, false } - return value.(map[string]interface{}), true + return value, true } // Set stores an item in the cache -func (c *MemoryCache) Set(key string, data map[string]interface{}) { +func (c *MemoryCache) Set(key string, data *CacheResult) { c.mu.Lock() defer c.mu.Unlock() c.cache[key] = data @@ -64,19 +69,19 @@ func (c *MemoryCache) Purge() {} func (c *MemoryCache) Clear() { c.mu.Lock() defer c.mu.Unlock() - c.cache = make(map[string]interface{}) + c.cache = make(map[string]*CacheResult) } // JsonFileCache implementation type JsonFileCache struct { filename string - cache map[string]interface{} + cache map[CacheResult]interface{} mu sync.RWMutex } // Init initializes the cache func (c *JsonFileCache) Init() { - c.cache = make(map[string]interface{}) + c.cache = make(map[CacheResult]interface{}) } // Load loads the cache from a JSON file @@ -104,21 +109,22 @@ func (c *JsonFileCache) Load() { } // Get retrieves an item from the cache -func (c *JsonFileCache) Get(key string) (map[string]interface{}, bool) { - c.mu.RLock() - defer c.mu.RUnlock() - value, found := c.cache[key] - if !found { - return nil, false - } - return value.(map[string]interface{}), true +func (c *JsonFileCache) Get(key string) (*CacheResult, bool) { + //c.mu.RLock() + //defer c.mu.RUnlock() + //value, found := c.cache[key] + //if !found { + // return nil, false + //} + //return value, true + return nil, false } // Set stores an item in the cache -func (c *JsonFileCache) Set(key string, data map[string]interface{}) { - c.mu.Lock() - defer c.mu.Unlock() - c.cache[key] = data +func (c *JsonFileCache) Set(key string, data *CacheResult) { + //c.mu.Lock() + //defer c.mu.Unlock() + //c.cache[key] = data } // Save saves the cache to a JSON file @@ -150,5 +156,5 @@ func (c *JsonFileCache) Purge() {} func (c *JsonFileCache) Clear() { c.mu.Lock() defer c.mu.Unlock() - c.cache = make(map[string]interface{}) + c.cache = make(map[CacheResult]interface{}) } diff --git a/mcfetch/player_fetcher.go b/mcfetch/player_fetcher.go index a736df8..0b8df15 100644 --- a/mcfetch/player_fetcher.go +++ b/mcfetch/player_fetcher.go @@ -16,6 +16,11 @@ type PlayerFetcher struct { cache ICache } +type FetchedPlayerResult struct { + UUID string `json:"id"` + Name string `json:"name"` +} + // NewPlayerFetcher creates a new PlayerFetcher with an abstract cache (ICache) func NewPlayerFetcher(playerName string, cache ICache, retries int, retryDelay time.Duration, timeout time.Duration) *PlayerFetcher { cache.Init() @@ -29,21 +34,21 @@ func NewPlayerFetcher(playerName string, cache ICache, retries int, retryDelay t } // FetchPlayerData fetches the player data synchronously -func (pf *PlayerFetcher) FetchPlayerData() (map[string]interface{}, error) { - cachedData, found := pf.cache.Get(pf.playerName) - if found { - return cachedData, nil - } +func (pf *PlayerFetcher) FetchPlayerData() (*FetchedPlayerResult, error) { + //cachedData, found := pf.cache.Get(pf.playerName) + //if found { + // return &FetchedPlayerResult{}, nil + //} - var data map[string]interface{} + var player FetchedPlayerResult for i := 0; i < pf.retries; i++ { resp, err := pf.makeRequest(pf.playerName) if err == nil { defer resp.Body.Close() - if err := json.NewDecoder(resp.Body).Decode(&data); err == nil { - pf.cache.Set(pf.playerName, data) - pf.cache.Sync() - return data, nil + if err := json.NewDecoder(resp.Body).Decode(&player); err == nil { + // pf.cache.Set(pf.playerName, player) + // pf.cache.Sync() + return &player, nil } } time.Sleep(pf.retryDelay)