cache

package module
v0.0.0-...-99efbde Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2018 License: Apache-2.0 Imports: 3 Imported by: 0

README

cache

A golang memcache with LRU to swap

Install

go get github.com/g4zhuj/cache

Usage

init
// If maxItemSize is zero, the cache has no limit.
	var cache Cache
	maxSize = 1024
	cache = NewMemCache(maxSize)
Get
value, ok := cache.Get("key")
Set
cache.Set("key", "value")
Delete
cache.Delete("delKey")
Status
status := cache.Status()
fmt.Println("Gets Count: ",	status.Gets)
fmt.Println("Hits Count: ",	status.Hits)
fmt.Println("MaxItemSize: ",	status.MaxItemSize)
fmt.Println("CurrentSize: ",	status.CurrentSize)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtomicInt

type AtomicInt int64

An AtomicInt is an int64 to be accessed atomically.

func (*AtomicInt) Add

func (i *AtomicInt) Add(n int64)

Add atomically adds n to i.

func (*AtomicInt) Get

func (i *AtomicInt) Get() int64

Get atomically gets the value of i.

type Cache

type Cache interface {
	Set(key string, value interface{})
	Get(key string) (interface{}, bool)
	Delete(key string)
	Status() *CacheStatus
}

this is a interface which defines some common functions

type CacheStatus

type CacheStatus struct {
	Gets        int64
	Hits        int64
	MaxItemSize int
	CurrentSize int
}

return status of chache

type MemCache

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

MemCache is an LRU cache. It is safe for concurrent access.

func NewMemCache

func NewMemCache(maxItemSize int) *MemCache

NewMemCache If maxItemSize is zero, the cache has no limit. if maxItemSize is not zero, when cache's size beyond maxItemSize,start to swap

func (*MemCache) Delete

func (c *MemCache) Delete(key string)

Delete delete the key

func (*MemCache) Get

func (c *MemCache) Get(key string) (interface{}, bool)

Get value with key

func (*MemCache) RemoveOldest

func (c *MemCache) RemoveOldest()

RemoveOldest remove the oldest key

func (*MemCache) Set

func (c *MemCache) Set(key string, value interface{})

Set a value with key

func (*MemCache) Status

func (c *MemCache) Status() *CacheStatus

Status return the status of cache

Jump to

Keyboard shortcuts

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