disklru

package module
v0.0.0-...-4dd9ec4 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2014 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

disklru

Package disklru implements a disk-based LRU cache.

License

MIT

Documentation

Overview

Package disklru implements a disk-based LRU cache.

A small key-value store (powered by kv [1]) keeps track of the keys/size/last access time of each time, and data are stored in files (one file per blob, in nested directory like "blobs/07/077f2cfed67c84d8785515481766669a3a734bd6".

Eviction is triggered when the cache size exceed a limit, it tries to keep the size of the cache under a fixed threshold and remove lest recently used item first.

Index

Data are stored the following way (all the uint32 are binary encoded):

  • Index byte + last access time (uint32) + key => size (uint32)
  • Keys byte + key => current access time
  • MetaCnt byte => number of items currently in the cache (uint32)
  • MetaSize byte => total size of the cache (uint32)

Links

[1] https://github.com/cznic/kv

Index

Constants

View Source
const (
	Keys byte = iota
	Index
	MetaSize
	MetaCnt
)

Namespaces for the DB keys

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheItem

type CacheItem struct {
	Key   string
	Index uint32
	Size  uint32
}

type DiskLRU

type DiskLRU struct {
	Func    func(string, interface{}) ([]byte, error)
	MaxSize uint32

	sync.Mutex
	// contains filtered or unexported fields
}

func New

func New(path string, f func(string, interface{}) ([]byte, error), maxSize uint32) (*DiskLRU, error)

New initialize a new DiskLRU.

func (*DiskLRU) Close

func (lru *DiskLRU) Close()

Close cleanly close the kv db.

func (*DiskLRU) Cnt

func (lru *DiskLRU) Cnt() uint32

Cnt returns the number of items in the cache.

func (*DiskLRU) Get

func (lru *DiskLRU) Get(key string, whatever interface{}) (data []byte, fetched bool, err error)

Get the value for the given key, call Func if the key isn't stored yet.

func (*DiskLRU) Remove

func (lru *DiskLRU) Remove()

func (*DiskLRU) Size

func (lru *DiskLRU) Size() uint32

Size returns the total size of data stored to disk.

Jump to

Keyboard shortcuts

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