core

package
v0.0.0-...-9d41389 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2021 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

package core defines some core function to a cache ,and provides a lru implement

package core defines a goroutines unsafely core cache implements

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {

	// optional and executed when an entry is purged.
	OnEvicted func(key string, value interface{})
	// contains filtered or unexported fields
}

Cache is a LRU cache. It is not safe for concurrent access.

func NewLRUCache

func NewLRUCache(maxBytes int64, onEvicted func(key string, value interface{})) *Cache

NewLRUCache returns a pointer to a lru cache maxBytes: max usage memory size onEvicted: after func call when keys evicted

func (*Cache) Add

func (c *Cache) Add(key string, value Value)

Add adds a value to the cache.

func (*Cache) Get

func (c *Cache) Get(key string) (v Value, ok bool)

Get look ups a key's value

func (*Cache) Len

func (c *Cache) Len() int

Len the number of cache entries

func (*Cache) RemoveOldest

func (c *Cache) RemoveOldest()

RemoveOldest removes the oldest item

type CacheCore

type CacheCore interface {
	Get(key string) (Value, bool)
	Add(key string, value Value)
}

CacheCore interface defines a a cache what should do has some implements ,like FIFO, LFU, LRU, e.g.

type Value

type Value interface {
	Len() int
}

Value use Len to count how many bytes it takes

Jump to

Keyboard shortcuts

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