github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/store/store.go (about) 1 package store 2 3 import ( 4 "github.com/isyscore/isc-gobase/constants" 5 "github.com/isyscore/isc-gobase/goid" 6 cmap "github.com/orcaman/concurrent-map" 7 "net/http" 8 ) 9 10 var headKeyValueStorage goid.LocalStorage 11 12 func init() { 13 headKeyValueStorage = goid.NewLocalStorage() 14 } 15 16 func PutFromHead(httpHead http.Header) { 17 headKvMap := headKeyValueStorage.Get() 18 19 if headKvMap == nil { 20 headKvMap = cmap.New() 21 } 22 kvMap := headKvMap.(cmap.ConcurrentMap) 23 kvMap.Set(constants.TRACE_HEAD_ID, httpHead.Get(constants.TRACE_HEAD_ID)) 24 kvMap.Set(constants.TRACE_HEAD_RPC_ID, httpHead.Get(constants.TRACE_HEAD_RPC_ID)) 25 kvMap.Set(constants.TRACE_HEAD_SAMPLED, httpHead.Get(constants.TRACE_HEAD_SAMPLED)) 26 kvMap.Set(constants.TRACE_HEAD_USER_ID, httpHead.Get(constants.TRACE_HEAD_USER_ID)) 27 kvMap.Set(constants.TRACE_HEAD_USER_NAME, httpHead.Get(constants.TRACE_HEAD_USER_NAME)) 28 kvMap.Set(constants.TRACE_HEAD_REMOTE_IP, httpHead.Get(constants.TRACE_HEAD_REMOTE_IP)) 29 kvMap.Set(constants.TRACE_HEAD_REMOTE_APPNAME, httpHead.Get(constants.TRACE_HEAD_REMOTE_APPNAME)) 30 kvMap.Set(constants.TRACE_HEAD_ORIGNAL_URL, httpHead.Get(constants.TRACE_HEAD_ORIGNAL_URL)) 31 headKeyValueStorage.Set(kvMap) 32 } 33 34 func Put(key string, value any) { 35 headKvMap := headKeyValueStorage.Get() 36 37 if headKvMap == nil { 38 headKvMap = map[string]any{} 39 } 40 kvMap := headKvMap.(cmap.ConcurrentMap) 41 kvMap.Set(key, value) 42 headKeyValueStorage.Set(kvMap) 43 } 44 45 func Get(key string) any { 46 headKvMap := headKeyValueStorage.Get() 47 48 if headKvMap == nil { 49 headKvMap = cmap.New() 50 headKeyValueStorage.Set(headKvMap) 51 return "" 52 } 53 kvMap := headKvMap.(cmap.ConcurrentMap) 54 data, exist := kvMap.Get(key) 55 if exist { 56 return data 57 } 58 return "" 59 } 60 61 func Keys() []string { 62 headKvMap := headKeyValueStorage.Get() 63 64 if headKvMap == nil { 65 return []string{} 66 } 67 68 kvMap := headKvMap.(cmap.ConcurrentMap) 69 return kvMap.Keys() 70 } 71 72 func Clean() { 73 headKeyValueStorage.Del() 74 }