ridata

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2020 License: MIT Imports: 7 Imported by: 0

README

GoDoc Build Status Coverage Status Go Report Card

lru

An LRU implementation. Accessors available for the first item, last item, list of all keys, count, fullness, membership checks, explicit drops, and popping the oldest in a loop. You can also set a callback for when items are dropped and dump the current contents of the LRU.

mimetype

Convenience function for determining a mime-type from an io.Reader.

Documentation

Index

Constants

View Source
const (
	MimetypeLeadBytesCount = 512
)

Variables

View Source
var (
	// ErrLruEmpty indicates that the LRU is empty..
	ErrLruEmpty = errors.New("lru is empty")
)

Functions

func DetectMimetype

func DetectMimetype(f *os.File) (mimetype string, err error)

DetectMimetype is a wrapper for GetMimetypeFromContent which returns the mime-type for the given `File`. An empty-string is returned if it is a zero- length file.

func GetMimetypeFromContent

func GetMimetypeFromContent(r io.Reader, fileSize int64) (mimetype string, err error)

GetMimetypeFromContent uses net/http to map from magic-bytes to mime-type.

Types

type Lru

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

Lru establises an LRU of IDs of any type.

func NewLru

func NewLru(maxSize int) *Lru

NewLru returns a new instance.

func (*Lru) All

func (lru *Lru) All() []LruKey

All returns a list of all IDs.

func (*Lru) Count

func (lru *Lru) Count() int

Count returns the number of items in the LRU.

func (*Lru) Drop

func (lru *Lru) Drop(id LruKey) (found bool, err error)

Drop discards the given item.

func (*Lru) Dump

func (lru *Lru) Dump()

Dump returns a list of all IDs.

func (*Lru) Exists

func (lru *Lru) Exists(id LruKey) bool

Exists will do a membership check for the given key.

func (*Lru) FindPosition

func (lru *Lru) FindPosition(id LruKey) int

FindPosition will return the numerical position in the list. Since the LRU will never be very large, this call is not expensive, per se. But, it *is* O(n) and any call to us will compound with any loops you happen to wrap us into.

func (*Lru) Get

func (lru *Lru) Get(id LruKey) (found bool, item LruItem, err error)

Get touches the cache and returns the data.

func (*Lru) IsFull

func (lru *Lru) IsFull() bool

IsFull will return true if at capacity.

func (*Lru) MaxCount

func (lru *Lru) MaxCount() int

MaxCount returns the maximum number of items the LRU can contain.

func (*Lru) Newest

func (lru *Lru) Newest() LruKey

Newest returns the most recently used ID.

func (*Lru) Oldest

func (lru *Lru) Oldest() LruKey

Oldest returns the least recently used ID.

func (*Lru) PopOldest

func (lru *Lru) PopOldest() (item LruItem, err error)

PopOldest will pop the oldest entry out of the LRU and return it. It will return ErrLruEmpty when empty.

func (*Lru) Set

func (lru *Lru) Set(item LruItem) (added bool, droppedItem LruItem, err error)

Set bumps an item to the front of the LRU. It will be added if it doesn't already exist. If as a result of adding an item the LRU exceeds the maximum size, the least recently used item will be discarded.

If it was not previously in the LRU, `added` will be `true`.

func (*Lru) SetDropCb

func (lru *Lru) SetDropCb(cb lruEventFunc)

SetDropCb sets a callback that will be triggered whenever an item ages out or is manually dropped.

type LruItem

type LruItem interface {
	Id() LruKey
}

LruItem is the interface that any item we add to the LRU must satisfy.

type LruKey

type LruKey interface{}

LruKey is the type of an LRU key.

Jump to

Keyboard shortcuts

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