Documentation ¶
Overview ¶
Package cacher provides a uniform interface for different caching strategies.
Index ¶
- func Add(key string, value []byte, ttl int64) error
- func CompareAndReplace(token, key string, value []byte, ttl int64) error
- func Decrement(key string, initial, offset, ttl int64) error
- func Delete(key string) error
- func DeleteMulti(keys []string) map[string]error
- func Flush() error
- func Get(key string) ([]byte, string, error)
- func GetMulti(keys []string) (map[string][]byte, map[string]string, map[string]error)
- func Increment(key string, initial, offset, ttl int64) error
- func Replace(key string, value []byte, ttl int64) error
- func Set(key string, value []byte, ttl int64) error
- func SetMulti(keys map[string][]byte, ttl int64) map[string]error
- func Touch(key string, ttl int64) error
- type Cacher
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶
Add adds a value to the cache under the specified key with a given TTL.
Example ¶
package main import ( "fmt" "github.com/jelmersnoeck/cacher" ) func main() { cacher.Flush() ok1 := cacher.Add("key1", []byte("value1"), 0) val1, _, _ := cacher.Get("key1") fmt.Println(string(val1), ok1) ok2 := cacher.Add("key1", []byte("value2"), 0) val2, _, _ := cacher.Get("key1") fmt.Println(string(val2), ok2) }
Output: value1 <nil> value1 Key `key1` already exists.
func CompareAndReplace ¶
CompareAndReplace validates the token with the token in the store. If the tokens match, we will replace the value and return true. If it doesn't, we will not replace the value and return false.
Example ¶
package main import ( "fmt" "github.com/jelmersnoeck/cacher" ) func main() { cacher.Flush() cacher.Set("key1", []byte("hello world"), 0) _, token, _ := cacher.Get("key1") var err error var val []byte err = cacher.CompareAndReplace(token+"FALSE", "key1", []byte("replacement1"), 0) val, _, _ = cacher.Get("key1") fmt.Println(err, string(val)) err = cacher.CompareAndReplace(token, "key1", []byte("replacement2"), 0) val, _, _ = cacher.Get("key1") fmt.Println(err, string(val)) }
Output: Key `key1` does not exist. hello world <nil> replacement2
func Decrement ¶
Decrement subtracts a value of offset to the initial value. If the initial value is already set, it will be added to the value currently stored in the cache.
Example ¶
package main import ( "fmt" "github.com/jelmersnoeck/cacher" "github.com/jelmersnoeck/cacher/internal/encoding" ) func main() { cacher.Flush() cacher.Decrement("key1", 10, 1, 0) cacher.Decrement("key1", 10, 3, 0) v, _, _ := cacher.Get("key1") num, _ := encoding.BytesInt64(v) fmt.Println(num) cacher.Set("key2", []byte("string value, not decrementable"), 0) ok := cacher.Decrement("key2", 0, 5, 0) v2, _, _ := cacher.Get("key2") fmt.Println(ok, string(v2)) }
Output: 7 Value for key `key2` could not be encoded. string value, not decrementable
func Delete ¶
Delete will validate if the key actually is stored in the cache. If it is stored, it will remove the item from the cache. If it is not stored, it will return false.
Example ¶
package main import ( "fmt" "github.com/jelmersnoeck/cacher" ) func main() { cacher.Flush() var err error cacher.Set("key1", []byte("value1"), 0) err = cacher.Delete("key1") fmt.Println(err) err = cacher.Delete("non-existing") fmt.Println(err) }
Output: <nil> Key `non-existing` was not found.
func DeleteMulti ¶
DeleteMulti will delete multiple values at a time. It uses the `Delete` method internally to do so. It will return a map of results to see if the deletion is successful.
Example ¶
package main import ( "fmt" "github.com/jelmersnoeck/cacher" ) func main() { cacher.Flush() keys := []string{"key1", "key2", "non-existing"} multi := map[string][]byte{ "key1": []byte("value1"), "key2": []byte("value2"), } cacher.SetMulti(multi, 0) oks := cacher.DeleteMulti(keys) fmt.Println(oks["key1"]) fmt.Println(oks["key2"]) fmt.Println(oks["non-existing"]) }
Output: <nil> <nil> Key `non-existing` was not found.
func Flush ¶
func Flush() error
Flush will remove all the items from the storage.
Example ¶
package main import ( "fmt" "github.com/jelmersnoeck/cacher" ) func main() { cacher.Flush() var errs map[string]error keys := []string{"key1", "key2"} multi := map[string][]byte{ "key1": []byte("value1"), "key2": []byte("value2"), } cacher.SetMulti(multi, 0) _, _, errs = cacher.GetMulti(keys) fmt.Println(errs["key1"], errs["key2"]) cacher.Flush() _, _, errs = cacher.GetMulti(keys) fmt.Println(errs["key1"], errs["key2"]) }
Output: <nil> <nil> Key `key1` does not exist. Key `key2` does not exist.
func Get ¶
Get gets the value out of the map associated with the provided key.
Example ¶
package main import ( "fmt" "github.com/jelmersnoeck/cacher" ) func main() { cacher.Flush() var value []byte var token string var err error value, token, err = cacher.Get("non-existing") fmt.Println(string(value), token, err) cacher.Set("key1", []byte("Hello world!"), 0) value, token, err = cacher.Get("key1") fmt.Println(string(value), token, err) }
Output: Key `non-existing` does not exist. Hello world! 86fb269d190d2c85f6e0468ceca42a20 <nil>
func GetMulti ¶
GetMulti gets multiple values from the cache and returns them as a map. It uses `Get` internally to retrieve the data.
Example ¶
package main import ( "fmt" "github.com/jelmersnoeck/cacher" ) func main() { cacher.Flush() multi := map[string][]byte{ "key1": []byte("value1"), "key2": []byte("value2"), } cacher.SetMulti(multi, 0) keys := []string{"key1", "key2"} values, tokens, bools := cacher.GetMulti(keys) fmt.Println(values["key1"], values["key2"]) fmt.Println(tokens["key1"], tokens["key2"]) fmt.Println(bools["key1"], bools["key2"]) }
Output: [118 97 108 117 101 49] [118 97 108 117 101 50] 9946687e5fa0dab5993ededddb398d2e f066ce9385512ee02afc6e14d627e9f2 <nil> <nil>
func Increment ¶
Increment adds a value of offset to the initial value. If the initial value is already set, it will be added to the value currently stored in the cache.
Example ¶
package main import ( "fmt" "github.com/jelmersnoeck/cacher" "github.com/jelmersnoeck/cacher/internal/encoding" ) func main() { cacher.Flush() cacher.Increment("key1", 0, 1, 0) cacher.Increment("key1", 0, 1, 0) v, _, _ := cacher.Get("key1") num, _ := encoding.BytesInt64(v) fmt.Println(num) cacher.Set("key2", []byte("string value, not incrementable"), 0) ok := cacher.Increment("key2", 0, 5, 0) v2, _, _ := cacher.Get("key2") fmt.Println(ok, string(v2)) }
Output: 1 Value for key `key2` could not be encoded. string value, not incrementable
func Replace ¶
Replace will update and only update the value of a cache key. If the key is not previously used, we will return false.
Example ¶
package main import ( "fmt" "github.com/jelmersnoeck/cacher" ) func main() { cacher.Flush() var err error err = cacher.Replace("key1", []byte("replacement"), 0) fmt.Println(err) cacher.Set("key1", []byte("value1"), 0) err = cacher.Replace("key1", []byte("replacement"), 0) fmt.Println(err) }
Output: Key `key1` does not exist. <nil>
func Set ¶
Set sets the value of an item, regardless of wether or not the value is already cached.
ttl defines the number of seconds the value should be cached. If ttl is 0, the item will be cached infinitely.
Example ¶
package main import ( "fmt" "github.com/jelmersnoeck/cacher" ) func main() { cacher.Flush() ok1 := cacher.Set("key1", []byte("value1"), 0) val1, _, _ := cacher.Get("key1") fmt.Println(string(val1), ok1) ok2 := cacher.Set("key1", []byte("value2"), 0) val2, _, _ := cacher.Get("key1") fmt.Println(string(val2), ok2) }
Output: value1 <nil> value2 <nil>
func SetMulti ¶
SetMulti sets multiple values for their respective keys. This is a shorthand to use `Set` multiple times.
Example ¶
package main import ( "fmt" "github.com/jelmersnoeck/cacher" ) func main() { cacher.Flush() multi := map[string][]byte{ "key1": []byte("value1"), "key2": []byte("value2"), } cacher.SetMulti(multi, 0) val1, _, _ := cacher.Get("key1") val2, _, _ := cacher.Get("key2") fmt.Println(string(val1)) fmt.Println(string(val2)) }
Output: value1 value2
func Touch ¶
Touch will update the key's ttl to the given ttl value without altering the value.
Example ¶
package main import ( "fmt" "github.com/jelmersnoeck/cacher" ) func main() { cacher.Flush() cacher.Set("key1", []byte("value1"), 1) err := cacher.Touch("key1", 5) fmt.Println(err) }
Output: <nil>
Types ¶
type Cacher ¶
type Cacher interface { Add(key string, value []byte, ttl int64) error CompareAndReplace(token, key string, value []byte, ttl int64) error Set(key string, value []byte, ttl int64) error SetMulti(keys map[string][]byte, ttl int64) map[string]error Replace(key string, value []byte, ttl int64) error Increment(key string, initial, offset, ttl int64) error Decrement(key string, initial, offset, ttl int64) error Delete(key string) error DeleteMulti(keys []string) map[string]error Get(key string) ([]byte, string, error) GetMulti(keys []string) (map[string][]byte, map[string]string, map[string]error) Flush() error Touch(key string, ttl int64) error }
Cacher is the Caching interface that uniforms all the different strategies.