本文整理匯總了Golang中github.com/golang/groupcache.AllocatingByteSliceSink函數的典型用法代碼示例。如果您正苦於以下問題:Golang AllocatingByteSliceSink函數的具體用法?Golang AllocatingByteSliceSink怎麽用?Golang AllocatingByteSliceSink使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了AllocatingByteSliceSink函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
func main() {
var err error
p = groupcache.NewHTTPPool("http://127.0.0.1:50000")
// update p's list
p.Set("http://127.0.0.1:50001", "http://127.0.0.1:50002")
// create a new group by name and set a function for get a value by key
// if you g.Get KEY at first time, it will store the value use sink, run the function
// if you g.Get KEY not at first time, actually it NOT entry the function
// does not support update a key's value
g = groupcache.NewGroup("dns", 64<<20, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) (err error) {
if ctx == nil {
ctx = "fuck"
}
log.Println(key, ctx)
err = dest.SetString(ctx.(string))
return
}))
// let it listen
go http.ListenAndServe("127.0.0.1:50000", nil)
// get a key's value into data
time.Sleep(2 * time.Second)
var data []byte
var key = "key"
err = g.Get("aa", key, groupcache.AllocatingByteSliceSink(&data))
if err != nil {
log.Println("get error:", err)
}
log.Println(string(data))
time.Sleep(2 * time.Second)
key = "key1"
err = g.Get("bb", key, groupcache.AllocatingByteSliceSink(&data))
if err != nil {
log.Println("get error:", err)
}
log.Println(string(data))
time.Sleep(2 * time.Second)
key = "key"
err = g.Get("cc", key, groupcache.AllocatingByteSliceSink(&data))
if err != nil {
log.Println("get error:", err)
}
log.Println(string(data))
}
示例2: handleImageRequest
func handleImageRequest(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
}
w.Header().Set("Cache-Control", "public, max-age=31536000")
// Client is checking for a cached URI, assume it is valid
// and return a 304
if r.Header.Get("If-Modified-Since") != "" {
w.WriteHeader(http.StatusNotModified)
return
}
gc := fetch.RequestContext(r)
var data []byte
err := cache.Get(gc, gc.CacheKey(), groupcache.AllocatingByteSliceSink(&data))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", http.DetectContentType(data))
http.ServeContent(w, r, gc.ImageId, time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), bytes.NewReader(data))
}
示例3: main
func main() {
// STARTINIT OMIT
me := "http://10.0.0.1"
peers := groupcache.NewHTTPPool(me)
// Whenever peers change:
peers.Set("http://10.0.0.1", "http://10.0.0.2", "http://10.0.0.3")
// ENDINIT OMIT
// STARTGROUP OMIT
var thumbNails = groupcache.NewGroup("thumbnail", 64<<20, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
fileName := key
dest.SetBytes(generateThumbnail(fileName))
return nil
}))
// ENDGROUP OMIT
var ctx groupcache.Context
var w http.ResponseWriter
var r *http.Request
// STARTUSE OMIT
var data []byte
err := thumbNails.Get(ctx, "big-file.jpg",
groupcache.AllocatingByteSliceSink(&data))
// ...
_ = err // OMIT
var modTime time.Time // OMIT
http.ServeContent(w, r, "big-file-thumb.jpg", modTime, bytes.NewReader(data))
// ENDUSE OMIT
}
示例4: main
func main() {
me := ":" + os.Args[1]
peers := groupcache.NewHTTPPool("http://localhost" + me)
peers.Set("http://localhost:8081", "http://localhost:8082",
"http://localhost:8083")
helloworld := groupcache.NewGroup("helloworld",
1024*1024*10, groupcache.GetterFunc(
func(ctx groupcache.Context,
key string,
dest groupcache.Sink) error {
log.Println(me)
dest.SetString(me + "->" + key)
return nil
}))
fmt.Println("GroupName: ", helloworld.Name())
http.HandleFunc("/xbox/", func(w http.ResponseWriter, r *http.Request) {
parts := strings.SplitN(r.URL.Path[len("/xbox/"):], "/", 1)
for _, x := range parts {
fmt.Println("get: ", x)
}
if len(parts) != 1 {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
var data []byte
helloworld.Get(nil, parts[0], groupcache.AllocatingByteSliceSink(&data))
w.Write(data)
log.Println("Gets: ", helloworld.Stats.Gets.String())
log.Println("Load: ", helloworld.Stats.Loads.String())
log.Println("LocalLoad: ", helloworld.Stats.LocalLoads.String())
log.Println("PeerError: ", helloworld.Stats.PeerErrors.String())
log.Println("PeerLoad: ", helloworld.Stats.PeerLoads.String())
})
http.ListenAndServe(me, nil)
}
示例5: main
func main() {
// STARTINIT OMIT
me := "http://127.0.0.1:11211"
peers := groupcache.NewHTTPPool(me)
// Whenever peers change:
peers.Set("http://127.0.0.1:11211")
// ENDINIT OMIT
// STARTGROUP OMIT
var thumbNails = groupcache.NewGroup("thumbnail", 64<<20, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
dest.SetBytes(generateThumbnail(key))
return nil
}))
// ENDGROUP OMIT
var ctx groupcache.Context
// STARTUSE OMIT
var data []byte
thumbNails.Get(ctx, "big-file.jpg", groupcache.AllocatingByteSliceSink(&data))
fmt.Println(string(data))
}
示例6: resultFromCache
func resultFromCache(siteURL string) ([]Icon, error) {
if iconCache == nil {
return FetchIcons(siteURL)
}
// Let results expire after a day
now := time.Now()
key := fmt.Sprintf("%d-%02d-%02d-%s", now.Year(), now.Month(), now.Day(), siteURL)
var data []byte
err := iconCache.Get(siteURL, key, groupcache.AllocatingByteSliceSink(&data))
if err != nil {
logger.Println("ERR:", err)
return FetchIcons(siteURL)
}
res := &result{}
err = json.Unmarshal(data, res)
if err != nil {
panic(err)
}
if res.Error != "" {
return res.Icons, errors.New(res.Error)
}
return res.Icons, nil
}
示例7: ServeHTTP
func (g *GGFetchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
method := r.URL.Path[1:]
key := r.URL.RawQuery
log.Println("METHOD", method, "KEY", key)
hi, ok := g.methods[method]
if !ok {
http.NotFound(w, r)
return
}
buf := make([]byte, 0)
if err := hi.Group.Get(nil, key, groupcache.AllocatingByteSliceSink(&buf)); err != nil {
log.Println("ERROR", err, "METHOD", method, "KEY", key)
var statusCode int
switch err {
case image.ErrFormat, io.ErrUnexpectedEOF:
statusCode = http.StatusBadRequest
default:
statusCode = http.StatusInternalServerError
}
http.Error(w, err.Error(), statusCode)
return
}
if err := hi.Fetcher.WriteResponse(w, buf); err != nil {
log.Println("ERROR", err, "METHOD", method, "KEY", key)
}
}
示例8: Get
func (c *DimensionsCache) Get(url *url.URL) (d Dimensions, err error) {
var data []byte
c.group.Get(nil, url.String(), groupcache.AllocatingByteSliceSink(&data))
d = Dimensions{}
err = binary.Read(bytes.NewReader(data), binary.BigEndian, &d)
return d, err
}
示例9: Get
func (s *Frontend) Get(args *api.Load, reply *api.ValueResult) error {
var data []byte
fmt.Printf("cli asked for %s from groupcache\n", args.Key)
err := s.cacheGroup.Get(nil, args.Key,
groupcache.AllocatingByteSliceSink(&data))
reply.Value = string(data)
return err
}
示例10: Get
func (this *GroupCacheKvStorage) Get(key interface{}) (interface{}, error) {
var data []byte
this.CacheGroup.Get(nil, key.(string), groupcache.AllocatingByteSliceSink(&data))
object, err := bytesToInterface(data, this.T)
if err != nil {
return nil, err
}
return object, nil
}
示例11: ReadAll
func (f File) ReadAll(intr fs.Intr) ([]byte, fuse.Error) {
var contents []byte
err := filecache.Get(nil, f.Path, groupcache.AllocatingByteSliceSink(&contents))
if err != nil {
log.Print(err)
return nil, fuse.ENOENT
}
return contents, nil
}
示例12: TestGetterErrorContextType
func TestGetterErrorContextType(t *testing.T) {
var data []byte
dest := groupcache.AllocatingByteSliceSink(&data)
gt := &Getter{
Server: &imageserver.StaticServer{
Image: testdata.Medium,
},
}
err := gt.Get("invalid", "foo", dest)
if err == nil {
t.Fatal("no error")
}
}
示例13: TestGetterErrorContextType
func TestGetterErrorContextType(t *testing.T) {
var data []byte
dest := groupcache.AllocatingByteSliceSink(&data)
gt := &Getter{
Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
return testdata.Medium, nil
}),
}
err := gt.Get("invalid", "foo", dest)
if err == nil {
t.Fatal("no error")
}
}
示例14: console
func console() {
scanner := bufio.NewScanner(os.Stdin)
quit := false
for !quit {
fmt.Print("gc> ")
if !scanner.Scan() {
break
}
line := scanner.Text()
parts := strings.Split(line, " ")
cmd := parts[0]
args := parts[1:]
switch cmd {
case "peers":
pool.Set(args...)
case "stats":
stats := dns.CacheStats(groupcache.MainCache)
fmt.Println("Bytes: ", stats.Bytes)
fmt.Println("Items: ", stats.Items)
fmt.Println("Gets: ", stats.Gets)
fmt.Println("Hits: ", stats.Hits)
fmt.Println("Evictions:", stats.Evictions)
case "get":
var data []byte
err := dns.Get(nil, args[0],
groupcache.AllocatingByteSliceSink(&data))
if err != nil {
fmt.Println("get error:", err)
continue
}
fmt.Print(args[0], ":")
io.Copy(os.Stdout, bytes.NewReader(data))
fmt.Println()
case "quit":
quit = true
default:
fmt.Println("unrecognized command:", cmd, args)
}
}
if err := scanner.Err(); err != nil {
fmt.Println("reading stdin:", err)
}
}
示例15: handler
// handler handles all incoming requests for a definition.
func handler(w http.ResponseWriter, r *http.Request) {
log.Println("received request:", r.Method, r.URL.Path)
word := strings.Trim(path.Base(r.URL.Path), "/")
// Get the definition from groupcache and write it out.
var data []byte
err := dict.Get(nil, word, groupcache.AllocatingByteSliceSink(&data))
if err != nil {
log.Println("retreiving definition for", word, "-", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
io.Copy(w, bytes.NewReader(data))
}