package mcfetch import ( "encoding/json" "io" "log" "os" "sync" ) type CacheResult struct { UUID string `json:"id"` Name string `json:"name"` } type ICache interface { Init() Load() Get(key string) (*CacheResult, bool) Set(key string, data *CacheResult) Save() Sync() Purge() Clear() } // MemoryCache implementation type MemoryCache struct { cache map[string]*CacheResult mu sync.RWMutex } // Init initializes the cache (no-op for MemoryCache) func (c *MemoryCache) Init() { 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) (*CacheResult, bool) { c.mu.RLock() defer c.mu.RUnlock() value, found := c.cache[key] if !found { return nil, false } return value, true } // Set stores an item in the cache func (c *MemoryCache) Set(key string, data *CacheResult) { c.mu.Lock() defer c.mu.Unlock() c.cache[key] = data } // Save saves the cache (no-op for MemoryCache) func (c *MemoryCache) Save() {} // Sync syncs the cache (no-op for MemoryCache) func (c *MemoryCache) Sync() {} // Purge will be implemented later func (c *MemoryCache) Purge() {} // Clear clears the cache func (c *MemoryCache) Clear() { c.mu.Lock() defer c.mu.Unlock() c.cache = make(map[string]*CacheResult) } // JsonFileCache implementation type JsonFileCache struct { filename string cache map[CacheResult]interface{} mu sync.RWMutex } // Init initializes the cache func (c *JsonFileCache) Init() { c.cache = make(map[CacheResult]interface{}) } // Load loads the cache from a JSON file func (c *JsonFileCache) Load() { c.mu.Lock() defer c.mu.Unlock() file, err := os.Open(c.filename) if err != nil { log.Println("Error opening file:", err) return } defer file.Close() byteValue, err := io.ReadAll(file) if err != nil { log.Println("Error reading file:", err) return } err = json.Unmarshal(byteValue, &c.cache) if err != nil { log.Println("Error unmarshalling JSON:", err) } } // Get retrieves an item from the cache 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 *CacheResult) { //c.mu.Lock() //defer c.mu.Unlock() //c.cache[key] = data } // Save saves the cache to a JSON file func (c *JsonFileCache) Save() { c.mu.Lock() defer c.mu.Unlock() byteValue, err := json.MarshalIndent(c.cache, "", " ") if err != nil { log.Println("Error marshalling JSON:", err) return } err = os.WriteFile(c.filename, byteValue, 0644) if err != nil { log.Println("Error writing file:", err) } } // Sync is the same as Save for the JsonFileCache func (c *JsonFileCache) Sync() { c.Save() } // Purge will be implemented later func (c *JsonFileCache) Purge() {} // Clear clears the cache func (c *JsonFileCache) Clear() { c.mu.Lock() defer c.mu.Unlock() c.cache = make(map[CacheResult]interface{}) }