本文整理匯總了Golang中github.com/golang/groupcache.GetterFunc函數的典型用法代碼示例。如果您正苦於以下問題:Golang GetterFunc函數的具體用法?Golang GetterFunc怎麽用?Golang GetterFunc使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了GetterFunc函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Init
/*
Init returns an initialised Map180. d must have access to the map180 tables in the
public schema. cacheBytes is the size of the memory cache
for land and lake layers. region must be a valid Region.
Example:
wm, err = map180.Init(db, map180.Region(`newzealand`), 256000000)
*/
func Init(d *sql.DB, region Region, cacheBytes int64) (*Map180, error) {
w := &Map180{}
var err error
if _, ok := allZoomRegions[region]; !ok {
err = fmt.Errorf("invalid region (allZoomRegions): %s", region)
return w, err
}
if _, ok := allMapBounds[region]; !ok {
err = fmt.Errorf("invalid region (allMapBounds): %s", region)
return w, err
}
if _, ok := allNamedMapBounds[region]; !ok {
err = fmt.Errorf("invalid region (allNamedMapBounds): %s", region)
return w, err
}
zoomRegion = allZoomRegions[region]
mapBounds = allMapBounds[region]
namedMapBounds = allNamedMapBounds[region]
db = d
mapLayers = groupcache.NewGroup("mapLayers", cacheBytes, groupcache.GetterFunc(layerGetter))
landLayers = groupcache.NewGroup("landLayers", cacheBytes, groupcache.GetterFunc(landGetter))
lakeLayers = groupcache.NewGroup("lakeLayers", cacheBytes, groupcache.GetterFunc(lakeGetter))
return w, nil
}
示例2: 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))
}
示例3: StartCache
// StartCache is used to start up the groupcache mechanism
func (c *Constellation) StartCache() {
log.Print("Starting AuthCache")
var peers []string
if c.PeerList == nil {
log.Print("Initializing PeerList")
c.PeerList = make(map[string]string)
}
for _, peer := range c.PeerList {
log.Printf("Assigning peer '%s'", peer)
if peer > "" {
peers = append(peers, "http://"+peer+":"+GCPORT)
}
}
c.Peers.Set(peers...)
var authcache = groupcache.NewGroup(c.Groupname, 64<<20, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
pod := key
auth := c.GetAuthForPodFromConfig(pod)
if auth > "" {
dest.SetString(auth)
return nil
}
err := fmt.Errorf("Found no value for auth on pod %s, not storing anything", pod)
return err
}))
c.AuthCache = NewCache(authcache)
}
示例4: SetCacheMaxSize
// SetCacheMaxSize enables icon caching if sizeInMB > 0.
func SetCacheMaxSize(sizeInMB int64) {
if sizeInMB > 0 {
iconCache = groupcache.NewGroup("icons", sizeInMB<<20, groupcache.GetterFunc(generatorFunc))
} else {
iconCache = nil
}
}
示例5: main
func main() {
flag.Parse()
peers := gc.NewHTTPPool("http://localhost:" + *port)
peers.Set("http://localhost:8001", "http://localhost:8002")
cg = gc.NewGroup("QRCache", 1<<20, gc.GetterFunc(
func(ctx gc.Context, key string, dest gc.Sink) error {
fmt.Printf("asking for data of %s\n", key)
url := fmt.Sprintf("http://chart.apis.google.com/chart?chs=300x300&cht=qr&choe=UTF-8&chl=%s", key)
resp, err := http.Get(url)
if err != nil {
return nil
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil
}
value := base64.StdEncoding.EncodeToString(body)
dest.SetBytes([]byte(value))
return nil
}))
// run groupcache process in goroutine
go http.ListenAndServe("localhost:"+*port, peers)
http.Handle("/", http.HandlerFunc(QR))
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}
示例6: main
func main() {
/* me := "http://127.0.0.1"
peers := groupcache.NewHTTPPool(me)
// Whenever peers change:
peers.Set("http://127.0.0.1", "http://10.0.0.2", "http://10.0.0.3")*/
getter := groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
// fileName := key
bytes := []byte{'a', 'b', 'c'}
dest.SetBytes(bytes)
return nil
})
func(g *igetter) Get(ctx Content, key string, dest Sink) {
}
grp := groupcache.NewGroup("Grp", 64<<20, getter)
err := grp.Get(ctx groupcache.Context, key string, dest groupcache.Sink)
//hook
/* hook := groupcache.RegisterNewGroupHook(func(grp1 *groupcache.Group) {
fmt.Println(grp1.Name())
fmt.Println("hook")
})*/
fmt.Println(grp.Name())
fmt.Println(grp.Stats)
}
示例7: main
func main() {
flag.Parse()
// Setup the doozer connection.
d, err := doozer.Dial(daddr)
if err != nil {
log.Fatalf("connecting to doozer: %v\n", err)
}
defer d.Close()
// Setup the cache.
pool = groupcache.NewHTTPPool("http://" + addr)
dict = groupcache.NewGroup("dict", 64<<20, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
def, err := query(key)
if err != nil {
err = fmt.Errorf("querying remote dictionary: %v", err)
log.Println(err)
return err
}
log.Println("retrieved remote definition for", key)
dest.SetString(def)
return nil
}))
// Start watching for changes and signals.
go watch(d)
// Add the handler for definition requests and then start the
// server.
http.Handle("/define/", http.HandlerFunc(handler))
log.Println(http.ListenAndServe(addr, nil))
}
示例8: 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
}
示例9: 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)
}
示例10: InitRadosCache
func InitRadosCache(name string, size int64, chunkSize int) {
if cacheMade {
panic("groupcache: InitRadosCache must be called only once")
}
cacheMade = true
radosCache = groupcache.NewGroup(name, size, groupcache.GetterFunc(radosGetter))
chunkPool = sync.Pool{
New: func() interface{} { return make([]byte, chunkSize) },
}
}
示例11: NewGroupCache
// NewGroupCache function
func NewGroupCache(cacheSize int64, redisGetFunc GetFunc) Cache {
return &groupCacheMgr{
group: gc.NewGroup(singleGroupName, cacheSize, gc.GetterFunc(func(ctx gc.Context, key string, dest gc.Sink) error {
log.WithField("domain", key).Info("GroupCache:RedisRequest")
redisRequest := api.NewValueRequest(key)
redisGetFunc(redisRequest)
redisResponse := <-redisRequest.Response
log.WithField("domain", key).Info("GroupCache:RedisRequest:Done")
return dest.SetString(redisResponse.Value)
}))}
}
示例12: setupGroupcache
func setupGroupcache(config GroupcacheConfig) error {
if config.GB == 0 {
return nil
}
var cacheBytes int64
cacheBytes = int64(config.GB) << 30
pool := groupcache.NewHTTPPool(config.Host)
if pool != nil {
dvid.Infof("Initializing groupcache with %d GB at %s...\n", config.GB, config.Host)
manager.gcache.cache = groupcache.NewGroup("immutable", cacheBytes, groupcache.GetterFunc(
func(c groupcache.Context, key string, dest groupcache.Sink) error {
// Use KeyValueDB defined as context.
gctx, ok := c.(GroupcacheCtx)
if !ok {
return fmt.Errorf("bad groupcache context: expected GroupcacheCtx, got %v", c)
}
// First four bytes of key is instance ID to isolate groupcache collisions.
tk := TKey(key[4:])
data, err := gctx.KeyValueDB.Get(gctx.Context, tk)
if err != nil {
return err
}
return dest.SetBytes(data)
}))
manager.gcache.supported = make(map[dvid.DataSpecifier]struct{})
for _, dataspec := range config.Instances {
name := strings.Trim(dataspec, "\"")
parts := strings.Split(name, ":")
switch len(parts) {
case 2:
dataid := dvid.GetDataSpecifier(dvid.InstanceName(parts[0]), dvid.UUID(parts[1]))
manager.gcache.supported[dataid] = struct{}{}
default:
dvid.Errorf("bad data instance specification %q given for groupcache support in config file\n", dataspec)
}
}
// If we have additional peers, add them and start a listener via the HTTP port.
if len(config.Peers) > 0 {
peers := []string{config.Host}
peers = append(peers, config.Peers...)
pool.Set(peers...)
dvid.Infof("Groupcache configuration has %d peers in addition to local host.\n", len(config.Peers))
dvid.Infof("Starting groupcache HTTP server on %s\n", config.Host)
http.ListenAndServe(config.Host, http.HandlerFunc(pool.ServeHTTP))
}
}
return nil
}
示例13: 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))
}
示例14: MakeCache
func (g *GroupCacheProxy) MakeCache(c *Cluster, size int64) CacheGetter {
return groupcache.NewGroup(
"ReticulumCache", size, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
// get image from disk
ri := NewImageSpecifier(key)
img_data, err := c.RetrieveImage(ri)
if err != nil {
return err
}
dest.SetBytes([]byte(img_data))
return nil
}))
}
示例15: main
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
awsAuth, err := aws.EnvAuth()
if err != nil {
log.Fatalf(err.Error())
}
s3conn := s3.New(awsAuth, getRegion())
storage = store.NewS3Store(s3conn)
peers = peer.DebugPool()
peers.SetContext(func(r *http.Request) groupcache.Context {
return fetch.RequestContext(r)
})
cache = groupcache.NewGroup("ImageProxyCache", 64<<20, groupcache.GetterFunc(
func(c groupcache.Context, key string, dest groupcache.Sink) error {
log.Printf("Cache MISS for key -> %s", key)
// Get image data from S3
b, err := fetch.ImageData(storage, c)
if err != nil {
return err
}
return dest.SetBytes(b)
}))
if !*verbose {
logwriter, err := syslog.Dial("udp", "app_syslog:514", syslog.LOG_NOTICE, "vip")
if err != nil {
log.Println(err.Error())
log.Println("using default logger")
} else {
log.SetOutput(logwriter)
}
}
go peers.Listen()
go listenHttp()
go Queue.Start(4)
log.Println("Cache listening on port :" + peers.Port())
s := &http.Server{
Addr: ":" + peers.Port(),
Handler: peers,
}
s.ListenAndServe()
}