-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use short-lived in-memory cache to cache previewed feeds (#31)
- Loading branch information
Showing
7 changed files
with
155 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package cache | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/guyfedwards/nom/internal/rss" | ||
) | ||
|
||
var DefaultPath = filepath.Join(os.TempDir(), "nom") | ||
|
||
type FileCache struct { | ||
expiry time.Duration | ||
path string | ||
content CacheContent | ||
} | ||
|
||
// Write writes content to a file at the location specified in the cache | ||
func (c *FileCache) Write(key string, content rss.RSS) error { | ||
err := createCacheIfNotExists(c.path) | ||
if err != nil { | ||
return fmt.Errorf("createcache: %w", err) | ||
} | ||
|
||
data, err := os.ReadFile(filepath.Join(c.path, "cache.json")) | ||
if err != nil { | ||
return fmt.Errorf("cache Write: %w", err) | ||
} | ||
|
||
var cc CacheContent | ||
|
||
err = json.Unmarshal(data, &cc) | ||
if err != nil { | ||
return fmt.Errorf("cache Write json unmarshal: %w", err) | ||
} | ||
|
||
cc[key] = content | ||
|
||
str, err := json.Marshal(cc) | ||
if err != nil { | ||
return fmt.Errorf("cache write marshal json: %w", err) | ||
} | ||
|
||
err = os.WriteFile(filepath.Join(c.path, "cache.json"), str, 0655) | ||
if err != nil { | ||
return fmt.Errorf("cache Write: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Read reads from the cache, returning a ErrFileCacheMiss if nothing found or | ||
// if the cache is older than the expiry | ||
func (c *FileCache) Read(key string) (rss.RSS, error) { | ||
err := createCacheIfNotExists(c.path) | ||
if err != nil { | ||
return rss.RSS{}, fmt.Errorf("cache read: %w", err) | ||
} | ||
|
||
data, err := os.ReadFile(filepath.Join(c.path, "cache.json")) | ||
if err != nil { | ||
return rss.RSS{}, fmt.Errorf("cache read file: %w", err) | ||
} | ||
|
||
var cc CacheContent | ||
|
||
err = json.Unmarshal(data, &cc) | ||
if err != nil { | ||
return rss.RSS{}, fmt.Errorf("cache read unmarshal: %w", err) | ||
} | ||
|
||
if _, ok := cc[key]; !ok { | ||
return rss.RSS{}, ErrCacheMiss | ||
} | ||
|
||
return cc[key], nil | ||
} | ||
|
||
func createCacheIfNotExists(path string) error { | ||
cachePath := filepath.Join(path, "cache.json") | ||
info, _ := os.Stat(cachePath) | ||
if info != nil { | ||
return nil | ||
} | ||
|
||
fmt.Println("No existing cache found, creating") | ||
|
||
err := os.MkdirAll(path, 0755) | ||
if err != nil { | ||
return fmt.Errorf("createDirIfNotExists: %w", err) | ||
} | ||
|
||
var cc = make(CacheContent) | ||
|
||
str, err := json.Marshal(cc) | ||
if err != nil { | ||
return fmt.Errorf("createDirIfNotExists: %w", err) | ||
} | ||
|
||
err = os.WriteFile(cachePath, []byte(str), 0655) | ||
if err != nil { | ||
return fmt.Errorf("createDirIfNotExists: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cache | ||
|
||
import "github.com/guyfedwards/nom/internal/rss" | ||
|
||
type MemoryCache struct { | ||
content CacheContent | ||
} | ||
|
||
func (c *MemoryCache) Write(key string, content rss.RSS) error { | ||
if c.content == nil { | ||
c.content = make(CacheContent) | ||
} | ||
c.content[key] = content | ||
return nil | ||
} | ||
|
||
func (c *MemoryCache) Read(key string) (rss.RSS, error) { | ||
if _, ok := c.content[key]; !ok { | ||
return rss.RSS{}, ErrCacheMiss | ||
} | ||
|
||
return c.content[key], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters