Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3b6151c

Browse files
committed
Cache tokens in memory
1 parentb7cea93 commit3b6151c

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

‎api/account_test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,13 @@ func TestCreateToken(t *testing.T) {
6464
assert.Nil(t,err)
6565
assert.Len(t,token,64)
6666
}
67+
68+
funcBenchmarkPing(b*testing.B) {
69+
r:=createRouter()
70+
req:=testRequest(r,"GET","/account/ping",nil)
71+
req.Header.Set("Authorization","token")
72+
73+
fori:=0;i<b.N;i++ {
74+
req.send()
75+
}
76+
}

‎api/api.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import (
1010
)
1111

1212
var (
13-
storeAPIStore
14-
crawl*crawler
15-
config*Config
13+
storeAPIStore
14+
authCache*memAuthCache
15+
crawl*crawler
16+
config*Config
1617
)
1718

1819
typeConfigstruct {
@@ -34,6 +35,7 @@ func Configure(cfg *Config) {
3435

3536
funcServe() {
3637
openStore(path.Join(config.Dir,"store"))
38+
authCache=newMemAuthCache()
3739

3840
crawl=newCrawler()
3941
crawl.start(config.MaxDownloadConnections)

‎api/api_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestMain(m *testing.M) {
2323
deferos.Remove(storePath)
2424

2525
openStore(storePath)
26+
authCache=newMemAuthCache()
2627
initTestData()
2728
deferstore.Close()
2829

‎api/auth.go‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package api
22

33
import (
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 {
1214
returnecho.NewHTTPError(401)
1315
}
1416

15-
user:=store.GetUserByToken(token)
17+
user:=authCache.get(token)
18+
ifuser!=nil {
19+
c.Set("user",user)
20+
returnnil
21+
}
22+
23+
user=store.GetUserByToken(token)
1624
ifuser==nil {
1725
returnecho.NewHTTPError(401)
1826
}
1927

28+
authCache.set(token,user)
2029
c.Set("user",user)
2130
}
2231

2332
returnnil
2433
}
2534
}
35+
36+
typememAuthCachestruct {
37+
usersmap[string]*User
38+
lock sync.Mutex
39+
}
40+
41+
funcnewMemAuthCache()*memAuthCache {
42+
return&memAuthCache{
43+
users:make(map[string]*User),
44+
}
45+
}
46+
47+
func (c*memAuthCache)get(tokenstring)*User {
48+
c.lock.Lock()
49+
user:=c.users[token]
50+
c.lock.Unlock()
51+
returnuser
52+
}
53+
54+
func (c*memAuthCache)set(tokenstring,user*User) {
55+
c.lock.Lock()
56+
c.users[token]=user
57+
c.lock.Unlock()
58+
}

‎api/auth_test.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,18 @@ func TestAuth(t *testing.T) {
3131
c.Request().URL.Path="/account/login"
3232
assert.Nil(t,mw(c))
3333
}
34+
35+
funcBenchmarkAuth(b*testing.B) {
36+
fori:=0;i<b.N;i++ {
37+
store.GetUserByToken("token")
38+
}
39+
}
40+
41+
funcBenchmarkAuthCache(b*testing.B) {
42+
cache:=newMemAuthCache()
43+
cache.set("token",store.GetUserByToken("token"))
44+
45+
fori:=0;i<b.N;i++ {
46+
cache.get("token")
47+
}
48+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp