From 6ced549deaecb42b9bb93ea9efcb4c1bbaabe8a4 Mon Sep 17 00:00:00 2001 From: mhsanaei Date: Sat, 20 Sep 2025 09:35:50 +0200 Subject: docs: add comments for all functions --- web/global/hashStorage.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'web/global/hashStorage.go') diff --git a/web/global/hashStorage.go b/web/global/hashStorage.go index 5d8135ee..962493f7 100644 --- a/web/global/hashStorage.go +++ b/web/global/hashStorage.go @@ -8,18 +8,21 @@ import ( "time" ) +// HashEntry represents a stored hash entry with its value and timestamp. type HashEntry struct { - Hash string - Value string - Timestamp time.Time + Hash string // MD5 hash string + Value string // Original value + Timestamp time.Time // Time when the hash was created } +// HashStorage provides thread-safe storage for hash-value pairs with expiration. type HashStorage struct { sync.RWMutex - Data map[string]HashEntry - Expiration time.Duration + Data map[string]HashEntry // Map of hash to entry + Expiration time.Duration // Expiration duration for entries } +// NewHashStorage creates a new HashStorage instance with the specified expiration duration. func NewHashStorage(expiration time.Duration) *HashStorage { return &HashStorage{ Data: make(map[string]HashEntry), @@ -27,6 +30,7 @@ func NewHashStorage(expiration time.Duration) *HashStorage { } } +// SaveHash generates an MD5 hash for the given query string and stores it with a timestamp. func (h *HashStorage) SaveHash(query string) string { h.Lock() defer h.Unlock() @@ -45,6 +49,7 @@ func (h *HashStorage) SaveHash(query string) string { return md5HashString } +// GetValue retrieves the original value for the given hash, returning true if found. func (h *HashStorage) GetValue(hash string) (string, bool) { h.RLock() defer h.RUnlock() @@ -54,11 +59,13 @@ func (h *HashStorage) GetValue(hash string) (string, bool) { return entry.Value, exists } +// IsMD5 checks if the given string is a valid 32-character MD5 hash. func (h *HashStorage) IsMD5(hash string) bool { match, _ := regexp.MatchString("^[a-f0-9]{32}$", hash) return match } +// RemoveExpiredHashes removes all hash entries that have exceeded the expiration duration. func (h *HashStorage) RemoveExpiredHashes() { h.Lock() defer h.Unlock() @@ -72,6 +79,7 @@ func (h *HashStorage) RemoveExpiredHashes() { } } +// Reset clears all stored hash entries. func (h *HashStorage) Reset() { h.Lock() defer h.Unlock() -- cgit v1.2.3