recache-go – git.sr.ht/~jamesponddotco/recache-go Index | Files | Directories

package recache

import "git.sr.ht/~jamesponddotco/recache-go"

Package recache provides a simple caching interface for Go's regular expressions package, regexp.

Index

Constants

const (
	// ErrInvalidCapacity is returned when the provided capacity is less than 1.
	//
	// This error is not used by the package itself, but is exported for use by
	// packages implementing the Cache interface.
	ErrInvalidCapacity xerrors.Error = "invalid capacity"

	// ErrUnexpectedType is returned when the cache encounters an unexpected
	// type in the list.
	//
	// This error is not used by the package itself, but is exported for use by
	// packages implementing the Cache interface.
	ErrUnexpectedType xerrors.Error = "unexpected type found in the list: expected *recache.Entry"

	// ErrNotFound is returned when a regular expression is not found in the
	// cache.
	//
	// This error is not used by the package itself, but is exported for use by
	// packages implementing the Cache interface.
	ErrNotFound xerrors.Error = "not found in the cache"
)
const DefaultCapacity int = 25

DefaultCapacity is the default maximum number of regular expressions that can be stored in the cache.

This constant is not used by the package itself, but is exported for use by packages implementing the Cache interface.

const ErrInvalidEntry xerrors.Error = "invalid entry"

ErrInvalidEntry is returned when the entry in the cache is invalid.

Functions

func Compile

func Compile(pattern string, flag Flag) (*regexp.Regexp, error)

Compile compiles the provided regular expression pattern taking the provided control flag into account.

This function is not used by the package itself, but is exported for use by packages implementing the Cache interface.

func Key

func Key(pattern string, flag Flag) string

Key generates a cache key for the provided regular expression pattern and control flag by concatenating the two and hashing the result.

The generated key is of the form "pattern:PATTERN:flag:FLAG".

This function is not used by the package itself, but is exported for use by packages implementing the Cache interface.

Types

type Cache

type Cache interface {
	// Get returns a compiled regular expression from the cache given a pattern
	// and an optional flag.
	Get(ctx context.Context, pattern string, flag Flag) (*regexp.Regexp, error)

	// SetCapacity sets the maximum number of regular expressions that can be
	// stored in the cache.
	SetCapacity(capacity int) error

	// Capacity returns the maximum number of regular expressions that can be
	// stored in the cache.
	Capacity() int

	// Size returns the number of regular expressions currently stored in the
	// cache.
	Size() int

	// Clear removes all regular expressions from the cache.
	Clear()
}

Cache is a storage mechanism used to store and retrieve compiled regular expressions for improved performance.

type Entry

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

Entry represents an item in the cache.

func NewEntry

func NewEntry(key, pattern string, regex *regexp.Regexp) *Entry

NewEntry creates a new entry in the cache.

func (*Entry) Frequency

func (e *Entry) Frequency() uint64

Frequency returns the number of times the entry has been loaded.

func (*Entry) Key

func (e *Entry) Key() string

Key returns the entry's cache key.

func (*Entry) Load

func (e *Entry) Load() (*regexp.Regexp, string, error)

Load returns the compiled regex and pattern, and increments the frequency of the entry by one.

func (*Entry) Pattern

func (e *Entry) Pattern() string

Pattern returns the entry's pattern.

type Flag

type Flag int

Flag controls the behavior of the Get method when compiling regular expressions.

const (
	// DefaultFlag is the default flag used when compiling regular expressions.
	DefaultFlag Flag = 0

	// FlagPOSIX specifies that the regular expression should be restricted to
	// POSIX syntax.
	FlagPOSIX Flag = 1 << iota

	// FlagMust specifies that the regular expression should be compiled using
	// MustCompile.
	FlagMust

	// FlagMustPOSIX is like Must but restricts the regular expression to POSIX
	// syntax.
	FlagMustPOSIX = FlagMust | FlagPOSIX
)

func (Flag) String

func (f Flag) String() string

String returns a string representation of the flag.

Source Files

cache.go entry.go recache.go

Directories

PathSynopsis
lrurePackage lrure implements a thread-safe LRU cache for [Go's standard regex package] that complies with the recache.Cache interface.
Version
v1.0.1 (latest)
Published
Apr 1, 2023
Platform
linux/amd64
Imports
6 packages
Last checked
6 days ago

Tools for package owners.