-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcache.go
46 lines (34 loc) · 899 Bytes
/
cache.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
package main
import (
"github.com/hashicorp/golang-lru"
fb "github.com/huandu/facebook"
)
type FacebookInfo struct {
Id string
Name string
}
var (
NameCache, _ = lru.New(256)
GlobalApp = fb.New(ClientId, ApiSecret)
)
func GetId(accessToken string) (string, error) {
if val, ok := NameCache.Get(accessToken); ok {
return val.(FacebookInfo).Id, nil
}
return exchangeAccessToken(accessToken)
}
func exchangeAccessToken(accessToken string) (string, error) {
session := GlobalApp.Session(accessToken)
err := session.Validate()
if err != nil {
return "", err
}
res, _ := session.Get("me?fields=id,name", nil)
var ret string = res.Get("id").(string)
var name string = res.Get("name").(string)
NameCache.Add(accessToken, FacebookInfo{ret, name})
return ret, nil
}
func SetId(accessToken, id string, name string) {
NameCache.Add(accessToken, FacebookInfo{id, name})
}