fix: bug on file location

This commit is contained in:
Reinaldy Rafli 2021-11-16 12:20:12 +07:00
parent e6f33ca1d4
commit 012b2befd3
No known key found for this signature in database
GPG Key ID: CFDB9400255D8CB6
4 changed files with 10 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package cheapcash
import ( import (
"errors" "errors"
"strings"
"sync" "sync"
) )
@ -22,6 +23,10 @@ func Default() *Cache {
} }
func New(path string) *Cache { func New(path string) *Cache {
if !strings.HasSuffix(path, "/") {
path += "/"
}
return &Cache{ return &Cache{
Path: path, Path: path,
} }

View File

@ -24,8 +24,8 @@ func TestDefault(t *testing.T) {
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
c := cheapcash.New("/somewhere") c := cheapcash.New("/somewhere")
if c.Path != "/somewhere" { if c.Path != "/somewhere/" {
t.Error("expected path to return /somewhere, got:", c.Path) t.Error("expected path to return /somewhere/, got:", c.Path)
} }
} }

View File

@ -3,7 +3,7 @@ package cheapcash
import "os" import "os"
func (c *Cache) Delete(key string) error { func (c *Cache) Delete(key string) error {
check, err := c.Exists(c.Path + "/" + key) check, err := c.Exists(c.Path + key)
if err != nil { if err != nil {
return err return err
} }
@ -12,7 +12,7 @@ func (c *Cache) Delete(key string) error {
return ErrNotExists return ErrNotExists
} }
err = os.Remove(sanitizePath(c.Path + "/" + key)) err = os.Remove(sanitizePath(c.Path + key))
if err != nil { if err != nil {
return err return err
} }

View File

@ -8,7 +8,7 @@ func (c *Cache) Write(key string, value []byte) error {
return err return err
} }
check, err := c.Exists(key) check, err := c.Exists(c.Path + key)
if err != nil { if err != nil {
return err return err
} }