github.com/gogf/gf@v1.16.9/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/gogf/gf.
     6  
     7  package gcache
     8  
     9  import (
    10  	"github.com/gogf/gf/container/gset"
    11  	"sync"
    12  )
    13  
    14  type adapterMemoryExpireSets struct {
    15  	mu         sync.RWMutex        // expireSetMu ensures the concurrent safety of expireSets map.
    16  	expireSets map[int64]*gset.Set // expireSets is the expiring timestamp to its key set mapping, which is used for quick indexing and deleting.
    17  }
    18  
    19  func newAdapterMemoryExpireSets() *adapterMemoryExpireSets {
    20  	return &adapterMemoryExpireSets{
    21  		expireSets: make(map[int64]*gset.Set),
    22  	}
    23  }
    24  
    25  func (d *adapterMemoryExpireSets) Get(key int64) (result *gset.Set) {
    26  	d.mu.RLock()
    27  	result = d.expireSets[key]
    28  	d.mu.RUnlock()
    29  	return
    30  }
    31  
    32  func (d *adapterMemoryExpireSets) GetOrNew(key int64) (result *gset.Set) {
    33  	if result = d.Get(key); result != nil {
    34  		return
    35  	}
    36  	d.mu.Lock()
    37  	if es, ok := d.expireSets[key]; ok {
    38  		result = es
    39  	} else {
    40  		result = gset.New(true)
    41  		d.expireSets[key] = result
    42  	}
    43  	d.mu.Unlock()
    44  	return
    45  }
    46  
    47  func (d *adapterMemoryExpireSets) Delete(key int64) {
    48  	d.mu.Lock()
    49  	delete(d.expireSets, key)
    50  	d.mu.Unlock()
    51  }