github.com/wangyougui/gf/v2@v2.6.5/os/gcache/gcache_adapter_memory_expire_sets.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gcache
     8  
     9  import (
    10  	"sync"
    11  
    12  	"github.com/wangyougui/gf/v2/container/gset"
    13  )
    14  
    15  type adapterMemoryExpireSets struct {
    16  	mu         sync.RWMutex        // expireSetMu ensures the concurrent safety of expireSets map.
    17  	expireSets map[int64]*gset.Set // expireSets is the expiring timestamp to its key set mapping, which is used for quick indexing and deleting.
    18  }
    19  
    20  func newAdapterMemoryExpireSets() *adapterMemoryExpireSets {
    21  	return &adapterMemoryExpireSets{
    22  		expireSets: make(map[int64]*gset.Set),
    23  	}
    24  }
    25  
    26  func (d *adapterMemoryExpireSets) Get(key int64) (result *gset.Set) {
    27  	d.mu.RLock()
    28  	result = d.expireSets[key]
    29  	d.mu.RUnlock()
    30  	return
    31  }
    32  
    33  func (d *adapterMemoryExpireSets) GetOrNew(key int64) (result *gset.Set) {
    34  	if result = d.Get(key); result != nil {
    35  		return
    36  	}
    37  	d.mu.Lock()
    38  	if es, ok := d.expireSets[key]; ok {
    39  		result = es
    40  	} else {
    41  		result = gset.New(true)
    42  		d.expireSets[key] = result
    43  	}
    44  	d.mu.Unlock()
    45  	return
    46  }
    47  
    48  func (d *adapterMemoryExpireSets) Delete(key int64) {
    49  	d.mu.Lock()
    50  	delete(d.expireSets, key)
    51  	d.mu.Unlock()
    52  }