cache

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2024 License: MIT Imports: 14 Imported by: 0

README

erqi-cache

License GitHub issues GitHub release GitHub commit activity GitHub last commit

Overview

An cache reconstructed and optimized based on go-cache. It uses sharding and atomic operations to reduce the scope of lock usage and improve concurrency. At the same time, it expands the original cleanupJanitor cleaning conditions (Maxbytes) and cleaning methods (LRU), adds an automatically saved janitor, and supports dynamic mounting of janitors. In addition, the OnExited method is added to ensure cache data security.

Installation

go get github.com/ErQi22577/erqi-cache

Usage

Create Cache with Config
package main

import (
	"fmt"
	"github.com/ErQi22577/erqi-cache"
	"time"
)

func main() {
	c := cache.New(chache.Config{
		DefaultExpiration: 0, // Never expire.
		CleanupInterval:   0, // Disable cleanupJanitor.
		SavingInterval:    0, // Disable cleanupJanitor.
		ShardCount:        0, // Number of shard maps(0 mean that default 32). 
		MaxBytes:          0, // Max use memory(0 mean that default 16G).
	})
	
	err := c.Add("key", "value", 0) // false Disable overwrite.
	if err != nil {
		fmt.Println(err)
    }
	val, found := c.Get("key1")
	if !found {
		fmt.Println("Not Found.")
    }
}
Create Cache with Items and Use Janitor
package main

import (
	"fmt"
	"github.com/ErQi22577/erqi-cache"
	"time"
)

func main() {
	// First, items must exist.
	C := cache.New(chache.Config{})
	_ = C.Add("key", "value", 0)
	items := C.Items()

	// Set the cleanup interval and save interval, 
	// and you will get the corresponding janitor.
	c := cache.NewFrom(chache.Config{
		CleanupInterval: time.Duration(14400) * time.Minute,
		SavingInterval:  time.Duration(14400) * time.Minute,
	}, items)

	// Of course, you can also choose to mount the corresponding janitor at any time.
	C.CleanupJanitor = &cache.Janitor{
		Interval: time.Duration(14400) * time.Minute,
		StopChan: make(chan bool),
		Type: cache.Cleanup,
	}
	C.RunJanitor(cache.Cleanup)
	C.StopJanitor(cache.Cleanup)
}

For more usage, see cache_test.

Methods

Add
Add(k string, x any, d time.Duration) error

k key, x value, d defaultExpiration(0:Never expire.).

Set
Add(k string, x any, d time.Duration) error

k key, x value, d defaultExpiration(0:Never expire.). If the key exists, it will be overwritten.

Delete
Delete(k string)

really remove item.

UnsafeDelete
UnsafeDelete(k string)

Only modifies item.IsDeleted to make it invisible.

Update
Update(k string, x any) error

Alter item.V if item can be found.

UpdateExpiration
UpdateExpiration(k string, d time.Duration) error

Alter item.ExpirationTime if item can be found.

Get
Get(k string) (any, bool)

Return item.V, isFound. If item has expired or isDeleted is true, will not be found.

UnsafeGet
UnsafeGet(k string) (any, bool)

Return item.V, isExist. It will also return data that has expired and isDeleted is true.

GetWithExpiration
GetWithExpiration(k string) (any, time.Time, bool)

Return item.V, item.ExpirationTime, isFound.

GetCreateTime
GetCreateTime(k string) (time.Time, bool)

Return item.CreateTime, isFound.

GetUpdateTime
GetUpdateTime(k string) (time.Time, bool)

Return item.UpdateTime, isFound.

Increment
Increment(k string, n int64) error

For int, item.V + n.

IncrementFloat
IncrementFloat(k string, n float64) error

For float, item.V + n.

Decrement
Decrement(k string, n int64) error

For int, item.V - n.

DecrementFloat
DecrementFloat(k string, n float64) error

For float, item.V - n.

ItemCount
ItemCount() (count int)

Return count(item).

RunJanitor
RunJanitor(jt janitorType)

If you set CleanupInterval or SavingInterval, you do not need to call it explicitly. Of course, you can use this method to start a custom Janitor after mounting the Cache instance. Note that jt is a janitorType and needs to reference the Janitor constant(Cleanup or Saving).

StopJanitor
StopJanitor(jt janitorType)

If you set CleanupInterval or SavingInterval, you do not need to call it explicitly. Of course, you can use this method to stop a custom Janitor after mounting the Cache instance. Note that jt is a janitorType and needs to reference the Janitor constant(Cleanup or Saving).

OnExited
OnExited(f func(), isSave bool)

Supports configuring the actions you want to do when the program exits or crashes. And also supports choosing whether to export the data in the cache after your actions.

OnEvicted
OnEvicted(f func(key string, value any))

Supports configuring the actions you want to do after Delete || UnsafeDelete || c.clean().

CacheSize
CacheSize() int

Return cache memory size.

GetShardMap
GetShardMap(key string) *SharedMap

Return key's SharedMap. It is not recommended to directly operate SharedMap.

Items
Items() map[string]Item

Exports all data in the cache for data persistence or data migration.

ItemsInit
ItemsInit(items map[string]Item)

Writes the data exported by the c.Items() into the cache.

Flush
Flush()

Clear all data in the cache.

NewFrom
NewFrom(config Config, items map[string]Item) *Cache

Encapsulates the New and c.ItemInit(). You can also use them separately. Note that the items parameter needs to be generated in advance using the c.Items(). It is not recommended to pass custom items directly, as this may cause unknown errors.

New
New(config Config) *Cache

Create Cache with Config

Author

-ErQi-Full Stack Developer-ErQi22577

Star History

Star History Chart

Documentation

Index

Constants

View Source
const (
	DefaultExpiration time.Duration = 0
	MaxBytes                        = 17179869184 // 16G
	ShardCount                      = 32
)
View Source
const (
	Cleanup janitorType = "CLEANUP"
	Saving  janitorType = "SAVING"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

func New

func New(config Config) *Cache

New create Cache with Config

func NewFrom

func NewFrom(config Config, items map[string]Item) *Cache

NewFrom Encapsulates the New and c.ItemInit(). You can also use them separately. Note that the items parameter needs to be generated in advance using the c.Items(). It is not recommended to pass custom items directly, as this may cause unknown errors.

func (Cache) Add

func (c Cache) Add(k string, x any, d time.Duration) error

Add k key, x value, d defaultExpiration(0:Never expire).

func (Cache) CacheSize

func (c Cache) CacheSize() int

CacheSize return cache memory size.

func (Cache) Decrement

func (c Cache) Decrement(k string, n int64) error

Decrement for int, item.V - n.

func (Cache) DecrementFloat

func (c Cache) DecrementFloat(k string, n float64) error

DecrementFloat for float, item.V - n.

func (Cache) Delete

func (c Cache) Delete(k string)

Delete really remove item.

func (Cache) Flush

func (c Cache) Flush()

Flush Clear all data in the cache.

func (Cache) Get

func (c Cache) Get(k string) (any, bool)

Get return item.V, isFound. If item has expired or isDeleted is true, will not be found.

func (Cache) GetCreateTime

func (c Cache) GetCreateTime(k string) (time.Time, bool)

GetCreateTime return item.CreateTime, isFound.

func (Cache) GetShardMap

func (c Cache) GetShardMap(key string) *SharedMap

GetShardMap return key's SharedMap. It is not recommended to directly operate SharedMap.

func (Cache) GetUpdateTime

func (c Cache) GetUpdateTime(k string) (time.Time, bool)

GetUpdateTime return item.UpdateTime, isFound.

func (Cache) GetWithExpiration

func (c Cache) GetWithExpiration(k string) (any, time.Time, bool)

GetWithExpiration return item.V, item.ExpirationTime, isFound.

func (Cache) Increment

func (c Cache) Increment(k string, n int64) error

Increment for int, item.V + n.

func (Cache) IncrementFloat

func (c Cache) IncrementFloat(k string, n float64) error

IncrementFloat for float, item.V + n.

func (Cache) ItemCount

func (c Cache) ItemCount() (count int)

ItemCount return count(item).

func (Cache) Items

func (c Cache) Items() map[string]Item

Items Exports all data in the cache for data persistence or data migration.

func (Cache) ItemsInit

func (c Cache) ItemsInit(items map[string]Item)

ItemsInit writes the data exported by the c.Items() into the cache.

func (Cache) OnEvicted

func (c Cache) OnEvicted(f func(key string, value any))

OnEvicted supports configuring the actions you want to do after Delete || UnsafeDelete || c.clean().

func (Cache) OnExited

func (c Cache) OnExited(f func(), isSave bool)

OnExited supports configuring the actions you want to do when the program exits or crashes. And also supports choosing whether to export the data in the cache after your actions.

func (Cache) RunJanitor

func (c Cache) RunJanitor(jt janitorType)

RunJanitor If you set CleanupInterval or SavingInterval, you do not need to call it explicitly. Of course, you can use this method to start a custom Janitor after mounting the Cache instance. Note that jt is a janitorType and needs to reference the Janitor constant(Cleanup or Saving).

func (Cache) Set added in v1.0.5

func (c Cache) Set(k string, x any, d time.Duration) error

Set k key, x value, d defaultExpiration(0:Never expire). If the key exists, it will be overwritten.

func (Cache) StopJanitor

func (c Cache) StopJanitor(jt janitorType)

StopJanitor If you set CleanupInterval or SavingInterval, you do not need to call it explicitly. Of course, you can use this method to stop a custom Janitor after mounting the Cache instance. Note that jt is a janitorType and needs to reference the Janitor constant(Cleanup or Saving).

func (Cache) UnsafeDelete

func (c Cache) UnsafeDelete(k string)

UnsafeDelete only modifies item.IsDeleted to make it invisible.

func (Cache) UnsafeGet

func (c Cache) UnsafeGet(k string) (any, bool)

UnsafeGet return item.V, isExist. It will also return data that has expired and isDeleted is true.

func (Cache) Update

func (c Cache) Update(k string, x any) error

Update alter item.V if item can be found.

func (Cache) UpdateExpiration

func (c Cache) UpdateExpiration(k string, d time.Duration) error

UpdateExpiration alter item.ExpirationTime if item can be found.

type Config

type Config struct {
	ShardCount        int
	MaxBytes          int
	DefaultExpiration time.Duration
	CleanupInterval   time.Duration
	SavingInterval    time.Duration
}

Config Note that the default values of MaxBytes and ShardCount are 16G and 32, the default values of DefaultExpiration, CleanupInterval, and SavingInterval which means that the data never expire, The CleanupJanitor and SavingJanitor are not enabled by default.

type Item

type Item struct {
	V          any
	Ele        *list.Element
	IsDeleted  bool
	Expiration int64
	CreateTime int64
	UpdateTime int64
}

type Janitor

type Janitor struct {
	Interval time.Duration
	StopChan chan bool
	Type     janitorType
}

type SharedMap

type SharedMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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