Update hashstorage functionality

This commit is contained in:
Hamidreza Ghavami
2023-05-21 08:33:08 +04:30
parent 76267b23a0
commit f82d0051b2
2 changed files with 79 additions and 77 deletions
+8 -27
View File
@@ -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
}