本文整理汇总了Golang中github.com/zond/god/client.Conn.Slice方法的典型用法代码示例。如果您正苦于以下问题:Golang Conn.Slice方法的具体用法?Golang Conn.Slice怎么用?Golang Conn.Slice使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/zond/god/client.Conn
的用法示例。
在下文中一共展示了Conn.Slice方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getActives
func getActives(w http.ResponseWriter, r *http.Request, c *client.Conn) {
var result []common.Message
for _, item := range c.Slice(activeObjectsKey, nil, nil, true, true) {
result = append(result, common.Message{
Object: string(item.Key),
})
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err := json.NewEncoder(w).Encode(result); err != nil {
panic(err)
}
}
示例2: getViews
func getViews(w http.ResponseWriter, r *http.Request, c *client.Conn) {
uid := mux.Vars(r)["user_id"]
vKey := uViewsKey(uid)
var result []common.Message
for _, item := range c.Slice(vKey, nil, nil, true, true) {
result = append(result, common.Message{
Type: common.View,
User: uid,
Object: string(item.Key),
})
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err := json.NewEncoder(w).Encode(result); err != nil {
panic(err)
}
}
示例3: slice
func slice(conn *client.Conn, args []string) {
for i, item := range conn.Slice([]byte(args[1]), []byte(args[2]), []byte(args[3]), true, false) {
fmt.Printf("%v: %v => %v\n", i, string(item.Key), decode(item.Value))
}
}
示例4: getRecommendations
func getRecommendations(w http.ResponseWriter, r *http.Request, c *client.Conn) {
uid := mux.Vars(r)["user_id"]
var request common.RecommendationsRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
panic(err)
}
// Create a set operation that returns the union of the likers of all objects we have liked, just returning the user key
likersOp := &setop.SetOp{
Merge: setop.First,
Type: setop.Union,
}
// For each object we have liked, add the likers of that flavor as a source to the union of likers
for _, obj := range c.Slice(uLikesKey(uid), nil, nil, true, true) {
likersOp.Sources = append(likersOp.Sources, setop.SetOpSource{Key: oLikesKey(string(obj.Key))})
}
// Create a set operation that returns the union of all things liked by all likers of objects we liked, returning the sum of the likes for each object
objectsOp := &setop.SetOp{
Merge: setop.FloatSum,
Type: setop.Union,
}
// For each user in the union of users having liked something we like
for _, user := range c.SetExpression(setop.SetExpression{
Op: likersOp,
}) {
// If the user is not us
if string(user.Key) != uid {
// Fetch the number of objects we have both liked
similarity := len(c.SetExpression(setop.SetExpression{
Code: fmt.Sprintf("(I:First %v %v)", string(uLikesKey(string(user.Key))), string(uLikesKey(uid))),
}))
// And weight the user according to how many commonalities we have
weight := math.Log(float64(similarity + 1))
// Add the objects liked by this user, weighed this much, as a source
objectsOp.Sources = append(objectsOp.Sources, setop.SetOpSource{Key: uLikesKey(string(user.Key)), Weight: &weight})
}
}
// If we want to filter on active objects
if request.Actives != "" {
// Create an operation on the simple Union and the active objects
objectsOp = &setop.SetOp{
Merge: setop.First,
Sources: []setop.SetOpSource{
setop.SetOpSource{
SetOp: objectsOp,
},
setop.SetOpSource{
Key: activeObjectsKey,
},
},
}
// And make it of the correct type
if request.Actives == common.Reject {
objectsOp.Type = setop.Difference
} else if request.Actives == common.Intersect {
objectsOp.Type = setop.Intersection
}
}
// If we want to filter on viewed objects
if request.Viewed != "" {
// Create an operation on the previous operation and the viewed objects
objectsOp = &setop.SetOp{
Merge: setop.First,
Sources: []setop.SetOpSource{
setop.SetOpSource{
SetOp: objectsOp,
},
setop.SetOpSource{
Key: uViewsKey(uid),
},
},
}
// And make it of the correct type
if request.Viewed == common.Reject {
objectsOp.Type = setop.Difference
} else if request.Viewed == common.Intersect {
objectsOp.Type = setop.Intersection
}
}
// Finally, fetch the wanted number of recommendations
var result []common.Message
for _, item := range c.SetExpression(setop.SetExpression{
Op: objectsOp,
}) {
w := godCommon.MustDecodeFloat64(item.Values[0])
i := sort.Search(len(result), func(i int) bool {
return w > result[i].Weight
})
if i < len(result) {
if len(result) < request.Num {
result = append(result[:i], append([]common.Message{common.Message{
Object: string(item.Key),
Weight: w,
}}, result[i:]...)...)
} else {
if i > 0 {
result = append(result[:i], append([]common.Message{common.Message{
Object: string(item.Key),
Weight: w,
}}, result[i:len(result)-1]...)...)
} else {
//.........这里部分代码省略.........