Beispiel #1
0
func SetMulti(c appengine.Context, items []*memcache.Item) error {
	err := memcache.SetMulti(c, items)
	if err == nil {
		return make(appengine.MultiError, len(items))
	}
	return err
}
Beispiel #2
0
func (builder *stepsBuilder) SaveMemcacheBatch(context appengine.Context) stream.EachFn {
	return func(data stream.T) {
		if err := memcache.SetMulti(context, data.(*MemcacheSaveBatch).Items); err != nil {
			panic(err)
		}
	}
}
Beispiel #3
0
func cacheClear(c appengine.Context, keys ...string) error {
	items := make([]*memcache.Item, len(keys))
	for i := range keys {
		items[i] = &memcache.Item{Key: keys[i], Expiration: 2 * time.Minute, Value: []byte{0}}
	}
	return memcache.SetMulti(c, items)
}
Beispiel #4
0
// Object usually too big (1MB limit), so shard.
// http://stackoverflow.com/questions/9127982/
func BytesToShardedMemcache(c appengine.Context, key string, b []byte) {
	/*
		var buf bytes.Buffer
		if err := gob.NewEncoder(&buf).Encode(f); err != nil {
			db.C.Errorf("fdb error encoding item: %v", err)
			return
		}
		b := buf.Bytes()
	*/

	items := []*memcache.Item{}
	for i := 0; i < len(b); i += chunksize {
		k := fmt.Sprintf("=%d=%s", i, key)
		s, e := i, i+chunksize-1
		if e >= len(b) {
			e = len(b) - 1
		}
		c.Infof(" #=== [%7d, %7d] (%d) %s", s, e, len(b), k)
		items = append(items, &memcache.Item{Key: k, Value: b[s : e+1]}) // slice sytax is [s,e)
	}

	if err := memcache.SetMulti(c, items); err != nil {
		c.Errorf(" #=== cdb sharded store fail: %v", err)
	}

	c.Infof(" #=== Stored '%s' (len=%d)!", key, len(b))
}
Beispiel #5
0
// Object usually too big (1MB limit), so shard.
// http://stackoverflow.com/questions/9127982/
func (db FlightDB) flightsToShardedMemcache(key string, f []f.Flight) {

	var buf bytes.Buffer
	if err := gob.NewEncoder(&buf).Encode(f); err != nil {
		db.C.Errorf("fdb error encoding item: %v", err)
		return
	}
	b := buf.Bytes()

	items := []*memcache.Item{}
	for i := 0; i < len(b); i += chunksize {
		k := fmt.Sprintf("=%d=%s", i, key)
		s, e := i, i+chunksize-1
		if e >= len(b) {
			e = len(b) - 1
		}
		db.C.Infof(" #--- [%7d, %7d] (%d) %s", s, e, len(b), k)
		items = append(items, &memcache.Item{Key: k, Value: b[s : e+1]}) // slice sytax is [s,e)
	}

	if err := memcache.SetMulti(db.C, items); err != nil {
		db.C.Errorf(" #--- fdb sharded store fail: %v", err)
	}

	db.C.Infof(" #--- Stored '%s' (len=%d)!", key, len(b))
}
Beispiel #6
0
// cache writes structs and PropertyLoadSavers to memcache.
func cache(key []*datastore.Key, src interface{}, c appengine.Context) error {
	items, err := encodeItems(key, src)
	if len(items) > 0 && err == nil {
		if Debug {
			c.Debugf("writing to memcache: %#v", src)
		}
		err = memcache.SetMulti(c, items)
	}
	return err
}
Beispiel #7
0
func (c Context) Save() {
	c.Stats.wg.Wait()
	c.Stats.Duration = time.Since(c.Stats.Start)

	var buf_part, buf_full bytes.Buffer
	full := stats_full{
		Header: c.req.Header,
		Stats:  c.Stats,
	}
	if err := gob.NewEncoder(&buf_full).Encode(&full); err != nil {
		c.Errorf("appstats Save error: %v", err)
		return
	} else if buf_full.Len() > bufMaxLen {
		// first try clearing stack traces
		for i := range full.Stats.RPCStats {
			full.Stats.RPCStats[i].StackData = ""
		}
		buf_full.Truncate(0)
		gob.NewEncoder(&buf_full).Encode(&full)
	}
	part := stats_part(*c.Stats)
	for i := range part.RPCStats {
		part.RPCStats[i].StackData = ""
		part.RPCStats[i].In = ""
		part.RPCStats[i].Out = ""
	}
	if err := gob.NewEncoder(&buf_part).Encode(&part); err != nil {
		c.Errorf("appstats Save error: %v", err)
		return
	}

	item_part := &memcache.Item{
		Key:   c.Stats.PartKey(),
		Value: buf_part.Bytes(),
	}

	item_full := &memcache.Item{
		Key:   c.Stats.FullKey(),
		Value: buf_full.Bytes(),
	}

	c.Infof("Saved; %s: %s, %s: %s, link: %v",
		item_part.Key,
		ByteSize(len(item_part.Value)),
		item_full.Key,
		ByteSize(len(item_full.Value)),
		c.URL(),
	)

	nc := context(c.req)
	memcache.SetMulti(nc, []*memcache.Item{item_part, item_full})
}
Beispiel #8
0
func (ks *KodeSession) ToCache(c appengine.Context) error {
	pair := strings.Join(ks.Pair, DELIMITER)

	// Cache the session under both ids.
	items := []*memcache.Item{
		&memcache.Item{Key: ks.Candidate, Value: []byte(pair)},
		&memcache.Item{Key: ks.Interviewer, Value: []byte(pair)},
	}

	err := memcache.SetMulti(c, items)
	if err != nil {
		return err
	}

	return nil
}
Beispiel #9
0
func (d *Driver) SetMulti(keys []string, src interface{}) error {
	var items = make([]*memcache.Item, 0)
	srcValue := reflect.ValueOf(src)
	for i, key := range keys {
		obj := srcValue.Index(i).Interface()
		if buff, err := json.Marshal(obj); err != nil {
			return err
		} else {
			items = append(items, &memcache.Item{
				Key:   key,
				Value: buff,
			})
		}
	}
	return memcache.SetMulti(d.ctx, items)
}
Beispiel #10
0
func (u GAE) MemorySet(items *[]MemoryItem) error {
	if items == nil || len(*items) < 1 {
		return nil
	}

	gaeItems := make([]*memcache.Item, len(*items))
	for index, item := range *items {
		gaeItems[index] = &memcache.Item{
			Key:        item.Key,
			Value:      []byte(item.Value),
			Expiration: time.Duration(item.Ttl) * time.Second,
		}

		Verbosef(u, "GAE.MemorySet item[%d] = %v", index, item)
	}

	return memcache.SetMulti(u.context, gaeItems)
}
Beispiel #11
0
func (g *Goon) putMemcache(srcs []interface{}, exists []byte) error {
	items := make([]*memcache.Item, len(srcs))
	payloadSize := 0
	for i, src := range srcs {
		toSerialize := src
		if exists[i] == 0 {
			toSerialize = nil
		}
		data, err := serializeStruct(toSerialize)
		if err != nil {
			g.error(err)
			return err
		}
		key, _, err := g.getStructKey(src)
		if err != nil {
			return err
		}
		// payloadSize will overflow if we push 2+ gigs on a 32bit machine
		payloadSize += len(data)
		items[i] = &memcache.Item{
			Key:   memkey(key),
			Value: data,
		}
	}
	memcacheTimeout := MemcachePutTimeoutSmall
	if payloadSize >= MemcachePutTimeoutThreshold {
		memcacheTimeout = MemcachePutTimeoutLarge
	}
	errc := make(chan error)
	go func() {
		errc <- memcache.SetMulti(appengine.Timeout(g.Context, memcacheTimeout), items)
	}()
	g.putMemoryMulti(srcs, exists)
	err := <-errc
	if appengine.IsTimeoutError(err) {
		g.timeoutError(err)
		err = nil
	} else if err != nil {
		g.error(err)
	}
	return err
}