cache

package
v0.0.0-...-f5d25ad Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2020 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// FetchAndPin fetches a page from this cache. If it does not exist in the
	// cache, it must be loaded from a configured source. After the page was
	// fetched, it is pinned, meaning that it's guaranteed that the page is not
	// evicted. After working with the page, it must be released again, in order
	// for the cache to be able to free memory. If a page with the given id does
	// not exist, an error will be returned.
	FetchAndPin(id page.ID) (*page.Page, error)
	// Unpin tells the cache that the page with the given id is no longer
	// required directly, and that it can be evicted. Unpin is not a guarantee
	// that the page will be evicted. The cache determines, when to evict a
	// page. If a page with that id does not exist, this call is a no-op.
	Unpin(id page.ID)
	// Flush writes the contents of the page with the given id to the configured
	// source. Before a page is evicted, it is always flushed. Use this method
	// to tell the cache that the page must be flushed immediately. If a page
	// with the given id does not exist, an error will be returned.
	Flush(id page.ID) error
	io.Closer
}

Cache describes a page cache that caches pages from a secondary storage.

type LRUCache

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

LRUCache is a simple implementation of an LRU cache.

func NewLRUCache

func NewLRUCache(size int, store SecondaryStorage) *LRUCache

NewLRUCache creates a new LRU cache with the given size and secondary storage to write dirty pages to. The size is the maximum amount of pages, that can be held by this cache. If more pages than the size are requested, old pages will be evicted from the cache. If all pages are pinned (in use and not released yet), requesting a new page will fail.

func (*LRUCache) Close

func (c *LRUCache) Close() error

Close will flush all dirty pages and then close this cache.

func (*LRUCache) FetchAndPin

func (c *LRUCache) FetchAndPin(id page.ID) (*page.Page, error)

FetchAndPin will return the page with the given ID. This will fail, if the page with the given ID is not in the cache, but the cache is full and all pages are pinned. After obtaining a page with this method, you MUST release it, once you are done using it, with Unpin(ID).

func (*LRUCache) Flush

func (c *LRUCache) Flush(id page.ID) error

Flush writes the contents of the page with the given ID to secondary storage. This fails, if the page with the given ID is not in the cache anymore. Only call this if you know what you are doing. Pages will always be flushed before being evicted. If you really do need to use this, call it before unpinning the page, to guarantee that the page will not be evicted between unpinning and flushing.

func (*LRUCache) Unpin

func (c *LRUCache) Unpin(id page.ID)

Unpin unpins the page with the given ID. If the page with the given ID is not pinned, then this is a no-op.

type SecondaryStorage

type SecondaryStorage interface {
	ReadPage(page.ID) (*page.Page, error)
	WritePage(*page.Page) error
}

SecondaryStorage is the abstraction that a cache uses, to synchronize dirty pages with secondary storage.

Jump to

Keyboard shortcuts

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