-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestmemCache.go
More file actions
118 lines (84 loc) · 2.35 KB
/
testmemCache.go
File metadata and controls
118 lines (84 loc) · 2.35 KB
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
109
110
111
112
113
114
115
116
117
118
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/UCMAndesLab/gosMAP"
"github.com/bradfitz/gomemcache/memcache"
"github.com/gin-gonic/gin"
"html/template"
//"log"
"net/http"
//"time"
"strings"
)
var apikey string = "rU3eqtaE4zBSzZKjoUS9Q7fVPbTmKmD2eOUr"
type Page struct {
Title string
ReadData gosMAP.Data
}
func viewHandler(c *gin.Context) {
conn, err := gosMAP.Connect("http://mercury:8079", apikey)
conn.ConnectMemcache("localhost:11211")
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
}
buidling := c.Request.URL.Path[len("/view/"):]
key := strings.Replace(buidling, " ", "\\", -1)
item, err := conn.Mc.Get(key)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
var query []string
err = json.Unmarshal(item.Value, &query)
fmt.Println(query[0])
d, err := conn.Get(query[0], 0, 0, 10)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
}
fmt.Fprintf(c.Writer, "<h1>%s</h1>", d)
}
func queryHandler(c *gin.Context) {
t, _ := template.ParseFiles("query.html")
t.Execute(c.Writer, nil)
}
func saveHandler(c *gin.Context) {
building := c.Request.FormValue("query")
key := strings.Replace(building, " ", "\\", -1)
conn, err := gosMAP.Connect("http://mercury:8079", apikey)
conn.ConnectMemcache("localhost:11211")
if err != nil {
c.AbortWithError(http.StatusInternalServerError, errors.New("Cannot connect to mercury"))
}
_, err = conn.Mc.Get(key)
if err != nil {
/*c.AbortWithError(http.StatusInternalServerError, errors.New("Cache miss"))
return*/
q, _ := conn.QueryList(fmt.Sprintf("select distinct uuid where Metadata/Location/Building = '%s'", building))
b, err := json.Marshal(q)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, errors.New("Cannot marshal"))
return
}
item := memcache.Item{Key: key, Value: b, Expiration: 0}
err = conn.Mc.Add(&item)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.Redirect(http.StatusFound, "/view/"+building)
} else {
c.Redirect(http.StatusFound, "/view/"+building)
}
}
func main() {
router := gin.Default()
router.GET("/", func(c *gin.Context) {
c.HTML(200, "index.html", nil)
})
router.GET("/view/:key", viewHandler)
router.GET("/query/", queryHandler)
router.POST("/save/", saveHandler)
router.Run(":8080")
}