mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-14 00:58:32 +00:00
Replace allegro/bigcache with something lighter.
In high frequency environment the allegro bigcache caused significant memory usage overhead. This commit replaces it with something much lighter.
This commit is contained in:
@@ -55,10 +55,67 @@ type customJsonEncoder struct {
|
||||
}
|
||||
|
||||
func (e *customJsonEncoder) Encode(v interface{}) error {
|
||||
data, err := json.Marshal(v)
|
||||
if v == nil {
|
||||
e.buf.WriteString("null")
|
||||
return nil
|
||||
}
|
||||
|
||||
switch val := v.(type) {
|
||||
case *OrderedMap:
|
||||
data, err := json.Marshal(val.values)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.buf.Write(data)
|
||||
default:
|
||||
data, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.buf.Write(data)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o OrderedMap) Keys() []string {
|
||||
return o.keys
|
||||
}
|
||||
|
||||
func (o OrderedMap) Values() []interface{} {
|
||||
var values []interface{}
|
||||
for _, k := range o.keys {
|
||||
values = append(values, o.values[k])
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func (o *OrderedMap) Delete(key string) {
|
||||
delete(o.values, key)
|
||||
for i, k := range o.keys {
|
||||
if k == key {
|
||||
o.keys = append(o.keys[:i], o.keys[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (o OrderedMap) Exists(key string) bool {
|
||||
_, exists := o.values[key]
|
||||
return exists
|
||||
}
|
||||
|
||||
func (o *OrderedMap) UnmarshalJSON(data []byte) error {
|
||||
var values map[string]interface{}
|
||||
err := json.Unmarshal(data, &values)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.buf.Write(data)
|
||||
|
||||
o.keys = []string{}
|
||||
o.values = map[string]interface{}{}
|
||||
for k, v := range values {
|
||||
o.Set(k, v)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user