validity_map.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package collect
  2. import (
  3. "sync"
  4. "sync/atomic"
  5. "github.com/v2ray/v2ray-core/common"
  6. )
  7. type Validity interface {
  8. common.Releasable
  9. IsValid() bool
  10. }
  11. type ValidityMap struct {
  12. sync.RWMutex
  13. cache map[string]Validity
  14. opCount int32
  15. }
  16. func NewValidityMap(cleanupIntervalSec int) *ValidityMap {
  17. instance := &ValidityMap{
  18. cache: make(map[string]Validity),
  19. }
  20. return instance
  21. }
  22. func (this *ValidityMap) cleanup() {
  23. type entry struct {
  24. key string
  25. value Validity
  26. }
  27. entry2Remove := make([]entry, 0, 128)
  28. this.RLock()
  29. for key, value := range this.cache {
  30. if !value.IsValid() {
  31. entry2Remove = append(entry2Remove, entry{
  32. key: key,
  33. value: value,
  34. })
  35. }
  36. }
  37. this.RUnlock()
  38. for _, e := range entry2Remove {
  39. if !e.value.IsValid() {
  40. this.Lock()
  41. delete(this.cache, e.key)
  42. this.Unlock()
  43. }
  44. }
  45. }
  46. func (this *ValidityMap) Set(key string, value Validity) {
  47. this.Lock()
  48. this.cache[key] = value
  49. this.Unlock()
  50. opCount := atomic.AddInt32(&this.opCount, 1)
  51. if opCount > 1000 {
  52. atomic.StoreInt32(&this.opCount, 0)
  53. go this.cleanup()
  54. }
  55. }
  56. func (this *ValidityMap) Get(key string) Validity {
  57. this.RLock()
  58. defer this.RUnlock()
  59. if value, found := this.cache[key]; found {
  60. return value
  61. }
  62. return nil
  63. }