tmcache

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package tmcache provide thread-safe cache that managed by timer

Example:

cache := NewCache()
cache.Put("key3", "value3", 3*time.Second)
cache.Put("key1", "value1", 1*time.Second)
cache.Put("key2", "value2", 2*time.Second)

if value, ok := cache.Get("key1"); ok == true {
  log.Print(value) // key1
}
if key, value, ok := cache.Top(); ok == true {
  log.Print(key, value) // key1, value1
}
if key, value, ok := cache.Pop(); ok == true {
  log.Print(key, value) // key1, value1
}
if key, value, ok := cache.Pop(); ok == true {
  log.Print(key, value) // key2, value2
}

<-time.After(10*Second)
if key, value, ok := cache.Pop(); ok == true {
  log.Print(key, value) // key3 has expired, no output here
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Len return length of cache
	Len() int
	// Put item into Cache
	Put(key interface{}, value interface{}, duration time.Duration)
	// Set item into Cache
	Set(key interface{}, value interface{}, duration time.Duration)
	// Get item by key
	Get(key interface{}) (value interface{}, ok bool)
	// Pop item that most close to expire
	Pop() (key interface{}, value interface{}, ok bool)
	// Top item that most close to expire
	Top() (key interface{}, value interface{}, ok bool)
	// Range cache like sync.Map by random order
	Range(fn func(key interface{}, value interface{}) bool)
}

Cache manage key/value cache by timer and auto clean up expire data with GC

func NewCache

func NewCache() Cache

NewCache return a Cache

func NewCacheExpectCap

func NewCacheExpectCap(expect int) Cache

NewCacheExpectCap return a Cache with expect capacity for optimize performance

Jump to

Keyboard shortcuts

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