From f82d0051b2cf827228b5c939d22622bd0d066149 Mon Sep 17 00:00:00 2001 From: Hamidreza Ghavami <70919649+hamid-gh98@users.noreply.github.com> Date: Sun, 21 May 2023 08:33:08 +0430 Subject: Update hashstorage functionality --- web/global/hashStorage.go | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) (limited to 'web/global') diff --git a/web/global/hashStorage.go b/web/global/hashStorage.go index 3d5a6a0d..9dfea169 100644 --- a/web/global/hashStorage.go +++ b/web/global/hashStorage.go @@ -6,7 +6,6 @@ import ( "regexp" "sync" "time" - "x-ui/util/common" ) type HashEntry struct { @@ -19,31 +18,17 @@ type HashStorage struct { sync.RWMutex Data map[string]HashEntry Expiration time.Duration - ForceSave bool + } -func NewHashStorage(expiration time.Duration, forceSave bool) *HashStorage { +func NewHashStorage(expiration time.Duration) *HashStorage { return &HashStorage{ Data: make(map[string]HashEntry), Expiration: expiration, - ForceSave: forceSave, } } -func (h *HashStorage) AddHash(query string) string { - if h.ForceSave { - return h.saveValue(query) - } - - // we only need to hash for more than 64 chars by default - if len(query) <= 64 { - return query - } - - return h.saveValue(query) -} - -func (h *HashStorage) saveValue(query string) string { +func (h *HashStorage) SaveHash(query string) string { h.Lock() defer h.Unlock() @@ -61,21 +46,17 @@ func (h *HashStorage) saveValue(query string) string { return md5HashString } -func (h *HashStorage) GetValue(hash string) (string, error) { + +func (h *HashStorage) GetValue(hash string) (string, bool) { h.RLock() defer h.RUnlock() entry, exists := h.Data[hash] - if !exists { - if h.isMD5(hash) { - return "", common.NewError("hash not found in storage!") - } - return hash, nil - } - return entry.Value, nil + + return entry.Value, exists } -func (h *HashStorage) isMD5(hash string) bool { +func (h *HashStorage) IsMD5(hash string) bool { match, _ := regexp.MatchString("^[a-f0-9]{32}$", hash) return match } -- cgit v1.2.3