Compare commits
No commits in common. "92732f9682ac5022710df1d1f03cf23141ec8482" and "ef80892aa542bd923925bcf8912b345cb5918792" have entirely different histories.
92732f9682
...
ef80892aa5
4 changed files with 0 additions and 168 deletions
77
cache/file.go
vendored
77
cache/file.go
vendored
|
@ -1,77 +0,0 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"git.nakama.town/fmartingr/gotoolkit/model"
|
||||
)
|
||||
|
||||
var _ model.Cache = (*FileCache)(nil)
|
||||
|
||||
type FileCache struct {
|
||||
path string
|
||||
}
|
||||
|
||||
func (c *FileCache) Get(key string) (result any, err error) {
|
||||
path := filepath.Join(c.path, key)
|
||||
|
||||
contents, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) || errors.Is(err, os.ErrPermission) {
|
||||
return result, model.ErrCacheKeyDontExist
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if info.ModTime().Add(expiration).Before(time.Now()) {
|
||||
c.Delete(key)
|
||||
return result, model.ErrCacheKeyDontExist
|
||||
}
|
||||
|
||||
return c.Get(key)
|
||||
}
|
||||
|
||||
func (c *FileCache) Set(key string, value any) error {
|
||||
path := filepath.Join(c.path, key)
|
||||
|
||||
if err := os.WriteFile(path, value.([]byte), 0766); err != nil {
|
||||
return fmt.Errorf("error writting cache file: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *FileCache) Delete(key string) error {
|
||||
path := filepath.Join(c.path, key)
|
||||
return os.Remove(path)
|
||||
}
|
||||
|
||||
func NewFileCache(folderName string) (*FileCache, error) {
|
||||
userCacheDir, err := os.UserCacheDir()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not retrieve user cache directory: %w", err)
|
||||
}
|
||||
path := filepath.Join(userCacheDir, folderName)
|
||||
|
||||
if err := os.MkdirAll(path, 0755); err != nil {
|
||||
return nil, fmt.Errorf("couldn't create cache directory: %w", err)
|
||||
}
|
||||
|
||||
return &FileCache{
|
||||
path: path,
|
||||
}, nil
|
||||
}
|
53
cache/memory.go
vendored
53
cache/memory.go
vendored
|
@ -1,53 +0,0 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.nakama.town/fmartingr/gotoolkit/model"
|
||||
)
|
||||
|
||||
var _ model.Cache = (*MemoryCache)(nil)
|
||||
|
||||
type MemoryCache struct {
|
||||
data map[string]any
|
||||
dataMu sync.RWMutex
|
||||
}
|
||||
|
||||
func (c *MemoryCache) Get(key string) (result any, err error) {
|
||||
c.dataMu.RLock()
|
||||
defer c.dataMu.RUnlock()
|
||||
|
||||
result, exists := c.data[key]
|
||||
if !exists {
|
||||
return result, model.ErrCacheKeyDontExist
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *MemoryCache) GetWithExpiry(key string, expiration time.Duration) (any, error) {
|
||||
// TODO: Implement expiration in memory cache.
|
||||
return c.Get(key)
|
||||
}
|
||||
|
||||
func (c *MemoryCache) Set(key string, value any) error {
|
||||
c.dataMu.Lock()
|
||||
c.data[key] = value
|
||||
c.dataMu.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *MemoryCache) Delete(key string) error {
|
||||
c.dataMu.Lock()
|
||||
delete(c.data, key)
|
||||
c.dataMu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewMemoryCache() *MemoryCache {
|
||||
return &MemoryCache{
|
||||
data: make(map[string]any),
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var ErrCacheKeyDontExist = errors.New("cache key don't exist")
|
||||
|
||||
type Cache interface {
|
||||
Get(key string) (any, error)
|
||||
GetWithOptions(key string, opts ...CacheGetOption)
|
||||
Set(key string, value any) error
|
||||
SetWithOptions(key string, value any, opts ...CacheSetOption)
|
||||
Delete(key string) error
|
||||
}
|
||||
|
||||
type CacheGetOption func()
|
||||
type CacheSetOption func()
|
|
@ -1,20 +0,0 @@
|
|||
package paths
|
||||
|
||||
import (
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ExpandUser(providedPath string) string {
|
||||
var path string = providedPath
|
||||
usr, _ := user.Current()
|
||||
dir := usr.HomeDir
|
||||
|
||||
if providedPath == "~" {
|
||||
path = dir
|
||||
} else if strings.HasPrefix(providedPath, "~/") {
|
||||
path = filepath.Join(dir, providedPath[2:])
|
||||
}
|
||||
return path
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue