| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package permlib
- import (
- "sync"
- "time"
- "golang.org/x/sync/singleflight"
- )
- type cachedUser struct {
- UserId int64
- Username string
- ProductCode string
- MemberType string
- Perms []string
- expiresAt time.Time
- }
- type permCache struct {
- store sync.Map
- ttl time.Duration
- sf singleflight.Group
- }
- func newPermCache(ttl time.Duration) *permCache {
- return &permCache{ttl: ttl}
- }
- func (c *permCache) get(key string) (*cachedUser, bool) {
- v, ok := c.store.Load(key)
- if !ok {
- return nil, false
- }
- entry := v.(*cachedUser)
- if time.Now().After(entry.expiresAt) {
- c.store.Delete(key)
- return nil, false
- }
- return entry, true
- }
- func (c *permCache) set(key string, u *cachedUser) {
- u.expiresAt = time.Now().Add(c.ttl)
- c.store.Store(key, u)
- }
|