bitbucket.org/alima123/expires@v0.0.0-20191226173914-cddaf5cffb3d/cachemap.go (about)

     1  package expires
     2  
     3  import "sync"
     4  
     5  var (
     6  	GlobalCacheOpMap = CacheOpMap{}
     7  )
     8  
     9  type (
    10  	CacheOpMap struct {
    11  		cachePool sync.Map
    12  	}
    13  )
    14  
    15  func (cm *CacheOpMap) LazyInitCachePoolOp(op string) CacheUnit {
    16  	cacheItf, ok := cm.cachePool.Load(op)
    17  	if !ok {
    18  		cache := &cacheUnit{}
    19  		cm.cachePool.Store(op, cache)
    20  		return cache
    21  	}
    22  	return cacheItf.(CacheUnit)
    23  }
    24  
    25  // ClearInvalidate 清除已过期的数据(一般用不到)
    26  func (cm *CacheOpMap) ClearInvalidate() {
    27  	cm.cachePool.Range(func(_, cacheItf interface{}) bool {
    28  		cache := cacheItf.(CacheUnit)
    29  		cache.Range(func(key interface{}, exp DataExpires) bool {
    30  			if exp.IsExpires() {
    31  				cache.Delete(key)
    32  			}
    33  			return true
    34  		})
    35  		return true
    36  	})
    37  }
    38  
    39  // PrintAll 输出所有缓冲项目
    40  func (cm *CacheOpMap) PrintAll() {
    41  
    42  }