# Packages
No description provided by the author
# README
Memcache Client
This is a memcache client library for the Go programming language, which uses memcache's Meta Text Protocol and supports namespacing out of the box.
Installing
To add this libraty to your project, just run:
go get github.com/kinescope/mc
Example
package main
import (
"context"
"log"
"time"
"github.com/kinescope/mc"
)
func ExampleBase() {
memcache, err := mc.New(&mc.Options{
Addrs: []string{"127.0.0.1:11211"},
})
if err != nil {
log.Fatal(err)
}
err = memcache.Set(&mc.Item{
Key: "key",
Value: []byte("value"),
Flags: 42,
}, mc.WithExpiration(5))
if err != nil {
log.Fatal(err)
}
i, err := memcache.Get("key")
if err != nil {
log.Fatal(err)
}
fmt.Printf("key=%q, value=%q\n", i.Key, i.Value)
// Output: key="key", value="value"
}
Namespacing
Let's say you have a user with some user_id like 123
. Given a user and all his related keys, you want a one-stop switch to invalidate all of their cache entries at the same time.
With namespacing you need to add a mc.WithNamespace
option when setting any user related key.
func ExampleNamespace() {
memcache, err := mc.New(&mc.Options{
Addrs: []string{"127.0.0.1:11211"},
})
if err != nil {
log.Fatal(err)
}
err = memcache.Set(&mc.Item{
Key: "key",
Value: []byte("value"),
Flags: 42,
}, mc.WithExpiration(5), mc.WithNamespace("namespace"))
if err != nil {
log.Fatal(err)
}
i, err := memcache.Get("key")
if err != nil {
log.Fatal(err)
}
fmt.Printf("key=%q, value=%q\n", i.Key, i.Value)
memcache.PurgeNamespace("namespace")
if _, err = memcache.Get("key"); err == nil {
log.Fatal("ns bug")
}
fmt.Println(err)
// Output:
// key="key", value="value"
// memcache: cache miss
}
Then invalidating all of the keys for a user with id 123
would be as easy as:
cache.PurgeNamespace(ctx, "user_123") // both 123:John and John:[email protected] entries will be deleted
For more info on namespaces see memcache wiki.
Other options
mc.WithMinUses(number uint32)
- if an item under the key has been set less thannumber
of times, requesting an item will result in a cache miss. See tests for clarity.mc.WithEarlyRecache(seconds int)
- https://docs.memcached.org/protocols/meta/#early-recachemc.WithLastAccess
- returns the time in seconds since last access.- ... - see
mc.With*
Contributing
For contributing see CONTRIBUTING.md
Licensing
The code in this project is licensed under MIT license.