Documentation ¶
Index ¶
- type Cache
- func (c *Cache) Clean(overdue time.Time)
- func (c *Cache) Del(key interface{})
- func (c *Cache) Get(key interface{}) (interface{}, bool)
- func (c *Cache) GetString(key interface{}) (string, bool)
- func (c *Cache) Keys() []interface{}
- func (c *Cache) Overdue(overdue time.Time) []interface{}
- func (c *Cache) Reset(key interface{})
- func (c *Cache) Set(key, value interface{})
- func (c *Cache) SetByDuration(key, value interface{}, expire time.Duration)
- func (c *Cache) SetByTime(key, value interface{}, expire time.Time)
- func (c *Cache) Size() int
- type ChMap
- func (p ChMap) Close()
- func (p ChMap) Del(key interface{})
- func (p ChMap) Get(key interface{}) (interface{}, bool)
- func (p ChMap) Has(key interface{}) bool
- func (p ChMap) Iterator(callBack func(k, v interface{}))
- func (p ChMap) Keys() []interface{}
- func (p ChMap) Set(key, value interface{})
- func (p ChMap) Size() int
- type NumHash
- type SyncMap
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct { Expire time.Duration LRU bool Callback func(key, value interface{}) // contains filtered or unexported fields }
Cache support LRU (Least Recently Used).
Example ¶
cache := NewCache(3*time.Second, true) cache.Set("key1", "value1") cache.SetByDuration("key2", "value2", time.Second) cache.Set("key3", "value3") fmt.Println(cache.Get("key1")) fmt.Println("init size:", cache.Size()) time.Sleep(2 * time.Second) fmt.Println(cache.Get("key3")) // reset expire time. fmt.Println("2 second:", cache.Size()) time.Sleep(2 * time.Second) fmt.Println("4 second:", cache.Size())
Output: value1 true init size: 3 value3 true 2 second: 2 4 second: 1
func NewCallbackCache ¶ added in v0.0.4
func NewCallbackCache(expire time.Duration, callback func(key, value interface{}), LRU ...bool) *Cache
NewCallbackCache new have del callback cache.
Example ¶
cache := NewCallbackCache(3*time.Second, func(key, value interface{}) { fmt.Println("del:", key, value) }, true) cache.Set("key1", "value1") cache.SetByDuration("key2", "value2", time.Second) cache.Set("key3", "value3") fmt.Println(cache.Get("key1")) fmt.Println("init size:", cache.Size()) time.Sleep(2 * time.Second) fmt.Println(cache.Get("key3")) // reset expire time. fmt.Println("2 second:", cache.Size()) time.Sleep(2 * time.Second) fmt.Println("4 second:", cache.Size())
Output: value1 true init size: 3 del: key2 value2 value3 true 2 second: 2 del: key1 value1 4 second: 1
func (*Cache) SetByDuration ¶
SetByDuration value by key.
type ChMap ¶
type ChMap struct {
// contains filtered or unexported fields
}
ChMap is channel map.
Example ¶
chMap := NewChMap() defer chMap.Close() chMap.Set("key1", "value1") chMap.Set("key2", "value2") fmt.Println(chMap.Get("key1")) fmt.Println(chMap.Size())
Output: value1 true 2
type NumHash ¶ added in v0.0.4
NumHash is hash to num.
func NewNumHash ¶ added in v0.0.4
NewNumHash by max num.
Example ¶
h := NewNumHash(10) for i := 0; i < 10; i++ { h.Reset() h.Write([]byte(fmt.Sprintf("value:%d", i))) fmt.Println(h.Sum1(), h.Sum2(), h.Sum3(), h.Sum4()) }
Output: 3 2 0 3 4 5 4 7 5 2 7 7 6 5 1 1 7 2 4 7 8 5 8 1 3 8 6 9 4 5 9 5 5 8 3 9 6 5 6 9
Click to show internal directories.
Click to hide internal directories.