Documentation ¶
Index ¶
- Constants
- Variables
- func CloseDatabase(db *bolt.DB)
- func LoggerConfig(prefix string)
- func OpenDatabase(path string) (*bolt.DB, error)
- type CacheCleaner
- type CachingProperties
- func (p *CachingProperties) CalculateSize()
- func (p *CachingProperties) Cleaner() *CacheCleaner
- func (p *CachingProperties) ClearCache() error
- func (p *CachingProperties) DB() *bolt.DB
- func (p *CachingProperties) GetPageFromCache(key []byte, req *http.Request) (*Page, error)
- func (p *CachingProperties) IncrementSize(delta int64)
- func (p *CachingProperties) InsertPageInCache(key []byte, req *http.Request, resp *http.Response, page *Page) error
- func (p *CachingProperties) Observe()
- func (p *CachingProperties) RemovePageFromCache(key []byte) (*PageMetadata, error)
- func (p *CachingProperties) RequestHashKey(req *http.Request) []byte
- type Key
- type Page
- type PageMetadata
- type UrlToKeyBuilder
Constants ¶
const ( // OnlyIfCachedKey is used for saving to request context the directive // 'only-if-cached' from Cache-Control. OnlyIfCachedKey = Key(iota) Hash )
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 ¶
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 LoggerConfig ¶
func LoggerConfig(prefix string)
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 ¶
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 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.