cache

package module
v0.0.0-...-bb2443e Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2021 License: MIT Imports: 3 Imported by: 0

README

lru-cache

Build Status Coverage Status

A LRU replacement cache

It comes with an in-memory store that is safe for concurrent use.

You can always roll out your own custom store implementation once it implements the Store interface.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is a LRU replacement cache

func New

func New(size int, store Store) *Cache

New returns an initialized LRU Cache

func (*Cache) Delete

func (c *Cache) Delete(key string)

Delete removes the key-value from the cache. It is a noop if no match is found.

func (*Cache) Get

func (c *Cache) Get(key string) (string, bool)

Get returns the value for the given key. It returns the value and true if found but false if otherwise

func (*Cache) Has

func (c *Cache) Has(key string) bool

Has checks if key exists in cache

func (*Cache) Set

func (c *Cache) Set(key, value string, expiresAt int)

Set inserts the value for the given key in the cache. It overrides old value if this keys already exists in the cache

func (*Cache) Size

func (c *Cache) Size() int

Size returns the size of the cache

type InMemoryStore

type InMemoryStore struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

InMemoryStore is an in-memory implementation of Store. Which is safe for concurrent use.

func (*InMemoryStore) Delete

func (i *InMemoryStore) Delete(key string)

Delete removes the given value for the key from the store.

func (*InMemoryStore) Get

func (i *InMemoryStore) Get(key string) (*list.Element, bool)

Get returns the value for the given key from the store.

func (*InMemoryStore) Set

func (i *InMemoryStore) Set(key string, value *list.Element)

Set saves the given value for the key in the store.

type Store

type Store interface {
	Get(k string) (*list.Element, bool)
	Set(k string, e *list.Element)
	Delete(k string)
}

Store is the underlying hash map used for fast access and deletion for the LRU Cache

func NewInMemoryStore

func NewInMemoryStore() Store

NewInMemoryStore creates a new in-memory store.

Jump to

Keyboard shortcuts

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