compression

package
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecompressAndDeserialize

func DecompressAndDeserialize(data []byte, isCompressed bool, compressor Compressor, target any) error

DecompressAndDeserialize decompresses and deserializes data back to a value

func SerializeAndCompress

func SerializeAndCompress(value any, compressor Compressor, minSize int) ([]byte, bool, error)

SerializeAndCompress converts a value to bytes and compresses it if it meets size threshold

Types

type Compressor

type Compressor interface {
	// Compress compresses the given data and returns compressed bytes
	Compress(data []byte) ([]byte, error)

	// Decompress decompresses the given compressed bytes
	Decompress(compressed []byte) ([]byte, error)

	// Name returns the name/identifier of the compressor
	Name() string
}

Compressor defines the interface for cache value compression

func NewCompressor

func NewCompressor(config *Config) (Compressor, error)

NewCompressor creates a new compressor based on the configuration

type CompressorType

type CompressorType string

CompressorType represents different compression algorithms

const (
	CompressorNone    CompressorType = "none"
	CompressorGzip    CompressorType = "gzip"
	CompressorDeflate CompressorType = "deflate"
)

type Config

type Config struct {
	// Enabled determines whether compression is enabled
	Enabled bool

	// Algorithm specifies which compression algorithm to use
	Algorithm CompressorType

	// MinSize is the minimum size in bytes before compression is applied
	// Values smaller than this will not be compressed to avoid overhead
	MinSize int

	// Level is the compression level (1-9 for gzip/deflate, -1 for default)
	Level int
}

Config holds compression configuration

func NewDefaultConfig

func NewDefaultConfig() *Config

NewDefaultConfig creates a default compression configuration

func (*Config) WithAlgorithm

func (c *Config) WithAlgorithm(algorithm CompressorType) *Config

WithAlgorithm sets the compression algorithm

func (*Config) WithEnabled

func (c *Config) WithEnabled(enabled bool) *Config

WithEnabled sets whether compression is enabled

func (*Config) WithLevel

func (c *Config) WithLevel(level int) *Config

WithLevel sets the compression level

func (*Config) WithMinSize

func (c *Config) WithMinSize(minSize int) *Config

WithMinSize sets the minimum size threshold for compression

type DeflateCompressor

type DeflateCompressor struct {
	// contains filtered or unexported fields
}

DeflateCompressor implements compression using zlib/deflate

func NewDeflateCompressor

func NewDeflateCompressor(level int) *DeflateCompressor

NewDeflateCompressor creates a new deflate compressor with the specified level

func (*DeflateCompressor) Compress

func (d *DeflateCompressor) Compress(data []byte) ([]byte, error)

Compress compresses data using deflate

func (*DeflateCompressor) Decompress

func (d *DeflateCompressor) Decompress(compressed []byte) ([]byte, error)

Decompress decompresses deflate data

func (*DeflateCompressor) Name

func (d *DeflateCompressor) Name() string

Name returns the compressor name

type GzipCompressor

type GzipCompressor struct {
	// contains filtered or unexported fields
}

GzipCompressor implements compression using gzip

func NewGzipCompressor

func NewGzipCompressor(level int) *GzipCompressor

NewGzipCompressor creates a new gzip compressor with the specified level

func (*GzipCompressor) Compress

func (g *GzipCompressor) Compress(data []byte) ([]byte, error)

Compress compresses data using gzip

func (*GzipCompressor) Decompress

func (g *GzipCompressor) Decompress(compressed []byte) ([]byte, error)

Decompress decompresses gzip data

func (*GzipCompressor) Name

func (g *GzipCompressor) Name() string

Name returns the compressor name

type NoOpCompressor

type NoOpCompressor struct{}

NoOpCompressor provides a no-op implementation that doesn't compress

func NewNoOpCompressor

func NewNoOpCompressor() *NoOpCompressor

NewNoOpCompressor creates a new no-op compressor

func (*NoOpCompressor) Compress

func (n *NoOpCompressor) Compress(data []byte) ([]byte, error)

Compress returns the data unchanged

func (*NoOpCompressor) Decompress

func (n *NoOpCompressor) Decompress(compressed []byte) ([]byte, error)

Decompress returns the data unchanged

func (*NoOpCompressor) Name

func (n *NoOpCompressor) Name() string

Name returns the compressor name

Jump to

Keyboard shortcuts

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