gocache

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2021 License: MIT Imports: 2 Imported by: 8

README

Build Status GoDoc Go Report Card codebeat badge codecov

gocache

An in memory flexible cache where both key and value are interfaces

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 the instance of the cache. Use New to create

func New

func New(expiration time.Duration) *Cache

New creates a new cache instance with expiration. For cache with no expiration policy provide a value of 0 or less

func (Cache) Delete added in v1.0.1

func (c Cache) Delete(key Key)

Delete will remove the item with the provided key from the cache.

func (Cache) DeleteAll added in v1.0.1

func (c Cache) DeleteAll()

DeleteAll deletes all items from the cache.

func (Cache) Evict

func (c Cache) Evict()

Evict will remove all expired items from the cache.

func (Cache) Get

func (c Cache) Get(key Key) (*Item, bool)

Get will return the key value from the cache based on Equals together with boolean which will be true if the key was found in cache and false otherwise. If the key exists based on Equals, then the *Item will be returned together with status true. If the key exists but is expired. it will be removed from the cache and the method will return nil false

func (Cache) GetAll

func (c Cache) GetAll() []Item

GetAll will return all items in the cache. If expiration on items is set GetAll will lazily remove all expired items and return the rest

func (Cache) ItemCount

func (c Cache) ItemCount() int

ItemCount will return the count of the items in the list. Expired items will be included in the count

func (Cache) Set

func (c Cache) Set(key Key, val interface{})

Set will add the key value in the cache. If the key already exists based on Equals, then the value and expiration will be updated. If the key already exists but is expired. it will be removed from the cache and the key value pair will be added as a new Item If the key does not exist a new Item will be added in the cache

type Item

type Item struct {
	Key   Key
	Value interface{}
	// contains filtered or unexported fields
}

Item is the key value pair together with the expiration (if applicable) Both key and value are interfaces

type Key

type Key interface {
	Equals(key Key) bool
}

Key is the interface that is used as the key for the cache Items

Jump to

Keyboard shortcuts

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