hudly/mcfetch/cache.go

155 lines
3.1 KiB
Go
Raw Normal View History

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