Created
July 28, 2013 00:53
-
-
Save capotej/6096925 to your computer and use it in GitHub Desktop.
Single node, nonnetwork example of groupcache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/golang/groupcache" | |
) | |
type SlowDB struct { | |
data map[string]string | |
} | |
func (db *SlowDB) Get(key string) string { | |
fmt.Printf("getting %s\n", key) | |
return db.data[key] | |
} | |
func (db *SlowDB) Set(key string, value string) { | |
fmt.Printf("setting %s to %s\n", key, value) | |
db.data[key] = value | |
} | |
func NewSlowDB() *SlowDB { | |
ndb := new(SlowDB) | |
ndb.data = make(map[string]string) | |
return ndb | |
} | |
func main() { | |
db := NewSlowDB() | |
db.Set("foo", "bar") | |
db.Set("one", "two") | |
var stringcache = groupcache.NewGroup("SlowDBCache", 64<<20, groupcache.GetterFunc( | |
func(ctx groupcache.Context, key string, dest groupcache.Sink) error { | |
result := db.Get(key) | |
dest.SetBytes([]byte(result)) | |
return nil | |
})) | |
var data []byte | |
err := stringcache.Get(nil, "foo", | |
groupcache.AllocatingByteSliceSink(&data)) | |
err2 := stringcache.Get(nil, "foo", | |
groupcache.AllocatingByteSliceSink(&data)) | |
if err != nil { | |
fmt.Println("error") | |
} | |
if err2 != nil { | |
fmt.Println("error") | |
} | |
fmt.Printf("data was %s\n", data) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment