cache

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2023 License: MIT Imports: 19 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// OnlyIfCachedKey is used for saving to request context the directive
	// 'only-if-cached' from Cache-Control.
	OnlyIfCachedKey = Key(iota)

	Hash
)
View Source
const (
	// DbDirectory is the directory of storing the BoltDB database.
	DbDirectory = "./cache-data"

	// DbName is a name of the database.
	DbName = "database.db"

	// DefaultKey is used if there's no key parameter of cache for url. Not used now.
	DefaultKey = "REQ_METHOD;REQ_HOST;REQ_URI"

	// PagesPath is the directory where the pages are written to.
	PagesPath = "./cache-data/db"
)

Variables

View Source
var (
	// ErrOnlyIfCached is used for sending to the client an error about
	// missing cache while 'only-if-cached' is specified in Cache-Control.
	ErrOnlyIfCached = errors.New("HTTP 504 Unsatisfiable Request (only-if-cached)")
)

Functions

func CloseDatabase

func CloseDatabase(db *bolt.DB)

CloseDatabase closes a database file.

func LoggerConfig

func LoggerConfig(prefix string)

func OpenDatabase

func OpenDatabase(path string) (*bolt.DB, error)

OpenDatabase opens a database file.

Types

type CacheCleaner

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

CacheCleaner deals with cache and its size by deleting expired or unnecessary pages. It goes in two steps: the first is removing expired cache. The cleaner looks only a max-age directive. The second step is removing unimportant pages: these are chosen by LRU algorithm. Each of metadata of the pages contains an amount of usage a particular page.

func NewCacheCleaner

func NewCacheCleaner(dbFile *os.File, maxFileSize int64, fillFactor float64, frequency *time.Ticker) *CacheCleaner

NewCacheCleaner creates a single item of a cleaner.

type CachingProperties

type CachingProperties struct {
	Size       int64
	PagesCount int
	// contains filtered or unexported fields
}

CachingProperties takes over the cache and the pages there. The structure of the cache reminds file systems in common operating systems. The pages are stored on a disk and the driver is stored in memory. The driver is a boltDB database containing buckets named by request hash. Each of buckets has metadata struct and an amount of usage the page during its life.

func NewCachingProperties

func NewCachingProperties(db *bolt.DB, cleaner *CacheCleaner) *CachingProperties

func (*CachingProperties) CalculateSize

func (p *CachingProperties) CalculateSize()

CalculateSize counts how many pages are stored in a cache and puts a total size of cache to CachingProperties. The function runs through the boltDB and checks if the cache is stored on a disk. If success, it adds its size to the counter.

func (*CachingProperties) Cleaner

func (p *CachingProperties) Cleaner() *CacheCleaner

func (*CachingProperties) ClearCache

func (p *CachingProperties) ClearCache() error

func (*CachingProperties) DB

func (p *CachingProperties) DB() *bolt.DB

func (*CachingProperties) GetPageFromCache

func (p *CachingProperties) GetPageFromCache(key []byte, req *http.Request) (*Page, error)

GetPageFromCache gets corresponding page and its metadata and returns it if it exists. Uses some parameters for building a request key, see in cache package and cacheConfig file.

func (*CachingProperties) IncrementSize

func (p *CachingProperties) IncrementSize(delta int64)

func (*CachingProperties) InsertPageInCache

func (p *CachingProperties) InsertPageInCache(key []byte, req *http.Request, resp *http.Response, page *Page) error

InsertPageInCache stores a new page in cache or rewrites the current page. First, it adds PageMetadata in DB and then the function starts a process of transactional writing the page on a disk. Page transforms to json-file.

func (*CachingProperties) Observe

func (p *CachingProperties) Observe()

Observe occasionally sets up a cleaner's goroutine which deletes expired and unnecessary pages from the cache. The frequency is declared in CacheCleaner struct.

func (*CachingProperties) RemovePageFromCache

func (p *CachingProperties) RemovePageFromCache(key []byte) (*PageMetadata, error)

RemovePageFromCache removes the page from disk if it exists and its metadata from the database.

func (*CachingProperties) RequestHashKey

func (p *CachingProperties) RequestHashKey(req *http.Request) []byte

RequestHashKey returns hash of a request.

type Key

type Key int

type Page

type Page struct {
	// Body is the body of the response saving to the cache.
	Body []byte

	// Header is the response header saving to the cache.
	Header http.Header
}

Page is a structure that is the cache unit storing on a disk.

type PageMetadata

type PageMetadata struct {
	// Size is the response body size.
	Size int64

	ResponseDirectives responseDirectives
}

PageMetadata contains a metadata about the page stored in a cache.

type UrlToKeyBuilder

type UrlToKeyBuilder map[string]func(r *http.Request) string

UrlToKeyBuilder is a map that contains parsers for each string. The parser takes a part of URI by string from the request. Not used.

Jump to

Keyboard shortcuts

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