Documentation
¶
Index ¶
- Constants
- func NewCacheWithReport(opts ...Option) (cache Cache, reporter *Reporter)
- func RunGCTask(cache Cache, duration time.Duration) (cancel func())
- func SetMapInitialCap(initialCap int)
- func SetSliceInitialCap(initialCap int)
- type Cache
- type CacheType
- type Option
- func WithCacheName(cacheName string) Option
- func WithDisableSingleflight() Option
- func WithGC(gcDuration time.Duration) Option
- func WithHash(hash func(key string) int) Option
- func WithLFU(maxEntries int) Option
- func WithLRU(maxEntries int) Option
- func WithMaxEntries(maxEntries int) Option
- func WithMaxScans(maxScans int) Option
- func WithNow(now func() int64) Option
- func WithRecordGC(recordGC bool) Option
- func WithRecordHit(recordHit bool) Option
- func WithRecordLoad(recordLoad bool) Option
- func WithRecordMissed(recordMissed bool) Option
- func WithReportGC(reportGC func(reporter *Reporter, cost time.Duration, cleans int)) Option
- func WithReportHit(reportHit func(reporter *Reporter, key string, value interface{})) Option
- func WithReportLoad(...) Option
- func WithReportMissed(reportMissed func(reporter *Reporter, key string)) Option
- func WithShardings(shardings int) Option
- type Reporter
- func (r *Reporter) CacheGC() time.Duration
- func (r *Reporter) CacheName() string
- func (r *Reporter) CacheShardings() int
- func (r *Reporter) CacheSize() int
- func (r *Reporter) CacheType() CacheType
- func (r *Reporter) CountGC() uint64
- func (r *Reporter) CountHit() uint64
- func (r *Reporter) CountLoad() uint64
- func (r *Reporter) CountMissed() uint64
- func (r *Reporter) HitRate() float64
- func (r *Reporter) MissedRate() float64
Constants ¶
const (
// NoTTL means a key is never expired.
NoTTL = 0
)
Variables ¶
This section is empty.
Functions ¶
func NewCacheWithReport ¶ added in v0.4.6
NewCacheWithReport creates a cache and a reporter with options. By default, it will create a standard cache which uses one lock to solve data race. It may cause a big performance problem in high concurrency. You can use WithShardings to create a sharding cache which is good for concurrency. Also, you can use options to specify the type of cache to others, such as lru.
func RunGCTask ¶ added in v0.4.6
RunGCTask runs a gc task in a new goroutine and returns a cancel function to cancel the task. However, you don't need to call it manually for most time, instead, use options is a better choice. Making it a public function is for more customizations in some situations. For example, using options to run gc task is un-cancelable, so you can use it to run gc task by your own and get a cancel function to cancel the gc task.
func SetMapInitialCap ¶ added in v0.4.6
func SetMapInitialCap(initialCap int)
SetMapInitialCap sets the initial capacity of map.
func SetSliceInitialCap ¶ added in v0.4.6
func SetSliceInitialCap(initialCap int)
SetSliceInitialCap sets the initial capacity of slice.
Types ¶
type Cache ¶
type Cache interface { // Get gets the value of key from cache and returns value if found. // A nil value will be returned if key doesn't exist in cache. // Notice that we won't remove expired keys in get method, so you should remove them manually or set a limit of keys. // The reason why we won't remove expired keys in get method is for higher re-usability, because we often set a new value // of expired key after getting it (so we can reuse the memory of entry). Get(key string) (value interface{}, found bool) // Set sets key and value to cache with ttl and returns evicted value if exists. // See NoTTL if you want your key is never expired. Set(key string, value interface{}, ttl time.Duration) (evictedValue interface{}) // Remove removes key and returns the removed value of key. // A nil value will be returned if key doesn't exist in cache. Remove(key string) (removedValue interface{}) // Size returns the count of keys in cache. // The result may be different in different implements. Size() (size int) // GC cleans the expired keys in cache and returns the exact count cleaned. // The exact cleans depend on implements, however, all implements should have a limit of scanning. GC() (cleans int) // Reset resets cache to initial status which is like a new cache. Reset() // Load loads a key with ttl to cache and returns an error if failed. // We recommend you use this method to load missed keys to cache, // because it may use singleflight to reduce the times calling load function. Load(key string, ttl time.Duration, load func() (value interface{}, err error)) (value interface{}, err error) }
Cache is the core interface of cachego. We provide some implements including standard cache and sharding cache.
func NewCache ¶
NewCache creates a cache with options. By default, it will create a standard cache which uses one lock to solve data race. It may cause a big performance problem in high concurrency. You can use WithShardings to create a sharding cache which is good for concurrency. Also, you can use options to specify the type of cache to others, such as lru. Use NewCacheWithReporter to get a reporter for use if you want.
type CacheType ¶ added in v0.4.10
type CacheType string
CacheType is the type of cache.
func (CacheType) IsStandard ¶ added in v0.4.10
IsStandard returns if cache type is standard.
type Option ¶ added in v0.2.1
type Option func(conf *config)
Option applies to config and sets some values to config.
func WithCacheName ¶ added in v0.4.8
WithCacheName returns an option setting the cacheName of config.
func WithDisableSingleflight ¶ added in v0.3.2
func WithDisableSingleflight() Option
WithDisableSingleflight returns an option turning off singleflight mode of cache.
func WithGC ¶ added in v0.4.6
WithGC returns an option setting the duration of cache gc. Negative value means no gc.
func WithHash ¶ added in v0.4.6
WithHash returns an option setting the hash function of cache. A hash function should return the hash code of key.
func WithLFU ¶ added in v0.4.6
WithLFU returns an option setting the type of cache to lfu. Notice that lfu cache must have max entries limit, so you have to specify a maxEntries.
func WithLRU ¶ added in v0.4.6
WithLRU returns an option setting the type of cache to lru. Notice that lru cache must have max entries limit, so you have to specify a maxEntries.
func WithMaxEntries ¶ added in v0.4.6
WithMaxEntries returns an option setting the max entries of cache. Negative value means no limit.
func WithMaxScans ¶ added in v0.4.6
WithMaxScans returns an option setting the max scans of cache. Negative value means no limit.
func WithNow ¶ added in v0.4.6
WithNow returns an option setting the now function of cache. A now function should return a nanosecond unix time.
func WithRecordGC ¶ added in v0.4.6
WithRecordGC returns an option setting the recordGC of config.
func WithRecordHit ¶ added in v0.4.6
WithRecordHit returns an option setting the recordHit of config.
func WithRecordLoad ¶ added in v0.4.6
WithRecordLoad returns an option setting the recordLoad of config.
func WithRecordMissed ¶ added in v0.4.6
WithRecordMissed returns an option setting the recordMissed of config.
func WithReportGC ¶ added in v0.4.6
WithReportGC returns an option setting the reportGC of config.
func WithReportHit ¶ added in v0.4.6
WithReportHit returns an option setting the reportHit of config.
func WithReportLoad ¶ added in v0.4.6
func WithReportLoad(reportLoad func(reporter *Reporter, key string, value interface{}, ttl time.Duration, err error)) Option
WithReportLoad returns an option setting the reportLoad of config.
func WithReportMissed ¶ added in v0.4.6
WithReportMissed returns an option setting the reportMissed of config.
func WithShardings ¶ added in v0.4.6
WithShardings returns an option setting the sharding count of cache. Negative value means no sharding.
type Reporter ¶ added in v0.4.6
type Reporter struct {
// contains filtered or unexported fields
}
Reporter stores some values for reporting.
func (*Reporter) CacheGC ¶ added in v0.4.12
CacheGC returns the gc duration of cache. You can use WithGC to set cache's gc duration. Zero duration means cache disables gc.
func (*Reporter) CacheName ¶ added in v0.4.8
CacheName returns the name of cache. You can use WithCacheName to set cache's name.
func (*Reporter) CacheShardings ¶ added in v0.4.9
CacheShardings returns the shardings of cache. You can use WithShardings to set cache's shardings. Zero shardings means cache is non-sharding.
func (*Reporter) CountMissed ¶ added in v0.4.6
CountMissed returns the missed count.
func (*Reporter) MissedRate ¶ added in v0.4.6
MissedRate returns the missed rate.