mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-03-30 02:22:49 +00:00
refactor(ratio): replace maps with RWMap for improved concurrency handling
This commit is contained in:
@@ -80,3 +80,24 @@ func LoadFromJsonString[K comparable, V any](m *RWMap[K, V], jsonStr string) err
|
||||
m.data = make(map[K]V)
|
||||
return common.Unmarshal([]byte(jsonStr), &m.data)
|
||||
}
|
||||
|
||||
// LoadFromJsonStringWithCallback loads a JSON string into the RWMap and calls the callback on success.
|
||||
func LoadFromJsonStringWithCallback[K comparable, V any](m *RWMap[K, V], jsonStr string, onSuccess func()) error {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.data = make(map[K]V)
|
||||
err := common.Unmarshal([]byte(jsonStr), &m.data)
|
||||
if err == nil && onSuccess != nil {
|
||||
onSuccess()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// MarshalJSONString returns the JSON string representation of the RWMap.
|
||||
func (m *RWMap[K, V]) MarshalJSONString() string {
|
||||
bytes, err := m.MarshalJSON()
|
||||
if err != nil {
|
||||
return "{}"
|
||||
}
|
||||
return string(bytes)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user