github.com/sandwich-go/boost@v1.3.29/xtime/gen_WheelMap.go (about)

     1  // Code generated by gotemplate. DO NOT EDIT.
     2  
     3  package xtime
     4  
     5  import (
     6  	"sort"
     7  	"sync"
     8  	"time"
     9  )
    10  
    11  //template type SyncMap(KType,VType)
    12  
    13  type WheelMap struct {
    14  	sm     sync.Map
    15  	locker sync.RWMutex
    16  }
    17  
    18  func NewWheelMap() *WheelMap {
    19  	return &WheelMap{}
    20  }
    21  
    22  func (s *WheelMap) Keys() (ret []time.Duration) {
    23  	s.sm.Range(func(key, value interface{}) bool {
    24  		ret = append(ret, key.(time.Duration))
    25  		return true
    26  	})
    27  	return ret
    28  }
    29  
    30  func (s *WheelMap) Len() (c int) {
    31  	s.sm.Range(func(key, value interface{}) bool {
    32  		c++
    33  		return true
    34  	})
    35  	return c
    36  }
    37  
    38  func (s *WheelMap) Contains(key time.Duration) (ok bool) {
    39  	_, ok = s.Load(key)
    40  	return
    41  }
    42  
    43  func (s *WheelMap) Get(key time.Duration) (value *Wheel) {
    44  	value, _ = s.Load(key)
    45  	return
    46  }
    47  
    48  func (s *WheelMap) Load(key time.Duration) (value *Wheel, loaded bool) {
    49  	if v, ok := s.sm.Load(key); ok {
    50  		return v.(*Wheel), true
    51  	}
    52  	return
    53  }
    54  func (s *WheelMap) DeleteMultiple(keys ...time.Duration) {
    55  	for _, k := range keys {
    56  		s.sm.Delete(k)
    57  	}
    58  }
    59  func (s *WheelMap) Clear() {
    60  	s.sm.Range(func(key, value interface{}) bool {
    61  		s.sm.Delete(key)
    62  		return true
    63  	})
    64  }
    65  func (s *WheelMap) Delete(key time.Duration)            { s.sm.Delete(key) }
    66  func (s *WheelMap) Store(key time.Duration, val *Wheel) { s.sm.Store(key, val) }
    67  func (s *WheelMap) LoadAndDelete(key time.Duration) (value *Wheel, loaded bool) {
    68  	if v, ok := s.sm.LoadAndDelete(key); ok {
    69  		return v.(*Wheel), true
    70  	}
    71  	return
    72  }
    73  func (s *WheelMap) GetOrSetFuncErrorLock(key time.Duration, cf func(key time.Duration) (*Wheel, error)) (value *Wheel, loaded bool, err error) {
    74  	return s.LoadOrStoreFuncErrorLock(key, cf)
    75  }
    76  
    77  func (s *WheelMap) LoadOrStoreFuncErrorLock(key time.Duration, cf func(key time.Duration) (*Wheel, error)) (value *Wheel, loaded bool, err error) {
    78  	if v, ok := s.Load(key); ok {
    79  		return v, true, nil
    80  	}
    81  	s.locker.Lock()
    82  	defer s.locker.Unlock()
    83  	// 再次重试,如果获取到则直接返回
    84  	if v, ok := s.Load(key); ok {
    85  		return v, true, nil
    86  	}
    87  	value, err = cf(key)
    88  	if err != nil {
    89  		return value, false, err
    90  	}
    91  	s.Store(key, value)
    92  	return value, false, nil
    93  }
    94  
    95  func (s *WheelMap) GetOrSetFuncLock(key time.Duration, cf func(key time.Duration) *Wheel) (value *Wheel, loaded bool) {
    96  	return s.LoadOrStoreFuncLock(key, cf)
    97  }
    98  
    99  func (s *WheelMap) LoadOrStoreFuncLock(key time.Duration, cf func(key time.Duration) *Wheel) (value *Wheel, loaded bool) {
   100  	value, loaded, _ = s.LoadOrStoreFuncErrorLock(key, func(key time.Duration) (*Wheel, error) {
   101  		return cf(key), nil
   102  	})
   103  	return value, loaded
   104  }
   105  
   106  func (s *WheelMap) LoadOrStore(key time.Duration, val *Wheel) (*Wheel, bool) {
   107  	actual, ok := s.sm.LoadOrStore(key, val)
   108  	return actual.(*Wheel), ok
   109  }
   110  
   111  func (s *WheelMap) Range(f func(key time.Duration, value *Wheel) bool) {
   112  	s.sm.Range(func(k, v interface{}) bool {
   113  		return f(k.(time.Duration), v.(*Wheel))
   114  	})
   115  }
   116  
   117  func (s *WheelMap) RangeDeterministic(f func(key time.Duration, value *Wheel) bool, sortableGetter func([]time.Duration) sort.Interface) {
   118  	var keys []time.Duration
   119  	s.sm.Range(func(key, value interface{}) bool {
   120  		keys = append(keys, key.(time.Duration))
   121  		return true
   122  	})
   123  	sort.Sort(sortableGetter(keys))
   124  	for _, k := range keys {
   125  		if v, ok := s.Load(k); ok {
   126  			if !f(k, v) {
   127  				break
   128  			}
   129  		}
   130  	}
   131  }