-
Notifications
You must be signed in to change notification settings - Fork 6
/
item_collaborative_filter_recall.go
72 lines (67 loc) · 2.21 KB
/
item_collaborative_filter_recall.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package recall
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/alibaba/pairec/v2/context"
"github.com/alibaba/pairec/v2/log"
"github.com/alibaba/pairec/v2/module"
"github.com/alibaba/pairec/v2/recconf"
"github.com/alibaba/pairec/v2/utils"
)
type ItemCollaborativeFilterRecall struct {
*BaseRecall
itemCollaborativeDao module.ItemCollaborativeDao
}
func NewItemCollaborativeFilterRecall(config recconf.RecallConfig) *ItemCollaborativeFilterRecall {
recall := &ItemCollaborativeFilterRecall{
BaseRecall: NewBaseRecall(config),
itemCollaborativeDao: module.NewItemCollaborativeDao(config),
}
return recall
}
func (r *ItemCollaborativeFilterRecall) GetCandidateItems(user *module.User, context *context.RecommendContext) (ret []*module.Item) {
start := time.Now()
if r.cache != nil {
key := r.cachePrefix + string(user.Id)
cacheRet := r.cache.Get(key)
if itemStr, ok := cacheRet.([]uint8); ok {
itemIds := strings.Split(string(itemStr), ",")
for _, id := range itemIds {
var item *module.Item
if strings.Contains(id, ":") {
vars := strings.Split(id, ":")
item = module.NewItem(vars[0])
f, _ := strconv.ParseFloat(vars[1], 64)
item.Score = f
} else {
item = module.NewItem(id)
}
item.RetrieveId = r.modelName
item.ItemType = r.itemType
ret = append(ret, item)
}
log.Info(fmt.Sprintf("requestId=%s\tmodule=ItemCollaborativeFilterRecall\tcount=%d\tcost=%d", context.RecommendId, len(ret), utils.CostTime(start)))
return
}
}
ret = r.itemCollaborativeDao.ListItemsByItem(user, context)
if r.cache != nil && len(ret) > 0 {
go func() {
key := r.cachePrefix + string(user.Id)
var itemIds string
for _, item := range ret {
//item.AddProperty(r.modelName, 0)
itemIds += fmt.Sprintf("%s:%v", string(item.Id), item.Score) + ","
}
itemIds = itemIds[:len(itemIds)-1]
if err := r.cache.Put(key, itemIds, 1800*time.Second); err != nil {
log.Error(fmt.Sprintf("requestId=%s\tmodule=ItemCollaborativeFilterRecall\terror=%v",
context.RecommendId, err))
}
}()
}
log.Info(fmt.Sprintf("requestId=%s\tmodule=ItemCollaborativeFilterRecall\tcount=%d\tcost=%d", context.RecommendId, len(ret), utils.CostTime(start)))
return
}