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