rcutil

package module
v0.16.4 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2024 License: MIT Imports: 21 Imported by: 0

README

rcutil Go Reference build Coverage Code to Test Ratio Test Execution Time

Package rcutil provides utility functions for rc

Documentation

Index

Constants

View Source
const (
	// NoLimitKeys is a special value that means no limit on the number of keys.
	NoLimitKeys = 0
	// NoLimitTotalBytes is a special value that means no limit on the total number of bytes.
	NoLimitTotalBytes = 0
	// NoLimitTTL is a special value that means no limit on the TTL.
	NoLimitTTL = ttlcache.NoTTL
	// DefaultCacheDirLen is the default length of the cache directory name.
	DefaultCacheDirLen = 2
)
View Source
const (
	CacheResultHeader = "X-Cache"
	CacheHit          = "HIT"
	CacheMiss         = "MISS"
)

Variables

View Source
var ErrCacheFull error = errors.New("cache full")

ErrCacheFull is returned if the cache is full

View Source
var ErrInvalidRequest = errors.New("invalid request")
View Source
var ErrNoRequest = errors.New("no request")

Functions

func DecodeReq added in v0.15.0

func DecodeReq(r io.Reader) (*http.Request, error)

DecodeReq decodes to http.Request

func DecodeReqRes

func DecodeReqRes(r io.Reader) (*http.Request, *http.Response, error)

DecodeReqRes decodes to http.Request and http.Response. Depracated: Use DecodeReq and DecodeRes instead.

func DecodeRes added in v0.15.0

func DecodeRes(r io.Reader) (*http.Response, error)

DecodeRes decodes to http.Response

func EncodeReq added in v0.15.0

func EncodeReq(req *http.Request, w io.Writer) error

EncodeReq encodes http.Request.

func EncodeReqRes

func EncodeReqRes(req *http.Request, res *http.Response, w io.Writer) error

EncodeReqRes encodes http.Request and http.Response. Depracated: Use EncodeReq and EncodeRes instead.

func EncodeRes added in v0.15.0

func EncodeRes(res *http.Response, w io.Writer) error

EncodeRes encodes http.Response.

func KeyToPath

func KeyToPath(key string, n int) string

KeyToPath converts key to path It is the responsibility of the user to pass path-safe keys

func PathToKey added in v0.14.0

func PathToKey(path string) string

PathToKey converts path to key

func Seed

func Seed(req *http.Request, vary []string) (string, error)

Seed returns seed for cache key. The return value seed is NOT path-safe.

func SetCacheResultHeader

func SetCacheResultHeader(res *http.Response, hit bool)

SetCacheResultHeader sets cache header.

Types

type DiskCache

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

DiskCache is a disk cache implementation.

func NewDiskCache

func NewDiskCache(cacheRoot string, defaultTTL time.Duration, opts ...DiskCacheOption) (*DiskCache, error)

NewDiskCache returns a new DiskCache. cacheRoot: the root directory of the cache. defaultTTL: the default TTL of the cache. maxKeys: the maximum number of keys that can be stored in the cache. If NoLimitKeys is specified, there is no limit. maxTotalBytes: the maximum number of bytes that can be stored in the cache. If NoLimitTotalBytes is specified, there is no limit.

func (*DiskCache) Delete

func (c *DiskCache) Delete(key string)

Delete deletes the cache.

func (*DiskCache) DeleteExpired

func (c *DiskCache) DeleteExpired()

DeleteExpired deletes expired caches.

func (*DiskCache) Load

func (c *DiskCache) Load(key string) (_ *http.Request, _ *http.Response, err error)

Load loads the response from the cache.

func (*DiskCache) Metrics

func (c *DiskCache) Metrics() Metrics

Metrics returns the metrics of the cache.

func (*DiskCache) StartAutoCleanup

func (c *DiskCache) StartAutoCleanup()

StartAutoCleanup starts the goroutine of automatic cache cleanup

func (*DiskCache) StopAdjust added in v0.14.0

func (c *DiskCache) StopAdjust()

StopAdjust

func (*DiskCache) StopAll added in v0.14.0

func (c *DiskCache) StopAll()

StopAll stops all the goroutines of the cache.

func (*DiskCache) StopAutoCleanup

func (c *DiskCache) StopAutoCleanup()

StopAutoCleanup stops the auto cleanup cache.

func (*DiskCache) StopWarmUp added in v0.14.0

func (c *DiskCache) StopWarmUp()

StopWarmUp stops the warm up cache.

func (*DiskCache) Store

func (c *DiskCache) Store(key string, req *http.Request, res *http.Response) error

Store stores the response in the cache with the default TTL.

func (*DiskCache) StoreWithTTL

func (c *DiskCache) StoreWithTTL(key string, req *http.Request, res *http.Response, ttl time.Duration) (err error)

StoreWithTTL stores the response in the cache with the specified TTL. If you want to store the response with no TTL, use NoLimitTTL.

type DiskCacheOption

type DiskCacheOption func(*DiskCache) error

DiskCacheOption is an option for DiskCache.

func DisableAutoCleanup

func DisableAutoCleanup() DiskCacheOption

DisableAutoCleanup disables the automatic cache cleanup.

func DisableWarmUp

func DisableWarmUp() DiskCacheOption

DisableWarmUp disables the automatic cache warm up.

func EnableAutoAdjust added in v0.12.0

func EnableAutoAdjust() DiskCacheOption

EnableAutoAdjust enables auto-adjustment to delete the oldest cache when the total cache size limit (maxTotalBytes) is reached.

func EnableAutoAdjustWithPercentage added in v0.13.0

func EnableAutoAdjustWithPercentage(percentage uint64) DiskCacheOption

EnableAutoAdjustWithPercentage enables auto-adjustment to delete the oldest cache when the total cache size limit (maxTotalBytes) is reached. percentage: Delete until what percentage of the total byte size is reached.

func EnableTouchOnHit

func EnableTouchOnHit() DiskCacheOption

EnableTouchOnHit enables the touch on hit feature.

func MaxKeys

func MaxKeys(n uint64) DiskCacheOption

MaxKeys sets the maximum number of keys that can be stored in the cache.

func MaxTotalBytes

func MaxTotalBytes(n uint64) DiskCacheOption

MaxTotalBytes sets the maximum number of bytes that can be stored in the cache.

type Metrics

type Metrics struct {
	ttlcache.Metrics
	TotalBytes uint64
	KeyCount   uint64
}

Metrics returns the metrics of the cache.

type WriteCounter

type WriteCounter struct {
	io.Writer
	Bytes uint64
}

WriteCounter counts bytes written.

func (*WriteCounter) Write

func (wc *WriteCounter) Write(p []byte) (int, error)

Write writes bytes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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