Skip to content

Commit

Permalink
feat: improve caching
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelroses committed Apr 20, 2024
1 parent 59c071e commit 74d4e46
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 7 deletions.
106 changes: 106 additions & 0 deletions lib/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package cache

import (
"encoding/json"
"os"
"path/filepath"
"sync"
"time"

"github.com/isabelroses/izrss/lib"
)

// CacheEntry represents a cached item with its expiration time
type CacheEntry struct {
Value lib.Post // Store Post object
Expiration time.Time // Expiration time
}

// Cache represents a cache with a map to store cached items
type Cache struct {
data map[string]CacheEntry
dir string
mu sync.RWMutex
}

// NewCache creates a new instance of Cache
func NewCache(dir string) *Cache {
return &Cache{
data: make(map[string]CacheEntry),
dir: dir,
}
}

// Get retrieves a value from the cache by key
func (c *Cache) Get(key string) (lib.Post, bool) {
c.mu.RLock()
defer c.mu.RUnlock()

entry, found := c.data[key]
if !found {
return lib.Post{}, false
}

// Check if the entry has expired
if time.Now().After(entry.Expiration) {
// If expired, delete the entry from the cache
c.mu.Lock()
delete(c.data, key)
c.mu.Unlock()
return lib.Post{}, false
}

return entry.Value, true
}

// Set adds or updates a value in the cache with a specified expiration time
func (c *Cache) Set(key string, value lib.Post, expiration time.Duration) {
c.mu.Lock()
defer c.mu.Unlock()

c.data[key] = CacheEntry{
Value: value,
Expiration: time.Now().Add(expiration),
}

// Save the cache entry to a file
c.saveToFile(key, value)
}

// saveToFile saves a cache entry to a file in the cache directory
func (c *Cache) saveToFile(key string, value lib.Post) error {
data, err := json.Marshal(value)
if err != nil {
return err
}

filename := filepath.Join(c.dir, key+".json")
return os.WriteFile(filename, data, 0644)
}

// LoadCache loads cache entries from files in the cache directory
func (c *Cache) LoadCache() error {
files, err := os.ReadDir(c.dir)
if err != nil {
return err
}

for _, file := range files {
if !file.IsDir() {
filename := filepath.Join(c.dir, file.Name())
data, err := os.ReadFile(filename)
if err != nil {
return err
}

var post lib.Post
if err := json.Unmarshal(data, &post); err != nil {
return err
}

c.Set(post.UUID, post, time.Until(post.Expiration))
}
}

return nil
}
15 changes: 8 additions & 7 deletions lib/feeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import "sort"

// Post represents a single post in a feed
type Post struct {
UUID string `json:"uuid"`
Title string `json:"-"`
Content string `json:"-"`
Link string `json:"-"`
Date string `json:"-"`
ID int `json:"-"`
Read bool `json:"read"`
UUID string `json:"uuid"`
Title string `json:"-"`
Expiration string `json:"-"`
Content string `json:"-"`
Link string `json:"-"`
Date string `json:"-"`
ID int `json:"-"`
Read bool `json:"read"`
}

// Feed represents a single feed
Expand Down

0 comments on commit 74d4e46

Please # to comment.