Documentation
¶
Index ¶
- type Cache
- type Interface
- type Item
- type ItemOption
- type Number
- type NumberCache
- type Option
- func AsClock[K comparable, V any](opts ...clock.Option) Option[K, V]
- func AsFIFO[K comparable, V any](opts ...fifo.Option) Option[K, V]
- func AsLFU[K comparable, V any](opts ...lfu.Option) Option[K, V]
- func AsLRU[K comparable, V any](opts ...lru.Option) Option[K, V]
- func AsMRU[K comparable, V any](opts ...mru.Option) Option[K, V]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
Cache is a thread safe cache.
Example ¶
package main
import (
"fmt"
cache "github.com/Code-Hex/go-generics-cache"
)
func main() {
// use simple cache algorithm without options.
c := cache.New[string, int]()
c.Set("a", 1)
gota, aok := c.Get("a")
gotb, bok := c.Get("b")
fmt.Println(gota, aok)
fmt.Println(gotb, bok)
}
Output: 1 true 0 false
func New ¶
func New[K comparable, V any](opts ...Option[K, V]) *Cache[K, V]
New creates a new thread safe Cache.
There are several Cache replacement policies available with you specified any options.
func (*Cache[K, V]) Contains ¶
Contains reports whether key is within cache.
Example ¶
package main
import (
"fmt"
cache "github.com/Code-Hex/go-generics-cache"
)
func main() {
c := cache.New(cache.AsLRU[string, int]())
c.Set("a", 1)
fmt.Println(c.Contains("a"))
fmt.Println(c.Contains("b"))
}
Output: true false
func (*Cache[K, V]) Delete ¶
func (c *Cache[K, V]) Delete(key K)
Delete deletes the item with provided key from the cache.
Example ¶
package main
import (
"fmt"
cache "github.com/Code-Hex/go-generics-cache"
)
func main() {
c := cache.New(cache.AsMRU[string, int]())
c.Set("a", 1)
c.Delete("a")
gota, aok := c.Get("a")
fmt.Println(gota, aok)
}
Output: 0 false
func (*Cache[K, V]) Keys ¶
func (c *Cache[K, V]) Keys() []K
Keys returns the keys of the cache. the order is relied on algorithms.
Example ¶
package main
import (
"fmt"
cache "github.com/Code-Hex/go-generics-cache"
)
func main() {
c := cache.New(cache.AsLFU[string, int]())
c.Set("a", 1)
c.Set("b", 1)
c.Set("c", 1)
fmt.Println(c.Keys())
}
Output: [a b c]
func (*Cache[K, V]) Set ¶
func (c *Cache[K, V]) Set(key K, val V, opts ...ItemOption)
Set sets a value to the cache with key. replacing any existing value.
type Interface ¶
type Interface[K comparable, V any] interface { Get(key K) (value V, ok bool) Set(key K, val V) Keys() []K Delete(key K) }
Interface is a common-cache interface.
type Item ¶
type Item[K comparable, V any] struct { Key K Value V Expiration time.Duration }
Item is an item
type ItemOption ¶
type ItemOption func(*itemOptions)
ItemOption is an option for cache item.
func WithExpiration ¶
func WithExpiration(exp time.Duration) ItemOption
WithExpiration is an option to set expiration time for any items. If the expiration is zero or negative value, it treats as w/o expiration.
Example ¶
package main
import (
"fmt"
"time"
cache "github.com/Code-Hex/go-generics-cache"
)
func main() {
c := cache.New(cache.AsFIFO[string, int]())
exp := 250 * time.Millisecond
c.Set("a", 1, cache.WithExpiration(exp))
// check item is set.
gota, aok := c.Get("a")
fmt.Println(gota, aok)
// set again
c.Set("a", 2, cache.WithExpiration(exp))
gota2, aok2 := c.Get("a")
fmt.Println(gota2, aok2)
// waiting expiration.
time.Sleep(exp + 100*time.Millisecond) // + buffer
gota3, aok3 := c.Get("a") // expired
fmt.Println(gota3, aok3)
}
Output: 1 true 2 true 0 false
type Number ¶
type Number interface {
constraints.Integer | constraints.Float | constraints.Complex
}
Number is a constraint that permits any numeric types.
type NumberCache ¶
type NumberCache[K comparable, V Number] struct { *Cache[K, V] // contains filtered or unexported fields }
NumberCache is a in-memory cache which is able to store only Number constraint.
func NewNumber ¶
func NewNumber[K comparable, V Number](opts ...Option[K, V]) *NumberCache[K, V]
NewNumber creates a new cache for Number constraint.
Example ¶
package main
import (
"fmt"
"time"
cache "github.com/Code-Hex/go-generics-cache"
)
func main() {
nc := cache.NewNumber[string, int]()
nc.Set("a", 1)
nc.Set("b", 2, cache.WithExpiration(time.Minute))
av := nc.Increment("a", 1)
gota, aok := nc.Get("a")
bv := nc.Decrement("b", 1)
gotb, bok := nc.Get("b")
// not set keys
cv := nc.Increment("c", 100)
dv := nc.Decrement("d", 100)
fmt.Println(av, gota, aok)
fmt.Println(bv, gotb, bok)
fmt.Println(cv)
fmt.Println(dv)
}
Output: 2 2 true 1 1 true 100 -100
func (*NumberCache[K, V]) Decrement ¶
func (nc *NumberCache[K, V]) Decrement(key K, n V) V
Decrement an item of type Number constraint by n. Returns the decremented value.
func (*NumberCache[K, V]) Increment ¶
func (nc *NumberCache[K, V]) Increment(key K, n V) V
Increment an item of type Number constraint by n. Returns the incremented value.
type Option ¶
type Option[K comparable, V any] func(*options[K, V])
Option is an option for cache.
func AsClock ¶
func AsClock[K comparable, V any](opts ...clock.Option) Option[K, V]
AsClock is an option to make a new Cache as clock algorithm.
Example ¶
package main
import (
"fmt"
cache "github.com/Code-Hex/go-generics-cache"
)
func main() {
// use clock cache algorithm.
c := cache.New(cache.AsClock[string, int]())
c.Set("a", 1)
gota, aok := c.Get("a")
gotb, bok := c.Get("b")
fmt.Println(gota, aok)
fmt.Println(gotb, bok)
}
Output: 1 true 0 false
func AsFIFO ¶
func AsFIFO[K comparable, V any](opts ...fifo.Option) Option[K, V]
AsFIFO is an option to make a new Cache as FIFO algorithm.
func AsLFU ¶
func AsLFU[K comparable, V any](opts ...lfu.Option) Option[K, V]
AsLFU is an option to make a new Cache as LFU algorithm.