toolkit

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2019 License: MIT Imports: 4 Imported by: 1

README

toolkit

golang toolkit.

Usage

Cache
package main

import (
  "fmt"
  "time"

  "github.com/xuender/toolkit"
)

func main() {
  // LRU
  cache := toolkit.NewCache(time.Second*3, true)
  cache.Set("key1", "value1")
  cache.SetByDuration("key2", "value2", time.Second)
  cache.Set("key3", "value3")

  fmt.Println("init size:", cache.Size())
  time.Sleep(time.Second * 2)
  cache.Get("key3") // reset expire time.
  fmt.Println("2 Second:", cache.Size())
  time.Sleep(time.Second * 2)
  fmt.Println("4 Second:", cache.Size())
}
SyncMap
package main

import (
  "fmt"

  "github.com/xuender/toolkit"
)

func main() {
  m := toolkit.NewSyncMap()
  m.Set("key1", "value1")
  m.Set("key2", "value2")

  fmt.Println(m.Get("key1"))
  fmt.Println(m.Size())
}
ChMap
package main

import (
  "fmt"

  "github.com/xuender/toolkit"
)

func main() {
  m:= toolkit.NewChMap()
  defer m.Close()
  m.Set("key1", "value1")
  m.Set("key2", "value2")

  fmt.Println(m.Get("key1"))
  fmt.Println(m.Size())
}

Documentation

Index

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 NewCache

func NewCache(expire time.Duration, LRU ...bool) *Cache

NewCache new cache.

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) Clean

func (c *Cache) Clean(overdue time.Time)

Clean overdue.

func (*Cache) Del

func (c *Cache) Del(key interface{})

Del key.

func (*Cache) Get

func (c *Cache) Get(key interface{}) (interface{}, bool)

Get value by key.

func (*Cache) GetString added in v0.0.4

func (c *Cache) GetString(key interface{}) (string, bool)

GetString by key.

func (*Cache) Keys

func (c *Cache) Keys() []interface{}

Keys by map.

func (*Cache) Overdue

func (c *Cache) Overdue(overdue time.Time) []interface{}

Overdue keys.

func (*Cache) Reset

func (c *Cache) Reset(key interface{})

Reset expire by key.

func (*Cache) Set

func (c *Cache) Set(key, value interface{})

Set value by key.

func (*Cache) SetByDuration

func (c *Cache) SetByDuration(key, value interface{}, expire time.Duration)

SetByDuration value by key.

func (*Cache) SetByTime

func (c *Cache) SetByTime(key, value interface{}, expire time.Time)

SetByTime value by key.

func (*Cache) Size

func (c *Cache) Size() int

Size by Cache.

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

func NewChMap

func NewChMap() *ChMap

NewChMap new ChMap.

func (ChMap) Close

func (p ChMap) Close()

Close this ChMap.

func (ChMap) Del added in v0.0.3

func (p ChMap) Del(key interface{})

Del obj by key.

func (ChMap) Get

func (p ChMap) Get(key interface{}) (interface{}, bool)

Get obj by key.

func (ChMap) Has

func (p ChMap) Has(key interface{}) bool

Has key.

func (ChMap) Iterator

func (p ChMap) Iterator(callBack func(k, v interface{}))

Iterator map.

func (ChMap) Keys

func (p ChMap) Keys() []interface{}

Keys is get this map keys.

func (ChMap) Set

func (p ChMap) Set(key, value interface{})

Set value by key.

func (ChMap) Size

func (p ChMap) Size() int

Size ChMap.

type NumHash added in v0.0.4

type NumHash struct {
	Max  int
	Hash hash.Hash32
}

NumHash is hash to num.

func NewNumHash added in v0.0.4

func NewNumHash(max int) *NumHash

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

func (*NumHash) Reset added in v0.0.4

func (h *NumHash) Reset()

Reset resets the Hash to its initial state.

func (*NumHash) Sum1 added in v0.0.4

func (h *NumHash) Sum1() int

Sum1 bytes hash to int by max.

func (*NumHash) Sum2 added in v0.0.4

func (h *NumHash) Sum2() int

Sum2 bytes hash to int by max.

func (*NumHash) Sum3 added in v0.0.4

func (h *NumHash) Sum3() int

Sum3 bytes hash to int by max.

func (*NumHash) Sum4 added in v0.0.4

func (h *NumHash) Sum4() int

Sum4 bytes hash to int by max.

func (*NumHash) Write added in v0.0.4

func (h *NumHash) Write(data []byte) (int, error)

Writer is the interface that wraps the basic Write method.

type SyncMap

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

SyncMap sync map

Example
m := NewSyncMap()
m.Set("key1", "value1")
m.Set("key2", "value2")

fmt.Println(m.Get("key1"))
fmt.Println(m.Size())
Output:

value1 true
2

func NewSyncMap

func NewSyncMap() *SyncMap

NewSyncMap new SyncMap.

func (*SyncMap) Del

func (p *SyncMap) Del(key interface{})

Del obj by key.

func (*SyncMap) Get

func (p *SyncMap) Get(key interface{}) (interface{}, bool)

Get obj by key.

func (*SyncMap) Has

func (p *SyncMap) Has(key interface{}) bool

Has key.

func (*SyncMap) Keys

func (p *SyncMap) Keys() []interface{}

Keys is get this map keys.

func (*SyncMap) Set

func (p *SyncMap) Set(key, value interface{})

Set value by key.

func (*SyncMap) Size

func (p *SyncMap) Size() int

Size by map.

Jump to

Keyboard shortcuts

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