cheapcash/delete.go

32 lines
588 B
Go
Raw Normal View History

2021-11-16 04:56:33 +00:00
package cheapcash
import "os"
2021-11-19 10:55:31 +00:00
// Delete a key from the cache directory.
// And of course would return an error of ErrNotExists
// if the key doesn't exists.
//
// c := cheapcash.Default()
// err := c.Write("users", []byte("Someone\n"))
// // Handle error here
// err = c.Delete("users")
// // Handle error here
//
2021-11-16 04:56:33 +00:00
func (c *Cache) Delete(key string) error {
2021-11-16 05:20:12 +00:00
check, err := c.Exists(c.Path + key)
2021-11-16 04:56:33 +00:00
if err != nil {
return err
}
if !check {
return ErrNotExists
}
2021-11-16 05:20:12 +00:00
err = os.Remove(sanitizePath(c.Path + key))
2021-11-16 04:56:33 +00:00
if err != nil {
return err
}
return nil
}