| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"sync" |
| 5 |
|
| 6 |
"github.com/henrybear327/go-proton-api" |
| 7 |
) |
| 8 |
|
| 9 |
func NewAuthCache() AuthCacher { |
| 10 |
return &authCache{ |
| 11 |
info: make(map[string]proton.AuthInfo), |
| 12 |
auth: make(map[string]proton.Auth), |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
type authCache struct { |
| 17 |
info map[string]proton.AuthInfo |
| 18 |
auth map[string]proton.Auth |
| 19 |
lock sync.RWMutex |
| 20 |
} |
| 21 |
|
| 22 |
func (c *authCache) GetAuthInfo(username string) (proton.AuthInfo, bool) { |
| 23 |
c.lock.RLock() |
| 24 |
defer c.lock.RUnlock() |
| 25 |
|
| 26 |
info, ok := c.info[username] |
| 27 |
|
| 28 |
return info, ok |
| 29 |
} |
| 30 |
|
| 31 |
func (c *authCache) SetAuthInfo(username string, info proton.AuthInfo) { |
| 32 |
c.lock.Lock() |
| 33 |
defer c.lock.Unlock() |
| 34 |
|
| 35 |
c.info[username] = info |
| 36 |
} |
| 37 |
|
| 38 |
func (c *authCache) GetAuth(username string) (proton.Auth, bool) { |
| 39 |
c.lock.RLock() |
| 40 |
defer c.lock.RUnlock() |
| 41 |
|
| 42 |
auth, ok := c.auth[username] |
| 43 |
|
| 44 |
return auth, ok |
| 45 |
} |
| 46 |
|
| 47 |
func (c *authCache) SetAuth(username string, auth proton.Auth) { |
| 48 |
c.lock.Lock() |
| 49 |
defer c.lock.Unlock() |
| 50 |
|
| 51 |
c.auth[username] = auth |
| 52 |
} |