38 lines
530 B
Go
38 lines
530 B
Go
|
package cache
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
|
||
|
"esway/internal/interfaces"
|
||
|
)
|
||
|
|
||
|
var Client interfaces.Cacher
|
||
|
|
||
|
type encoded_value interface {
|
||
|
MarshalBinary() ([]byte, error)
|
||
|
}
|
||
|
|
||
|
type decoded_value interface {
|
||
|
UnmarshalBinary(bs []byte) error
|
||
|
}
|
||
|
|
||
|
func handleValue(value any) ([]byte, error) {
|
||
|
var (
|
||
|
bs []byte
|
||
|
err error
|
||
|
)
|
||
|
|
||
|
switch value.(type) {
|
||
|
case []byte:
|
||
|
return value.([]byte), nil
|
||
|
}
|
||
|
|
||
|
if imp, ok := value.(encoded_value); ok {
|
||
|
bs, err = imp.MarshalBinary()
|
||
|
} else {
|
||
|
bs, err = json.Marshal(value)
|
||
|
}
|
||
|
|
||
|
return bs, err
|
||
|
}
|