/
main.go
108 lines (83 loc) · 1.88 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"github.com/hjr265/too"
"log"
"net"
)
type Like struct {
user too.User
show too.Item
}
type Client struct {
// Public access
Like chan Like
Suggestion chan too.User
Close chan bool
// Private access
connection *too.Engine
}
const NUM_RECOMMENDATIONS = 3
var redisConnection *too.Engine = nil
func main() {
c := NewClient()
// Add sample data in the database
addSampleData(c.Like)
// Get suggestions:
fmt.Println("Suggestions:")
getSuggestions(c.Suggestion, "Albert")
}
func NewClient() Client {
redisAddr, _ := net.ResolveTCPAddr("tcp", ":6379")
conn, err := too.New(redisAddr, "tvshows")
if err != nil {
log.Fatal(err)
}
client := Client{make(chan Like), make(chan too.User), make(chan bool), conn}
go client.run()
return client
}
func (c *Client) run() {
for {
select {
case like := <-c.Like:
fmt.Println("Adding recommendation", like.user, like.show)
c.connection.Likes.Add(like.user, like.show)
case user := <-c.Suggestion:
fmt.Println("Suggesting")
items, _ := c.connection.Suggestions.For(user, NUM_RECOMMENDATIONS)
for _, item := range items {
fmt.Println(item)
}
case <-c.Close:
return
}
}
}
func getSuggestions(c chan too.User, user too.User) {
c <- user
}
func addSampleData(c chan Like) {
likes := []Like{
{"Albert", "Game of Thrones"},
{"Albert", "The Big Bang Theory"},
{"Albert", "Fargo"},
{"Albert", "How I Met Your Mother"},
{"Albert", "Breaking Bad"},
{"Albert", "The Strain"},
{"Albert", "Six Feet Under"},
{"Noemí", "Grey's Anatomy"},
{"Noemí", "The Good Wife"},
{"Noemí", "Game of Thrones"},
{"Noemí", "The Strain"},
{"Pepe", "Grey's Anatomy"},
{"Pepe", "Six Feet Under"},
{"Pepe", "How I Met Your Mother"},
{"Paco", "The Good Wife"},
{"Paco", "Game of Thrones"},
{"Paco", "Fargo"},
}
for _, like := range likes {
c <- like
}
}