cache

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

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

Go to latest
Published: Nov 25, 2024 License: MIT Imports: 3 Imported by: 1

README

🪡cache

a simple cache framework for golang

golang

introduction

This is a cache framework implemented by Golang. This framework implements the FIFO and LRU algorithms for managing cache and is thread safe. The algorithm needs to be selected based on the actual situation, and using FIFO in an environment with more reads and less writes will result in higher performance.

installing

Select the version to install

go get github.com/go-needle/cache@version

If you have already get , you may need to update to the latest version

go get -u github.com/go-needle/cache

quickly start

package main

import (
	"fmt"
	"github.com/go-needle/cache"
	"math/rand"
	"strconv"
	"time"
)

func main() {
	c := cache.NewLRU(1024, time.Duration(10)*time.Second)
	//c := cache.NewFIFO(1<<32, time.Duration(10)*time.Second)
	for i := 0; i < 1000; i++ {
		num := i
		go func() {
			time.Sleep(time.Duration(rand.Intn(10)) * time.Second)
			c.Add("key"+strconv.Itoa(num), []byte(strconv.Itoa(num)))
		}()
	}
	for i := 0; i < 1000; i++ {
		num := i
		go func() {
			time.Sleep(time.Duration(rand.Intn(20)) * time.Second)
			v, ok := c.Get("key" + strconv.Itoa(num))
			fmt.Println(v.String(), ok)
		}()
	}
	time.Sleep(time.Duration(20) * time.Second)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloneBytes

func CloneBytes(b []byte) []byte

Types

type ByteView

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

A ByteView holds an immutable view of bytes.

func NewByteView

func NewByteView(b []byte) ByteView

NewByteView creates a new ByteView struct

func (ByteView) ByteSlice

func (v ByteView) ByteSlice() []byte

ByteSlice returns a copy of the data as a byte slice.

func (ByteView) ByteSource

func (v ByteView) ByteSource() []byte

ByteSource returns the byte slice of source.

func (ByteView) Len

func (v ByteView) Len() int

Len returns the view's length

func (ByteView) String

func (v ByteView) String() string

String returns the data as a string, making a copy if necessary.

type Cache

type Cache interface {
	Add(key string, value []byte)
	Get(key string) (ByteView, bool)
}

type FIFOCache

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

func (*FIFOCache) Add

func (c *FIFOCache) Add(key string, value []byte)

Add is safe for concurrent access.

func (*FIFOCache) Get

func (c *FIFOCache) Get(key string) (ByteView, bool)

Get is safe for concurrent access.

type LRUCache

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

func NewFIFO

func NewFIFO(cacheBytes int64, keySurvivalTime time.Duration) *LRUCache

NewFIFO creates a new cache struct and use the FIFO algorithm

func NewLRU

func NewLRU(cacheBytes int64, keySurvivalTime time.Duration) *LRUCache

NewLRU creates a new cache struct and use the LRU algorithm

func (*LRUCache) Add

func (c *LRUCache) Add(key string, value []byte)

Add is safe for concurrent access.

func (*LRUCache) Get

func (c *LRUCache) Get(key string) (ByteView, bool)

Get is safe for concurrent access.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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