hudly/mcfetch/cache.go

161 lines
3.1 KiB
Go
Raw Permalink Normal View History

2024-10-22 18:49:14 -06:00
package mcfetch
import (
"encoding/json"
"io"
"log"
"os"
"sync"
)
2024-10-26 18:46:58 -06:00
type CacheResult struct {
UUID string `json:"id"`
Name string `json:"name"`
}
2024-10-22 18:49:14 -06:00
type ICache interface {
Init()
Load()
2024-10-26 18:46:58 -06:00
Get(key string) (*CacheResult, bool)
Set(key string, data *CacheResult)
2024-10-22 18:49:14 -06:00
Save()
Sync()
Purge()
Clear()
}
// MemoryCache implementation
type MemoryCache struct {
2024-10-26 18:46:58 -06:00
cache map[string]*CacheResult
2024-10-22 18:49:14 -06:00
mu sync.RWMutex
}
// Init initializes the cache (no-op for MemoryCache)
func (c *MemoryCache) Init() {
2024-10-26 18:46:58 -06:00
c.cache = make(map[string]*CacheResult)
2024-10-22 18:49:14 -06:00
}
// Load loads the cache (no-op for MemoryCache)
func (c *MemoryCache) Load() {}
// Get retrieves an item from the cache
2024-10-26 18:46:58 -06:00
func (c *MemoryCache) Get(key string) (*CacheResult, bool) {
2024-10-22 18:49:14 -06:00
c.mu.RLock()
defer c.mu.RUnlock()
value, found := c.cache[key]
if !found {
return nil, false
}
2024-10-26 18:46:58 -06:00
return value, true
2024-10-22 18:49:14 -06:00
}
// Set stores an item in the cache
2024-10-26 18:46:58 -06:00
func (c *MemoryCache) Set(key string, data *CacheResult) {
2024-10-22 18:49:14 -06:00
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()
2024-10-26 18:46:58 -06:00
c.cache = make(map[string]*CacheResult)
2024-10-22 18:49:14 -06:00
}
// JsonFileCache implementation
type JsonFileCache struct {
filename string
2024-10-26 18:46:58 -06:00
cache map[CacheResult]interface{}
2024-10-22 18:49:14 -06:00
mu sync.RWMutex
}
// Init initializes the cache
func (c *JsonFileCache) Init() {
2024-10-26 18:46:58 -06:00
c.cache = make(map[CacheResult]interface{})
2024-10-22 18:49:14 -06:00
}
// 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
2024-10-26 18:46:58 -06:00
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
2024-10-22 18:49:14 -06:00
}
// Set stores an item in the cache
2024-10-26 18:46:58 -06:00
func (c *JsonFileCache) Set(key string, data *CacheResult) {
//c.mu.Lock()
//defer c.mu.Unlock()
//c.cache[key] = data
2024-10-22 18:49:14 -06:00
}
// 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()
2024-10-26 18:46:58 -06:00
c.cache = make(map[CacheResult]interface{})
2024-10-22 18:49:14 -06:00
}