This commit is contained in:
Sergey Melnikov 2024-01-18 14:31:25 +03:00
commit d191055c2d
3 changed files with 66 additions and 0 deletions

62
abstract/abstract.go Normal file
View File

@ -0,0 +1,62 @@
package abstract
import (
"sync"
"sync/atomic"
)
type IStore[K comparable, T any] interface {
Load(K) (T, bool)
Delete(K)
Store(K, T)
GetData() map[K]T
GetUID() int64
NewUID(int64) int64
Lock()
Unlock()
RLock()
RUnlock()
}
type Store[K comparable, V any] struct {
Data map[K]V
UID atomic.Int64
sync.RWMutex
}
func (c *Store[K, V]) GetUID() int64 {
return c.UID.Load()
}
func (c *Store[K, V]) NewUID(i int64) int64 {
return c.UID.Add(i)
}
func (c *Store[K, V]) GetData() map[K]V {
return c.Data
}
func (c *Store[K, V]) Load(key K) (V, bool) {
c.RLock()
if d, ok := c.Data[key]; ok {
c.RUnlock()
return d, true
}
c.RUnlock()
var empty V
return empty, false
}
func (c *Store[K, V]) Store(key K, d V) {
c.Lock()
c.Data[key] = d
c.Unlock()
}
func (c *Store[K, V]) Delete(key K) {
c.Lock()
delete(c.Data, key)
c.Unlock()
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module custom
go 1.21.6

1
main.go Normal file
View File

@ -0,0 +1 @@
package main