lru

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

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

Go to latest
Published: Dec 8, 2014 License: MIT Imports: 8 Imported by: 1

README

lru

This library is an easy way to manage a cache to store bytes on disk.

For now, this cache:

  • doesn't allow updates (no update implemented yet)
  • is never going over the limit (we check before writing to the disk instead of triming after)
  • is supposed to be empty at initialization (we do not delete anything, but a 2 keys on 2 differents instances will have the same file name and so value will be the same as the first one written to disk)
  • is goroutine safe (it has a lock mechanism)

Installation

go get github.com/dakis/lru

Initialize the cache

cache := lru.New(CACHE_SIZE, "/tmp")
defer cache.Clear()

Get a value

cache := lru.New(CACHE_SIZE, "/tmp")
defer cache.Clear()

if data, ok := cache.Get("key); ok {
    fmt.Println("value was retrieved")
} else {
    fmt.Println("value doesn't exist")
}

Set a value

cache := lru.New(CACHE_SIZE, "/tmp")
defer cache.Clear()

cache.Set(, "/tmp")

Delete a value

cache := lru.New(CACHE_SIZE, "/tmp")
defer cache.Clear()

cache.Set("key", []byte("test value"))

if cache.Delete("key") {
    fmt.Println("value was delete")
}

Full examples in https://github.com/dakis/lru/tree/master/examples.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func New

func New(maxSize uint64, path string, options Options) *Cache

func (*Cache) Clear

func (cache *Cache) Clear()

func (*Cache) Debug

func (cache *Cache) Debug(msg string)

func (*Cache) Delete

func (cache *Cache) Delete(key string) bool

func (*Cache) FilePath

func (cache *Cache) FilePath(key string) string

func (*Cache) Get

func (cache *Cache) Get(key string) ([]byte, bool)

func (*Cache) MaxSize

func (cache *Cache) MaxSize() uint64

func (*Cache) Oldest

func (cache *Cache) Oldest() (oldest time.Time)

func (*Cache) Set

func (cache *Cache) Set(key string, value []byte)

func (*Cache) Size

func (cache *Cache) Size() uint64

type Options

type Options struct {
	ClearCacheOnBoot bool
	Debug            bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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