cheapcash/append.go

41 lines
823 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
// Directly append a value into an existing key.
// If a key doesn't exists, it will return an error
// with a type of ErrNotExists.
//
// c := cheapcash.Default()
// err := c.Append("users", []byte("Someone\n"))
// if err != nil {
// if errors.Is(err, cheapcash.ErrNotExists) {
// // Handle if file does not exists!
// }
// // Handle any other errors
// }
//
2021-11-16 04:56:33 +00:00
func (c *Cache) Append(key string, value []byte) error {
check, err := c.Exists(c.Path + key)
if err != nil {
return err
}
if !check {
return ErrNotExists
}
file, err := os.OpenFile(sanitizePath(c.Path+key), os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(value)
if err != nil {
return err
}
return nil
}