From d191055c2d10ae4ffbd602c1a13ccdc104ce3636 Mon Sep 17 00:00:00 2001 From: Sergey Melnikov Date: Thu, 18 Jan 2024 14:31:25 +0300 Subject: [PATCH] init --- abstract/abstract.go | 62 ++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++ main.go | 1 + 3 files changed, 66 insertions(+) create mode 100644 abstract/abstract.go create mode 100644 go.mod create mode 100644 main.go diff --git a/abstract/abstract.go b/abstract/abstract.go new file mode 100644 index 0000000..3155b1a --- /dev/null +++ b/abstract/abstract.go @@ -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() +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7e0b753 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module custom + +go 1.21.6 diff --git a/main.go b/main.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/main.go @@ -0,0 +1 @@ +package main