cache

package
v0.0.0-...-ff8e54b Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2020 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 int
)

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, Key) []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 {
	Lang    string
	Board   string
	ID      uint64
	LastN   int
	Page    int
	Catalog bool
}

Represents some cached object.

func BoardKey

func BoardKey(l, b string, page int, catalog bool) Key

BoardKey encodes a key for a board page resource

func ThreadKey

func ThreadKey(l string, 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