recache

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2023 License: MIT Imports: 6 Imported by: 1

README

recache

Package recache is a lightweight caching library for Go's standard regular expression package that offers improved performance by avoiding recompilation of global regular expression variables and by caching regular expressions.

Features

  • Stable cache interface.
  • Simple and easy-to-use API.
  • Thread-safe caching of compiled regular expressions.
  • Lazy compilation of regular expressions.
  • Minimal memory allocations.
recache.Cache implementations

The recache package itself only provides a cache interface and some utility functions for users who wish to implement that interface. You can either use an implementation created by someone else or write your own.

Implementations

If wrote a recache.Cache implementation and wish it to be linked here, please send a patch.

Installation

To install recache alone, run:

go get git.sr.ht/~jamesponddotco/recache-go

Contributing

Anyone can help make recache better. Check out the contribution guidelines for more information.

Resources

The following resources are available:


Released under the MIT License.

Documentation

Overview

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

Index

Constants

View Source
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"
)
View Source
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.

View Source
const ErrInvalidEntry xerrors.Error = "invalid entry"

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

Variables

This section is empty.

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.

Directories

Path Synopsis
Package lrure implements a thread-safe LRU cache for [Go's standard regex package] that complies with the [recache.Cache] interface.
Package lrure implements a thread-safe LRU cache for [Go's standard regex package] that complies with the [recache.Cache] interface.

Jump to

Keyboard shortcuts

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