cheapcash/cheapcash.go

62 lines
1.4 KiB
Go
Raw Permalink Normal View History

2021-11-16 04:56:33 +00:00
package cheapcash
import (
"errors"
2021-11-16 05:20:12 +00:00
"strings"
2021-11-16 04:56:33 +00:00
"sync"
)
type Cache struct {
sync.Mutex
Path string
}
var ErrNotExists = errors.New("key does not exist")
var ErrInvalidPath = errors.New("path supplied is invalid")
var ErrDiskFull = errors.New("there was no space left on the device")
2021-11-16 13:18:23 +00:00
var ErrExists = errors.New("key already exists")
2021-11-16 04:56:33 +00:00
2021-11-19 10:55:31 +00:00
// Creates default Cheapcash instance which defaults
// the corresponding cache path to /tmp/cheapcash.
//
// The caveat of using this one is this will be most likely
// only compatible with UNIX-like filesystem.
// Windows devices will most likely happen to have
// an error of the invalid path.
//
// This returns a Cheapcash instance, which method
// ('Append', 'Exists', 'Write', 'Read') you can do
// by just specifying the key, without supplying the
// full path of the cached file.
2021-11-16 04:56:33 +00:00
func Default() *Cache {
return &Cache{
Path: "/tmp/cheapcash/",
}
}
2021-11-19 10:55:31 +00:00
// Creates a new Cheapcash instance with the given
// path from the argument provided.
//
// If path is empty (or an empty string), it will panic
// with ErrInvalidPath error.
//
// The path provided might have '/' as the ending, so
// these are valid and will return the same path:
//
// New("/tmp/box")
// New("/tmp/box/")
2021-11-16 04:56:33 +00:00
func New(path string) *Cache {
2021-11-19 10:55:31 +00:00
if path == "" {
panic(ErrInvalidPath)
}
2021-11-16 05:20:12 +00:00
if !strings.HasSuffix(path, "/") {
path += "/"
}
2021-11-16 04:56:33 +00:00
return &Cache{
Path: path,
}
}