gocache

package module
v0.0.0-...-3d635a1 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2019 License: MIT Imports: 5 Imported by: 0

README

gocache

简介

gocache 使用分片 + 锁实现进程内缓存。

缓存根据键的hash值分片到多个储存空间,每个储存空间单独加锁。

  1. 实现了滑动过期和绝对过期
  2. 实现了滑动过期指定创建函数再次生成缓存
  3. 实现缓存过期删除时的回调事件

使用

使用内置缓存
package main

import (
	"github.com/lifei6671/gocache"
	"log"
	"time"
)

func main() {
	
	gocache.AddWithCacheItem(gocache.CacheItem{Key: "cache", Value: "cache-1", RemovedCallback: func(key string, value interface{}, reason gocache.CacheEntryRemovedReason) {
		log.Println(key, value, reason)
	}})
	gocache.Add("cache", "cache-2")

	log.Println(gocache.Count())
	log.Println(gocache.Get("cache"))

}
使用自定义缓存
package main

import (
	"github.com/lifei6671/gocache"
	"log"
	"time"
)

func main() {
	m := gocache.NewMemoryCache(time.Second * 2)

	m.AddWithCacheItem(gocache.CacheItem{Key: "cache", Value: "cache-1", RemovedCallback: func(key string, value interface{}, reason gocache.CacheEntryRemovedReason) {
		log.Println(key, value, reason)
	}})
	m.Add("cache", "cache-2")

	log.Println(m.Count())
	log.Println(m.Get("cache"))

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MaxTimeValue = time.Date(9999, 12, 31, 23, 59, 59, 9999999, time.Local)
View Source
var SHARD_COUNT = 32

Functions

func Add

func Add(key string, value interface{})

func AddWithAbsoluteExpiration

func AddWithAbsoluteExpiration(key string, value interface{}, expiration time.Time)

func AddWithCacheItem

func AddWithCacheItem(item CacheItem)

func AddWithSlidingExpiration

func AddWithSlidingExpiration(key string, value interface{}, expiration time.Duration)

增加缓存并设置策略

func ContainsKey

func ContainsKey(key string) bool

ContainsKey 判断缓存中是否存在指定键

func Count

func Count() int

func Get

func Get(key string) (value interface{}, ok bool)

Types

type CacheEntryCreateCallback

type CacheEntryCreateCallback func(key string, oldValue interface{}) (value interface{}, err error)

当缓存失效后,可以指定重新生成缓存的回调方法

type CacheEntryRemovedCallback

type CacheEntryRemovedCallback func(key string, value interface{}, reason CacheEntryRemovedReason)

定义对某个方法的引用,在从缓存中移除某个缓存项后将调用该方法

type CacheEntryRemovedReason

type CacheEntryRemovedReason int

指定已移除或将要移除某个缓存项的原因

const (
	// 主动移除
	CacheEntryRemovedReasonRemoved CacheEntryRemovedReason = iota
	//某个缓存项由于已过期而被移除。 过期可基于绝对过期时间或可调过期时间
	CacheEntryRemovedReasonExpired

	//某个缓存项由于释放缓存中的内存的原因而被移除。
	// 当某个缓存实例将超出特定于缓存的内存限制或某个进程或缓存实例将超出整个计算机范围的内存限制时,会发生这种情况
	CacheEntryRemovedReasonEvicted

	//某个缓存项由于相关依赖项(如一个文件或其他缓存项)触发了其逐出操作而被移除
	CacheEntryRemovedReasonChangeMonitorChanged

	//某个缓存项由于特定缓存实现定义的原因而被逐出
	CacheEntryRemovedReasonCacheSpecificEviction
)

type CacheEntryUpdateCallback

type CacheEntryUpdateCallback func(key string, value interface{}, reason CacheEntryRemovedReason)

定义对某个方法的引用,在即将从缓存中移除某个缓存项时将调用该方法

type CacheItem

type CacheItem struct {
	sync.RWMutex
	Key   string
	Value interface{}

	RemovedCallback CacheEntryRemovedCallback
	CreateCallback  CacheEntryCreateCallback
	// contains filtered or unexported fields
}

func NewCacheItem

func NewCacheItem(key string, value interface{}) *CacheItem

func NewCacheItemWithAbsoluteExpiration

func NewCacheItemWithAbsoluteExpiration(key string, value interface{}, expiration time.Time) *CacheItem

func NewCacheItemWithSlidingExpiration

func NewCacheItemWithSlidingExpiration(key string, value interface{}, expiration time.Duration) *CacheItem

func (*CacheItem) HasExpiration

func (item *CacheItem) HasExpiration() bool

是否存在过期日期设置

func (*CacheItem) InExpires

func (item *CacheItem) InExpires() bool

是否已过期

type MemoryCache

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

func NewMemoryCache

func NewMemoryCache(interval time.Duration) *MemoryCache

func (*MemoryCache) Add

func (memory *MemoryCache) Add(key string, value interface{})

func (*MemoryCache) AddWithAbsoluteExpiration

func (memory *MemoryCache) AddWithAbsoluteExpiration(key string, value interface{}, expiration time.Time)

func (*MemoryCache) AddWithCacheItem

func (memory *MemoryCache) AddWithCacheItem(item CacheItem)

func (*MemoryCache) AddWithSlidingExpiration

func (memory *MemoryCache) AddWithSlidingExpiration(key string, value interface{}, expiration time.Duration)

增加缓存并设置策略

func (*MemoryCache) Clear

func (memory *MemoryCache) Clear()

func (*MemoryCache) Close

func (memory *MemoryCache) Close()

func (*MemoryCache) ContainsKey

func (memory *MemoryCache) ContainsKey(key string) bool

ContainsKey 判断缓存中是否存在指定键

func (*MemoryCache) Count

func (memory *MemoryCache) Count() int

func (*MemoryCache) Get

func (memory *MemoryCache) Get(key string) (value interface{}, ok bool)

func (*MemoryCache) Remove

func (memory *MemoryCache) Remove(key string) (value interface{}, ok bool)

type MemoryCacheStore

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

func NewMemoryCacheStore

func NewMemoryCacheStore(ctx context.Context, regionName string, interval time.Duration) *MemoryCacheStore

func (*MemoryCacheStore) Add

func (store *MemoryCacheStore) Add(item *CacheItem)

func (*MemoryCacheStore) AddWithAbsoluteExpiration

func (store *MemoryCacheStore) AddWithAbsoluteExpiration(key string, value interface{}, expiration time.Time)

func (*MemoryCacheStore) AddWithSlidingExpiration

func (store *MemoryCacheStore) AddWithSlidingExpiration(key string, value interface{}, expiration time.Duration)

func (*MemoryCacheStore) Clear

func (store *MemoryCacheStore) Clear()

func (*MemoryCacheStore) Close

func (store *MemoryCacheStore) Close()

func (*MemoryCacheStore) ContainsKey

func (store *MemoryCacheStore) ContainsKey(key string) bool

func (*MemoryCacheStore) Count

func (store *MemoryCacheStore) Count() int

func (*MemoryCacheStore) GetValue

func (store *MemoryCacheStore) GetValue(key string) (value interface{}, ok bool)

func (*MemoryCacheStore) Remove

func (store *MemoryCacheStore) Remove(key string) (value interface{}, ok bool)

type NoSlidingExpiration

type NoSlidingExpiration time.Duration

基于持续时间或定义的时间窗口过期也被称为可调过期。 通常情况下,逐出基于可调过期的项的缓存实现将删除在指定的时间段未被访问的项。 插入到缓存与某个缓存项NoSlidingExpiration字段值设置为过期值应该永远不会收回由于非活动的滑动时间窗口中。 但是,如果具有绝对到期,或某些其他逐出事件发生时,此类更改监视器或内存压力,可以逐出缓存项。

type SlidingExpiration

type SlidingExpiration time.Duration

一个时段,必须在此时段内访问某个缓存项,否则将从内存中逐出该缓存项

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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