示例#1
0
func (this *FunServantImpl) LcGet(ctx *rpc.Context, key string) (r []byte,
	miss *rpc.TCacheMissed, ex error) {
	const IDENT = "lc.get"

	svtStats.inc(IDENT)

	profiler, err := this.getSession(ctx).startProfiler()
	if err != nil {
		ex = err
		return
	}

	result, ok := this.lc.Get(key)
	if !ok {
		miss = rpc.NewTCacheMissed()
		miss.Message = thrift.StringPtr("lcache missed: " + key) // optional
	} else {
		r = result.([]byte)
	}

	profiler.do(IDENT, ctx,
		"{key^%s} {miss^%v r^%s}", key, miss, r)

	return
}
示例#2
0
func (this *FunServantImpl) McGet(ctx *rpc.Context, pool string,
	key string) (r *rpc.TMemcacheData,
	miss *rpc.TCacheMissed, ex error) {
	const IDENT = "mc.get"

	if this.mc == nil {
		ex = ErrServantNotStarted
		return
	}

	svtStats.inc(IDENT)

	profiler, err := this.getSession(ctx).startProfiler()
	if err != nil {
		ex = err
		return
	}

	it, err := this.mc.Get(pool, key)
	if err == nil {
		// cache hit
		r = rpc.NewTMemcacheData()
		r.Data = it.Value
		r.Flags = int32(it.Flags)
	} else if err == memcache.ErrCacheMiss {
		// cache miss
		miss = rpc.NewTCacheMissed()
		miss.Message = thrift.StringPtr(err.Error()) // optional
	} else {
		ex = err
		log.Error("Q=%s %s {key^%s}: %v", IDENT, ctx.String(), key, err)
	}

	profiler.do(IDENT, ctx,
		"{key^%s} {miss^%v val^%s}",
		key,
		miss,
		r)

	return
}
示例#3
0
func (this *FunServantImpl) MgFindOne(ctx *rpc.Context,
	pool string, table string, shardId int32,
	query []byte, fields []byte) (r []byte,
	miss *rpc.TMongoNotFound, ex error) {
	const IDENT = "mg.findOne"

	if this.mg == nil {
		ex = ErrServantNotStarted
		return
	}

	svtStats.inc(IDENT)

	profiler, err := this.getSession(ctx).startProfiler()
	if err != nil {
		ex = err
		return
	}

	// get mongodb session
	sess, err := this.mongoSession(pool, shardId)
	if err != nil {
		ex = err
		return
	}
	defer sess.Recyle(&err)

	bsonQuery, err := mongo.UnmarshalIn(query)
	if err != nil {
		ex = err
		return
	}
	var bsonFields bson.M
	if !mongo.FieldsIsNil(fields) {
		bsonFields, err = mongo.UnmarshalIn(fields)
		if err != nil {
			ex = err
			return
		}
	}

	var result bson.M
	q := sess.DB().C(table).Find(bsonQuery)
	if !mongo.FieldsIsNil(fields) {
		q.Select(bsonFields)
	}
	err = q.One(&result)
	if err != nil {
		if err != mgo.ErrNotFound {
			log.Error("%s: %v", IDENT, err)
		} else {
			miss = rpc.NewTMongoNotFound()
			miss.Message = thrift.StringPtr(err.Error())
			profiler.do(IDENT, ctx,
				"{pool^%s table^%s query^%v fields^%v} {miss^%v err^%v val^%v}",
				pool, table,
				bsonQuery,
				bsonFields,
				miss,
				ex,
				result)
			return
		}

		ex = err
		return
	}

	r = mongo.MarshalOut(result)

	profiler.do(IDENT, ctx,
		"{pool^%s table^%s query^%v fields^%v} {miss^%v err^%v val^%v}",
		pool, table,
		bsonQuery,
		bsonFields,
		miss,
		ex,
		result)

	return
}