rcutil

package module
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2023 License: MIT Imports: 15 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
)
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

Functions

func DecodeReqRes

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

DecodeReqRes decodes to http.Request and http.Response.

func EncodeReqRes

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

EncodeReqRes encodes http.Request and 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 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, 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) StopAutoCleanup

func (c *DiskCache) StopAutoCleanup()

StopAutoCleanup stops the auto cleanup 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) 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)

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 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