11package api
22
33import (
4+ "sync"
5+
46"github.com/Castcloud/castcloud-go-server/Godeps/_workspace/src/github.com/labstack/echo"
57)
68
@@ -12,14 +14,45 @@ func auth() echo.HandlerFunc {
1214return echo .NewHTTPError (401 )
1315}
1416
15- user := store .GetUserByToken (token )
17+ user := authCache .get (token )
18+ if user != nil {
19+ c .Set ("user" ,user )
20+ return nil
21+ }
22+
23+ user = store .GetUserByToken (token )
1624if user == nil {
1725return echo .NewHTTPError (401 )
1826}
1927
28+ authCache .set (token ,user )
2029c .Set ("user" ,user )
2130}
2231
2332return nil
2433}
2534}
35+
36+ type memAuthCache struct {
37+ users map [string ]* User
38+ lock sync.Mutex
39+ }
40+
41+ func newMemAuthCache ()* memAuthCache {
42+ return & memAuthCache {
43+ users :make (map [string ]* User ),
44+ }
45+ }
46+
47+ func (c * memAuthCache )get (token string )* User {
48+ c .lock .Lock ()
49+ user := c .users [token ]
50+ c .lock .Unlock ()
51+ return user
52+ }
53+
54+ func (c * memAuthCache )set (token string ,user * User ) {
55+ c .lock .Lock ()
56+ c .users [token ]= user
57+ c .lock .Unlock ()
58+ }