cheapcash/reader.go

42 lines
735 B
Go
Raw Normal View History

2021-11-16 04:56:33 +00:00
package cheapcash
import (
"io/ioutil"
"os"
)
2021-11-19 10:55:31 +00:00
// Read the value of a given key.
// Will return an error of ErrNotExists if the given
// key does not exists.
//
// c := cheapcash.Default()
// res, err := c.Read("users")
// if err != nil {
// // handle your error here!
// }
// log.Println(string(res))
//
2021-11-16 04:56:33 +00:00
func (c *Cache) Read(key string) ([]byte, error) {
check, err := c.Exists(c.Path + key)
if err != nil {
return []byte{}, err
}
if !check {
return []byte{}, ErrNotExists
}
file, err := os.Open(sanitizePath(c.Path + key))
if err != nil {
return []byte{}, err
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
return []byte{}, err
}
return data, nil
}