...

Package ridata

import "github.com/dsoprea/go-utility/v2/data"
Overview
Index

Overview ▾

Constants

const (
    // MimetypeLeadBytesCount is the number of bytes to use for detection.
    MimetypeLeadBytesCount = 512
)

Variables

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

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.

type Lru

Lru establises an LRU of IDs of any type.

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

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

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

type LruItem interface {
    Id() LruKey
}

type LruKey

LruKey is the type of an LRU key.

type LruKey interface{}