localcache

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

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

Go to latest
Published: Jun 29, 2022 License: MIT Imports: 8 Imported by: 0

README

localcache

localcache in golang

  1. customize ur conf
# customize your conf name
name="LOCAL_CACHE"
# maximum slots or maximum keys you want
max_slots=10
# maximum memory you want
max_memory=1024

# choose one specific strategy, such as default/lru/lfu/fifo
cache_strategy="default"

# clean interval, and the unit is `time.Second`
clean_interval=1

cache_strategy contains default, lru, fifo, lfu, ... select one which you need.

2 init the instance

package main

import (
    "time"
	
    "github.com/guoruibiao/localcache"
)

func main() {
    confPath := "./conf/localcache.toml"
    localcache.Init(confPath)
    
    localcache.Set("name", []byte("tiger"), time.Second*2)
    println(localcache.GetString("name"))
    time.Sleep(time.Second*3)
    println(localcache.GetString("name"))
}

Documentation

Index

Constants

View Source
const (
	LocalCacheTypeDefault = "default"
	LocalCacheTypeLRU     = "lru"
	LocalCacheTypeFIFO    = "fifo"
	LocalCacheTypeLFU     = "lfu"
)

Variables

This section is empty.

Functions

func Del

func Del(key string)

func Get

func Get(key string) (val []byte)

func GetString

func GetString(key string) (val string)

func Init

func Init(configPath string) error

func Set

func Set(key string, val []byte, ttl time.Duration) error

func Statistics

func Statistics()

Types

type Cache

type Cache interface {
	// contains filtered or unexported methods
}
var Cacher Cache

func NewDefaultLocalCache

func NewDefaultLocalCache(maxSlots, maxMemory, interval int) Cache

func NewFIFOCache

func NewFIFOCache(maxSlots, maxMemory int, interval int) Cache

func NewLFUCache

func NewLFUCache(maxSlots, maxMemory int, interval int) Cache

func NewLRUCache

func NewLRUCache(maxSlots, maxMemory int, interval int) Cache

type Configuration

type Configuration struct {
	Name          string `toml:"name"`
	MaxSlots      int    `toml:"max_slots"`
	MaxMemory     int    `toml:"max_memory"`
	CacheStrategy string `toml:"cache_strategy"`
	CleanInterval int    `toml:"clean_interval"`
}

type Default

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

type FIFOCache

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

type LFUCache

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

type LRUCache

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

type MinHeap

type MinHeap []*localValue

func (*MinHeap) Index

func (mh *MinHeap) Index(ele *localValue) int

func (MinHeap) Len

func (mh MinHeap) Len() int

func (MinHeap) Less

func (mh MinHeap) Less(i, j int) bool

func (*MinHeap) Pop

func (mh *MinHeap) Pop() interface{}

func (*MinHeap) Push

func (mh *MinHeap) Push(lv interface{})

func (MinHeap) Swap

func (mh MinHeap) Swap(i, j int)

func (*MinHeap) Top

func (mh *MinHeap) Top() interface{}

Jump to

Keyboard shortcuts

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