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
Source Files
¶
- heap.go
- tmcache.go
Click to show internal directories.
Click to hide internal directories.