diff options
| author | 2025-08-26 00:39:22 +0700 | |
|---|---|---|
| committer | 2025-08-26 00:39:22 +0700 | |
| commit | 5ed2499a1d8806fe78369b5b492d7990218131c9 (patch) | |
| tree | 0a9e0548aa145986b654af81554f16c8cf544c41 /internal/router/client-cache.go | |
| parent | b815b74d7a943d1f71a0ddcf6b343d3958d2b97a (diff) | |
| download | web-5ed2499a1d8806fe78369b5b492d7990218131c9.tar.gz web-5ed2499a1d8806fe78369b5b492d7990218131c9.zip | |
feat: client hash cache
feat: add local config
feat: create a volume for data (for the future)
format: unname unused vars
Diffstat (limited to 'internal/router/client-cache.go')
| -rw-r--r-- | internal/router/client-cache.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/internal/router/client-cache.go b/internal/router/client-cache.go new file mode 100644 index 0000000..192d2ed --- /dev/null +++ b/internal/router/client-cache.go @@ -0,0 +1,46 @@ +package router + +import ( + "log/slog" + "sync" + + "golang.org/x/crypto/argon2" +) + +type ClientCache struct { + hashMap map[string][]byte + mutexMap map[string]*sync.Mutex + salt []byte +} + +var CCache *ClientCache + +func NewClientCache(salt []byte) *ClientCache { + return &ClientCache{ + hashMap: make(map[string][]byte), + mutexMap: make(map[string]*sync.Mutex), + salt: salt, + } +} + +func (c *ClientCache) GetHash(id string) []byte { + if val, ok := c.hashMap[id]; ok { + slog.Debug("gave an old hash", slog.String("hash", string(val))) + return val + } + + if _, ok := c.mutexMap[id]; !ok { + c.mutexMap[id] = &sync.Mutex{} + } + c.mutexMap[id].Lock() + defer c.mutexMap[id].Unlock() + + if val, ok := c.hashMap[id]; ok { + slog.Debug("gave a newly generated hash", slog.String("hash", string(val))) + return val + } + + c.hashMap[id] = argon2.IDKey([]byte(id), c.salt, 1, 64*1024, 4, 32) + slog.Debug("generated hash", slog.String("hash", string(c.hashMap[id]))) + return c.hashMap[id] +} |