serializer

package
v1.0.0-rc.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package serializer defines the value encoding boundary used by cache. Built-in JSON and MessagePack implementations are available through New; callers can implement Serializer and inject it with cache.WithSerializer. Changing encodings requires invalidating existing entries or changing the cache key prefix because values written by one encoding may not decode with another.

Example
package main

import (
	"github.com/ceyewan/genesis/cache/serializer"
)

func main() {
	codec, err := serializer.New("json")
	if err != nil {
		return
	}
	_, _ = codec.Marshal(map[string]string{"status": "ok"})
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupportedSerializer 不支持的序列化器类型。
	ErrUnsupportedSerializer = xerrors.New("unsupported serializer type")
)

错误定义

Functions

This section is empty.

Types

type JSONSerializer

type JSONSerializer struct{}

JSONSerializer JSON 序列化器

func (*JSONSerializer) Marshal

func (j *JSONSerializer) Marshal(value any) ([]byte, error)

Marshal 序列化为 JSON

func (*JSONSerializer) Unmarshal

func (j *JSONSerializer) Unmarshal(data []byte, dest any) error

Unmarshal 从 JSON 反序列化

type MessagePackSerializer

type MessagePackSerializer struct{}

MessagePackSerializer MessagePack 序列化器

func (*MessagePackSerializer) Marshal

func (m *MessagePackSerializer) Marshal(value any) ([]byte, error)

Marshal 序列化为 MessagePack MessagePack 比 JSON 更高效:序列化速度快 2-3 倍,数据体积小 20-30%

func (*MessagePackSerializer) Unmarshal

func (m *MessagePackSerializer) Unmarshal(data []byte, dest any) error

Unmarshal 从 MessagePack 反序列化

type Serializer

type Serializer interface {
	Marshal(value any) ([]byte, error)
	Unmarshal(data []byte, dest any) error
}

Serializer 定义序列化接口

func New

func New(serializerType string) (Serializer, error)

New 创建序列化器

支持的序列化器类型:

  • "json": 标准库 JSON 序列化,兼容性最好
  • "msgpack": MessagePack 二进制序列化,性能更优

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL