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 (
"errors"
"strings"
"sync"
)
@ -22,6 +23,10 @@ func Default() *Cache {
}
func New(path string) *Cache {
if !strings.HasSuffix(path, "/") {
path += "/"
}
return &Cache{
Path: path,
}

View File

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

View File

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

View File

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