MCPcopy
hub / github.com/bradfitz/gomemcache / GetMulti

Method GetMulti

memcache/memcache.go:478–513  ·  view source on GitHub ↗

GetMulti is a batch version of Get. The returned map from keys to items may have fewer elements than the input slice, due to memcache cache misses. Each key must be at most 250 bytes in length. If no error is returned, the returned map will also be non-nil.

(keys []string)

Source from the content-addressed store, hash-verified

476// cache misses. Each key must be at most 250 bytes in length.
477// If no error is returned, the returned map will also be non-nil.
478func (c *Client) GetMulti(keys []string) (map[string]*Item, error) {
479 var mu sync.Mutex
480 m := make(map[string]*Item)
481 addItemToMap := func(it *Item) {
482 mu.Lock()
483 defer mu.Unlock()
484 m[it.Key] = it
485 }
486
487 keyMap := make(map[net.Addr][]string)
488 for _, key := range keys {
489 if !legalKey(key) {
490 return nil, ErrMalformedKey
491 }
492 addr, err := c.selector.PickServer(key)
493 if err != nil {
494 return nil, err
495 }
496 keyMap[addr] = append(keyMap[addr], key)
497 }
498
499 ch := make(chan error, buffered)
500 for addr, keys := range keyMap {
501 go func(addr net.Addr, keys []string) {
502 ch <- c.getFromAddr(addr, keys, addItemToMap)
503 }(addr, keys)
504 }
505
506 var err error
507 for _ = range keyMap {
508 if ge := <-ch; ge != nil {
509 err = ge
510 }
511 }
512 return m, err
513}
514
515// parseGetResponse reads a GET response from r and calls cb for each
516// read and allocated Item

Callers 1

testWithClientFunction · 0.80

Calls 3

getFromAddrMethod · 0.95
legalKeyFunction · 0.85
PickServerMethod · 0.65

Tested by 1

testWithClientFunction · 0.64