tests: cache
This commit is contained in:
parent
92732f9682
commit
597feb7b54
8 changed files with 822 additions and 32 deletions
86
cache/file.go
vendored
86
cache/file.go
vendored
|
@ -1,6 +1,7 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
@ -17,7 +18,23 @@ type FileCache struct {
|
|||
}
|
||||
|
||||
func (c *FileCache) Get(key string) (result any, err error) {
|
||||
path := filepath.Join(c.path, key)
|
||||
path := c.getPathForItem(key)
|
||||
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
return result, model.ErrCacheKeyDontExist
|
||||
}
|
||||
|
||||
metadata, err := c.getMetadata(key)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("error getting metadata: %s", err)
|
||||
}
|
||||
|
||||
if metadata.TTL != nil && metadata.TTL.Before(time.Now()) {
|
||||
if err := c.Delete(key); err != nil {
|
||||
return result, fmt.Errorf("error deleting cache key: %s", err)
|
||||
}
|
||||
return result, model.ErrCacheKeyDontExist
|
||||
}
|
||||
|
||||
contents, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
|
@ -30,23 +47,20 @@ func (c *FileCache) Get(key string) (result any, err error) {
|
|||
return contents, nil
|
||||
}
|
||||
|
||||
func (c *FileCache) GetWithExpiry(key string, expiration time.Duration) (result any, err error) {
|
||||
path := filepath.Join(c.path, key)
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return result, model.ErrCacheKeyDontExist
|
||||
func (c *FileCache) Set(key string, value any, opts ...model.CacheOption) error {
|
||||
path := c.getPathForItem(key)
|
||||
|
||||
metadata := model.CacheItem{
|
||||
Key: key,
|
||||
}
|
||||
|
||||
if info.ModTime().Add(expiration).Before(time.Now()) {
|
||||
c.Delete(key)
|
||||
return result, model.ErrCacheKeyDontExist
|
||||
for _, opt := range opts {
|
||||
opt(&metadata)
|
||||
}
|
||||
|
||||
return c.Get(key)
|
||||
}
|
||||
|
||||
func (c *FileCache) Set(key string, value any) error {
|
||||
path := filepath.Join(c.path, key)
|
||||
if err := c.setMetadata(key, metadata); err != nil {
|
||||
return fmt.Errorf("error setting metadata: %s", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(path, value.([]byte), 0766); err != nil {
|
||||
return fmt.Errorf("error writting cache file: %s", err)
|
||||
|
@ -56,8 +70,48 @@ func (c *FileCache) Set(key string, value any) error {
|
|||
}
|
||||
|
||||
func (c *FileCache) Delete(key string) error {
|
||||
path := filepath.Join(c.path, key)
|
||||
return os.Remove(path)
|
||||
path := c.getPathForItem(key)
|
||||
|
||||
if err := os.Remove(path); err != nil {
|
||||
return fmt.Errorf("error deleting cache file: %s", err)
|
||||
}
|
||||
|
||||
if err := os.Remove(path + ".metadata"); err != nil {
|
||||
return fmt.Errorf("error deleting cache metadata: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *FileCache) getPathForItem(key string) string {
|
||||
return filepath.Join(c.path, key)
|
||||
}
|
||||
|
||||
func (c *FileCache) getMetadata(key string) (model.CacheItem, error) {
|
||||
path := c.getPathForItem(key) + ".metadata"
|
||||
|
||||
contents, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return model.CacheItem{}, fmt.Errorf("error reading cache file: %s", err)
|
||||
}
|
||||
|
||||
var metadata model.CacheItem
|
||||
if err := json.Unmarshal(contents, &metadata); err != nil {
|
||||
return model.CacheItem{}, fmt.Errorf("error unmarshalling cache file: %s", err)
|
||||
}
|
||||
|
||||
return metadata, nil
|
||||
}
|
||||
|
||||
func (c *FileCache) setMetadata(key string, metadata model.CacheItem) error {
|
||||
path := c.getPathForItem(key) + ".metadata"
|
||||
|
||||
contents, err := json.Marshal(metadata)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error marshalling cache file: %s", err)
|
||||
}
|
||||
|
||||
return os.WriteFile(path, contents, 0644)
|
||||
}
|
||||
|
||||
func NewFileCache(folderName string) (*FileCache, error) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue