cache

package
v4.4.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2017 License: AGPL-3.0, AGPL-3.0-or-later Imports: 4 Imported by: 0

Documentation

Overview

Package cache provides an in-memory LRU cache for reducing the duplicate workload of database requests and post HTML and JSON generation

Index

Constants

This section is empty.

Variables

View Source
var (

	// Size sets the maximum size of cache before evicting unread data in MB
	Size float64 = 1 << 7
)

Functions

func Clear

func Clear()

Clear the cache. Only used for testing.

func GetHTML

func GetHTML(k Key, f FrontEnd) ([]byte, interface{}, uint64, error)

GetHTML retrieves post HTML from the cache or generates fresh HTML as needed

func GetJSONAndData

func GetJSONAndData(k Key, f FrontEnd) ([]byte, interface{}, uint64, error)

GetJSON retrieves JSON from the cache along with unencoded post data, validates, if is still fresh, or retrieves fresh data, if needed

Types

type FrontEnd

type FrontEnd struct {
	// GetCounter retrieves a new update counter for a resource
	GetCounter func(Key) (uint64, error)

	// GetFresh retrieves new post data from the database
	GetFresh func(Key) (interface{}, error)

	// Encode data into JSON. If null, default encoder is used.
	EncodeJSON func(data interface{}) ([]byte, error)

	// RenderHTML produces HTML from the passed in data and JSON
	RenderHTML func(interface{}, []byte) []byte

	// Calculates the size taken by the store.
	// If nil, the default function is used.
	Size func(data interface{}, json, html []byte) int
}

FrontEnd provides functions for fetching, validating and generating the cache. GetCounter and GetFresh are mandatory, but you may omit RenderHTML, if you don't plan to call GetHTML with this FrontEnd.

type Key

type Key struct {
	LastN uint8
	Board string
	ID    uint64
	Page  int64
}

Key stores the ID of either a thread or board page

func BoardKey

func BoardKey(b string, page int64, index bool) Key

BoardKey encodes a key for a board page resource

func ThreadKey

func ThreadKey(id uint64, lastN int) Key

ThreadKey encodes a Key from a thread's ID and last N posts to show setting

Jump to

Keyboard shortcuts

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