cache

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: May 17, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Functions

func GetCacheKey

func GetCacheKey(req *http.Request) string

func ParseCacheControl

func ParseCacheControl(header string) map[string]string

ParseCacheControl parses the Cache-Control header and returns a map of directives.

func ReconstructCacheControl

func ReconstructCacheControl(cacheControl map[string]string) string

ReconstructCacheControl reconstructs the Cache-Control header from a map of directives.

Types

type CacheConfig

type CacheConfig struct {
	CacheableMIMETypes map[string]bool
	DefaultExpiration  time.Duration
}

CacheConfig holds configuration for the cache, including cacheable MIME types.

type CacheItem

type CacheItem struct {
	Path            string
	ContentType     string
	Content         []byte
	ContentEncoding string
	Expiration      time.Time
	CacheControl    string
}

CacheItem represents an item in the cache.

type CacheManager

type CacheManager interface {
	Read(key string) (CacheItem, bool)
	Write(key string, item CacheItem)
	Delete(key string)
	List() []string
	ShouldCache(resp *http.Response, cacheControl map[string]string) bool
	SetItemExpiration(item *CacheItem, cacheControl map[string]string)
	IsCacheableContentType(contentType string) bool
	WriteWithExpiration(key string, item CacheItem, cacheControl map[string]string)
	WriteWithDefaultExpiration(key string, item CacheItem)
}

CacheManager defines the interface for interacting with the cache.

type InMemoryCache

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

InMemoryCache provides a simple in-memory cache implementation of CacheManager.

func NewInMemoryCache

func NewInMemoryCache(maxSize int, config CacheConfig) *InMemoryCache

NewInMemoryCache creates a new InMemoryCache with a specified maximum size and cache configuration.

func (*InMemoryCache) Delete

func (c *InMemoryCache) Delete(key string)

Delete removes an item from the cache.

func (*InMemoryCache) IsCacheableContentType

func (c *InMemoryCache) IsCacheableContentType(contentType string) bool

IsCacheableContentType checks if the content type is cacheable based on the configuration.

func (*InMemoryCache) List

func (c *InMemoryCache) List() []string

List returns a list of all keys in the cache.

func (*InMemoryCache) Read

func (c *InMemoryCache) Read(key string) (CacheItem, bool)

Read retrieves an item from the cache.

func (*InMemoryCache) ReadFromCache

func (c *InMemoryCache) ReadFromCache(key string) (CacheItem, bool)

ReadFromCache is a convenience function to access cached items. This could be a method of InMemoryCache if it needs to access the cache's internal state.

func (*InMemoryCache) SetItemExpiration

func (c *InMemoryCache) SetItemExpiration(item *CacheItem, cacheControl map[string]string)

SetItemExpiration adjusts the expiration of a cache item based on Cache-Control directives.

func (*InMemoryCache) ShouldCache

func (c *InMemoryCache) ShouldCache(resp *http.Response, cacheControl map[string]string) bool

ShouldCache decides whether the response should be cached based on Cache-Control directives and the content type, with a default caching policy for cacheable content types.

func (*InMemoryCache) Write

func (c *InMemoryCache) Write(key string, item CacheItem)

Write adds or updates an item in the cache.

func (*InMemoryCache) WriteToCache

func (c *InMemoryCache) WriteToCache(key string, item CacheItem, cacheControl map[string]string)

WriteToCache stores an item in the cache with an optional expiration based on cacheControl directives.

func (*InMemoryCache) WriteWithDefaultExpiration

func (c *InMemoryCache) WriteWithDefaultExpiration(key string, item CacheItem)

Write adds or updates an item in the cache with the default expiration if not set.

func (*InMemoryCache) WriteWithExpiration

func (c *InMemoryCache) WriteWithExpiration(key string, item CacheItem, cacheControl map[string]string)

WriteWithExpiration stores an item in the cache with an expiration time based on cacheControl.

Jump to

Keyboard shortcuts

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