2024-02-13 04:11:30 +08:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/google/uuid"
|
2024-04-06 22:43:36 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
2024-02-13 04:11:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var uniqueID string
|
|
|
|
|
|
|
|
func InitUniqueID() {
|
|
|
|
uniqueID = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUniqueID() string {
|
|
|
|
return uniqueID
|
|
|
|
}
|
2024-04-06 22:43:36 +08:00
|
|
|
|
2024-06-25 21:27:44 +08:00
|
|
|
var PatchedMessageCache = make(map[uint32][]byte)
|
|
|
|
var PatchedMessageCacheMutex sync.RWMutex
|
2024-04-06 22:43:36 +08:00
|
|
|
|
2024-06-25 21:27:44 +08:00
|
|
|
func GetPatchedMessageCache(cacheKey uint32) ([]byte, bool) {
|
|
|
|
PatchedMessageCacheMutex.RLock()
|
|
|
|
defer PatchedMessageCacheMutex.RUnlock()
|
2024-04-06 22:43:36 +08:00
|
|
|
|
2024-06-25 21:27:44 +08:00
|
|
|
jsonDiffPatch, jsonDiffPatchExists := PatchedMessageCache[cacheKey]
|
2024-04-06 22:43:36 +08:00
|
|
|
return jsonDiffPatch, jsonDiffPatchExists
|
|
|
|
}
|
|
|
|
|
2024-06-25 21:27:44 +08:00
|
|
|
func StorePatchedMessageCache(cacheKey uint32, data []byte) {
|
|
|
|
PatchedMessageCacheMutex.Lock()
|
|
|
|
defer PatchedMessageCacheMutex.Unlock()
|
2024-04-06 22:43:36 +08:00
|
|
|
|
2024-06-25 21:27:44 +08:00
|
|
|
PatchedMessageCache[cacheKey] = data
|
2024-04-06 22:43:36 +08:00
|
|
|
|
|
|
|
//Remove the cache after 30 seconds
|
2024-06-25 21:27:44 +08:00
|
|
|
go RemovePatchedMessageCache(cacheKey, 30)
|
2024-04-06 22:43:36 +08:00
|
|
|
}
|
|
|
|
|
2024-06-25 21:27:44 +08:00
|
|
|
func RemovePatchedMessageCache(cacheKey uint32, delayInSecs time.Duration) {
|
2024-04-06 22:43:36 +08:00
|
|
|
time.Sleep(delayInSecs * time.Second)
|
|
|
|
|
2024-06-25 21:27:44 +08:00
|
|
|
PatchedMessageCacheMutex.Lock()
|
|
|
|
defer PatchedMessageCacheMutex.Unlock()
|
|
|
|
delete(PatchedMessageCache, cacheKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
var HasuraMessageCache = make(map[uint32]HasuraMessage)
|
|
|
|
var HasuraMessageKeyCache = make(map[uint32]string)
|
|
|
|
var HasuraMessageCacheMutex sync.RWMutex
|
|
|
|
|
|
|
|
func GetHasuraMessageCache(cacheKey uint32) (string, HasuraMessage, bool) {
|
|
|
|
HasuraMessageCacheMutex.RLock()
|
|
|
|
defer HasuraMessageCacheMutex.RUnlock()
|
|
|
|
|
|
|
|
hasuraMessageDataKey, _ := HasuraMessageKeyCache[cacheKey]
|
|
|
|
hasuraMessage, hasuraMessageExists := HasuraMessageCache[cacheKey]
|
|
|
|
return hasuraMessageDataKey, hasuraMessage, hasuraMessageExists
|
|
|
|
}
|
|
|
|
|
|
|
|
func StoreHasuraMessageCache(cacheKey uint32, dataKey string, hasuraMessage HasuraMessage) {
|
|
|
|
HasuraMessageCacheMutex.Lock()
|
|
|
|
defer HasuraMessageCacheMutex.Unlock()
|
|
|
|
|
|
|
|
HasuraMessageKeyCache[cacheKey] = dataKey
|
|
|
|
HasuraMessageCache[cacheKey] = hasuraMessage
|
|
|
|
|
|
|
|
//Remove the cache after 30 seconds
|
|
|
|
go RemoveHasuraMessageCache(cacheKey, 30)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveHasuraMessageCache(cacheKey uint32, delayInSecs time.Duration) {
|
|
|
|
time.Sleep(delayInSecs * time.Second)
|
|
|
|
|
|
|
|
HasuraMessageCacheMutex.Lock()
|
|
|
|
defer HasuraMessageCacheMutex.Unlock()
|
|
|
|
delete(HasuraMessageKeyCache, cacheKey)
|
|
|
|
delete(HasuraMessageCache, cacheKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
var StreamCursorValueCache = make(map[uint32]interface{})
|
|
|
|
var StreamCursorValueCacheMutex sync.RWMutex
|
|
|
|
|
|
|
|
func GetStreamCursorValueCache(cacheKey uint32) (interface{}, bool) {
|
|
|
|
StreamCursorValueCacheMutex.RLock()
|
|
|
|
defer StreamCursorValueCacheMutex.RUnlock()
|
|
|
|
|
|
|
|
streamCursorValue, streamCursorValueExists := StreamCursorValueCache[cacheKey]
|
|
|
|
return streamCursorValue, streamCursorValueExists
|
|
|
|
}
|
|
|
|
|
|
|
|
func StoreStreamCursorValueCache(cacheKey uint32, streamCursorValue interface{}) {
|
|
|
|
StreamCursorValueCacheMutex.Lock()
|
|
|
|
defer StreamCursorValueCacheMutex.Unlock()
|
|
|
|
|
|
|
|
StreamCursorValueCache[cacheKey] = streamCursorValue
|
|
|
|
|
|
|
|
//Remove the cache after 30 seconds
|
|
|
|
go RemoveStreamCursorValueCache(cacheKey, 30)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveStreamCursorValueCache(cacheKey uint32, delayInSecs time.Duration) {
|
|
|
|
time.Sleep(delayInSecs * time.Second)
|
|
|
|
|
|
|
|
StreamCursorValueCacheMutex.Lock()
|
|
|
|
defer StreamCursorValueCacheMutex.Unlock()
|
|
|
|
delete(StreamCursorValueCache, cacheKey)
|
2024-04-06 22:43:36 +08:00
|
|
|
}
|