本站源代码
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

59 行
1.2KB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package openid
  5. import (
  6. "sync"
  7. "time"
  8. "github.com/yohcop/openid-go"
  9. )
  10. type timedDiscoveredInfo struct {
  11. info openid.DiscoveredInfo
  12. time time.Time
  13. }
  14. type timedDiscoveryCache struct {
  15. cache map[string]timedDiscoveredInfo
  16. ttl time.Duration
  17. mutex *sync.Mutex
  18. }
  19. func newTimedDiscoveryCache(ttl time.Duration) *timedDiscoveryCache {
  20. return &timedDiscoveryCache{cache: map[string]timedDiscoveredInfo{}, ttl: ttl, mutex: &sync.Mutex{}}
  21. }
  22. func (s *timedDiscoveryCache) Put(id string, info openid.DiscoveredInfo) {
  23. s.mutex.Lock()
  24. defer s.mutex.Unlock()
  25. s.cache[id] = timedDiscoveredInfo{info: info, time: time.Now()}
  26. }
  27. // Delete timed-out cache entries
  28. func (s *timedDiscoveryCache) cleanTimedOut() {
  29. now := time.Now()
  30. for k, e := range s.cache {
  31. diff := now.Sub(e.time)
  32. if diff > s.ttl {
  33. delete(s.cache, k)
  34. }
  35. }
  36. }
  37. func (s *timedDiscoveryCache) Get(id string) openid.DiscoveredInfo {
  38. s.mutex.Lock()
  39. defer s.mutex.Unlock()
  40. // Delete old cached while we are at it.
  41. s.cleanTimedOut()
  42. if info, has := s.cache[id]; has {
  43. return info.info
  44. }
  45. return nil
  46. }
上海开阖软件有限公司 沪ICP备12045867号-1