github.com/saintwish/kv@v1.0.4/kvswiss/kvswiss.go (about)

     1  package kvswiss
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  
     7  	"github.com/saintwish/kv/swiss"
     8  )
     9  
    10  type Cache[K comparable, V any] struct {
    11  	Map *swiss.Map[K, V]
    12  	OnEvicted func(K, V) //function that's called when cached item is deleted.
    13  
    14  	*sync.RWMutex //mutex
    15  }
    16  
    17  func New[K comparable, V any](size uint64) *Cache[K, V] {
    18  	return &Cache[K, V] {
    19  		Map: swiss.NewMap[K, V]( uint32(size) ),
    20  		RWMutex: &sync.RWMutex{},
    21  	}
    22  }
    23  
    24  func (c *Cache[K, V]) SetOnEvicted(f func(K, V)) {
    25  	c.OnEvicted = f
    26  }
    27  
    28  // Sets the key with value, will overwrite if key exists
    29  func (c *Cache[K, V]) Get(key K) (data V) {
    30  	c.RLock()
    31  
    32  	data = c.Map.Get(key)
    33  
    34  	c.RUnlock()
    35  	return
    36  }
    37  
    38  func (c *Cache[K, V]) GetHas(key K) (data V, ok bool) {
    39  	c.RLock()
    40  
    41  	ok, data = c.Map.GetHas(key);
    42  
    43  	c.RUnlock()
    44  	return
    45  }
    46  
    47  func (c *Cache[K, V]) Has(key K) (ok bool) {
    48  	c.RLock()
    49  
    50  	ok = c.Map.Has(key)
    51  
    52  	c.RUnlock()
    53  	return
    54  }
    55  
    56  func (c *Cache[K, V]) Set(key K, val V) {
    57  	c.Lock()
    58  
    59  	c.Map.Set(key, val)
    60  
    61  	c.Unlock()
    62  }
    63  
    64  // Adds key with value to map, will error if key already exists.
    65  func (c *Cache[K, V]) Add(key K, val V) error {
    66  	if c.Has(key) {
    67  		return fmt.Errorf("ccmap: Data already exists with given key %T", key)
    68  	}
    69  	
    70  	c.Set(key, val)
    71  
    72  	return nil
    73  }
    74  
    75  // Updates given key, errors if key doesn't already exists.
    76  func (c *Cache[K, V]) Update(key K, val V) error {
    77  	if !c.Has(key) {
    78  		return fmt.Errorf("ccmap: Data doesn't exists with given key %T", key)
    79  	}
    80  
    81  	c.Set(key, val)
    82  
    83  	return nil
    84  }
    85  
    86  func (c *Cache[K, V]) Delete(key K) {
    87  	if c.Has(key) {
    88  		c.Map.Delete(key)
    89  	}
    90  }
    91  
    92  // Gets the max size of the cache before the internal map needs to be resized.
    93  func (c *Cache[K, V]) GetMaxSize(key K) int {
    94  	return c.Map.MaxCapacity()
    95  }
    96  
    97  // Gets the current capacity of the cache.
    98  func (c *Cache[K, V]) GetCapacity(key K) int {
    99  	return c.Map.Capacity()
   100  }
   101  
   102  // Clears the map of everything, but doesn't call OnEvicted.
   103  func (c *Cache[K, V]) Clear() {
   104  	c.Map.Clear()
   105  }
   106  
   107  // Same as Clear but calls OnEvicted for each object before it's deleted.
   108  func (c *Cache[K, V]) Flush() {
   109  	c.Lock()
   110  
   111  	c.Map.Iter(func (key K, val V) (stop bool) {
   112  		c.OnEvicted(key, val)
   113  		c.Map.Delete(key)
   114  		
   115  		if stop {
   116  			c.Unlock()
   117  			return
   118  		}
   119  
   120  		return
   121  	})
   122  
   123  	c.Unlock()
   124  }
   125  
   126  /*
   127  TODO:
   128  
   129  func (c *Cache[K, V]) LoadFromJSON(b []byte) (err error) {
   130  	c.Lock()
   131  
   132  	err = json.Unmarshal(b, &c.Map)
   133  
   134  	c.Unlock()
   135  	return
   136  }
   137  */