eleum

package module
v0.0.0-...-e2e7ae7 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2019 License: MIT Imports: 7 Imported by: 0

README

eleum

L1

eleum is an instance cache provider to avoid i/o operation. It was made to be simple and fast.

  • Safe for multiple goroutines
  • Simple
  • Fast

Install

go get -u github.com/kauehmoreno/eleum

Usage

Simple One
cache := eleum.New()
key := eleum.FormatKey("key", "param1", "param2")
var expected string
err := cache.Get(key, &expected)
if err != nil{
    result := fn()
    defer cache.Expire(key, time.Second*30)
    cache.Set(key, result)
}
With context
cache := eleum.New()
key := eleum.FormatKey("key", "param1", "param2")
ctx := context.Background()
err := cache.GetWithContext(ctx, key, &expected)
if err != nil{
    result := fn()
    cache.SetWithContext(ctx, key, result)
}
New options
  • MaxNumberOfKeys to be set on cache object
cache:= eleum.New(eleum.MaxNumOfKeys(10000))
  • WriteTimeout and ReadTimeout it's used with GetWithContext and SetWithContext
cache:= eleum.New(
    eleum.ReadTimeout(time.Milliseconds*10), eleum.WriteTimeout(time.Milliseconds*10))
TotalKeys()
  • Return total of keys on cache object
cache := eleum.New()
total:= cache.TotalKeys()
Flushall()
  • Return total of keys on cache object
 eleum.New().Flushall()
Del()
  • Delete a key from cache object
eleum.New().Del("key")
Expire()
  • Is rensposable for expire key/value pair from cache
  • it must be use together with background
  • without it will only mark a key to be expired but will not do it
eleum.New().Expire(key, time.Second*30)
Background()
  • A goroutine is started to operate background check to expire all keys based - on expiration set from each key
  • it runs a time ticker defined by client
  • Background should be used one time preferably.
eleum.New().Background(time.Minute)
Benchmark
Operation NumOp TimeOp BytesOp allocOp
BenchmarkTestInstanceAllocationsOnMultipleCalls/CacheInstance_1_-4 1000000 1063 ns/op 0 B/op 0 allocs/op
BenchmarkTestInstanceAllocationsOnMultipleCalls/CacheInstance_2_-4 1000000 1098 ns/op 0 B/op 0 allocs/op
BenchmarkTestInstanceAllocationsOnMultipleCalls/CacheInstance_4_-4 1000000 1084 ns/op 0 B/op 0 allocs/op
BenchmarkTestInstanceAllocationsOnMultipleCalls/CacheInstance_8_-4 1000000 1048 ns/op 0 B/op 0 allocs/op
BenchmarkTestInstanceAllocationsOnMultipleCalls/CacheInstance_16_-4 1000000 1032 ns/op 0 B/op 0 allocs/op
BenchmarkTestInstanceAllocationsOnMultipleCalls/CacheInstance_32_-4 1000000 1037 ns/op 0 B/op 0 allocs/op
BenchmarkTestInstanceAllocationsOnMultipleCalls/CacheInstance_64_-4 1000000 1040 ns/op 0 B/op 0 allocs/op
BenchmarkTestInstanceAllocationsOnMultipleCalls/CacheInstance_128_-4 1000000 1039 ns/op 0 B/op 0 allocs/op
BenchmarkTestInstanceAllocationsOnMultipleCalls/CacheInstance_256_-4 1000000 1051 ns/op 0 B/op 0 allocs/op
BenchmarkTestInstanceAllocationsOnMultipleCalls/CacheInstance_512_-4 1000000 1017 ns/op 0 B/op 0 allocs/op
BenchmarkTestInstanceAllocationsOnMultipleCalls/CacheInstance_1024_-4 1000000 1044 ns/op 0 B/op 0 allocs/op
BenchmarkTestInstanceAllocationsOnMultipleCalls/CacheInstance_2048_-4 1000000 1033 ns/op 0 B/op 0 allocs/op
BenchmarkTestInstanceAllocationOnParallelCalls-4 50000 23917 ns/op 0 B/op 0 allocs/op
BenchmarkTestGetExistingKeyUnmarshalStruct/Get_existing_key_1_-4 100000 18186 ns/op 336 B/op 11 allocs/op
BenchmarkTestGetExistingKeyUnmarshalStruct/Get_existing_key_2_-4 100000 18472 ns/op 336 B/op 11 allocs/op
BenchmarkTestGetExistingKeyUnmarshalStruct/Get_existing_key_4_-4 100000 18110 ns/op 336 B/op 11 allocs/op
BenchmarkTestGetExistingKeyUnmarshalStruct/Get_existing_key_8_-4 100000 17989 ns/op 336 B/op 11 allocs/op
BenchmarkTestGetExistingKeyUnmarshalStruct/Get_existing_key_16_-4 100000 19003 ns/op 336 B/op 11 allocs/op
BenchmarkTestGetExistingKeyUnmarshalStruct/Get_existing_key_32_-4 100000 19000 ns/op 336 B/op 11 allocs/op
BenchmarkTestGetExistingKeyUnmarshalStruct/Get_existing_key_64_-4 100000 19803 ns/op 336 B/op 11 allocs/op
BenchmarkTestGetExistingKeyUnmarshalStruct/Get_existing_key_128_-4 100000 18089 ns/op 336 B/op 11 allocs/op
BenchmarkTestGetExistingKeyUnmarshalStruct/Get_existing_key_256_-4 100000 18354 ns/op 336 B/op 11 allocs/op
BenchmarkTestGetExistingKeyUnmarshalStruct/Get_existing_key_512_-4 100000 18240 ns/op 336 B/op 11 allocs/op
BenchmarkTestGetExistingKeyUnmarshalStruct/Get_existing_key_1024_-4 100000 17926 ns/op 336 B/op 11 allocs/op
BenchmarkTestGetExistingKeyUnmarshalStruct/Get_existing_key_2048_-4 100000 18295 ns/op 336 B/op 11 allocs/op
BenchmarkTestGetExistingKeyUnmarshalStructInParallel-4 100000 23617 ns/op 960 B/op 36 allocs/op
BenchmarkGetKeyThatDoesNotExist/Get_not_existing_key_1_-4 2000000 827 ns/op 48 B/op 3 allocs/op
BenchmarkGetKeyThatDoesNotExist/Get_not_existing_key_2_-4 2000000 856 ns/op 48 B/op 3 allocs/op
BenchmarkGetKeyThatDoesNotExist/Get_not_existing_key_4_-4 2000000 815 ns/op 48 B/op 3 allocs/op
BenchmarkGetKeyThatDoesNotExist/Get_not_existing_key_8_-4 2000000 799 ns/op 48 B/op 3 allocs/op
BenchmarkGetKeyThatDoesNotExist/Get_not_existing_key_16_-4 2000000 888 ns/op 48 B/op 3 allocs/op
BenchmarkGetKeyThatDoesNotExist/Get_not_existing_key_32_-4 2000000 773 ns/op 48 B/op 3 allocs/op
BenchmarkGetKeyThatDoesNotExist/Get_not_existing_key_64_-4 2000000 833 ns/op 48 B/op 3 allocs/op
BenchmarkGetKeyThatDoesNotExist/Get_not_existing_key_128_-4 2000000 768 ns/op 48 B/op 3 allocs/op
BenchmarkGetKeyThatDoesNotExist/Get_not_existing_key_256_-4 2000000 819 ns/op 48 B/op 3 allocs/op
BenchmarkGetKeyThatDoesNotExist/Get_not_existing_key_512_-4 2000000 872 ns/op 48 B/op 3 allocs/op
BenchmarkGetKeyThatDoesNotExist/Get_not_existing_key_1024_-4 2000000 895 ns/op 48 B/op 3 allocs/op
BenchmarkGetKeyThatDoesNotExist/Get_not_existing_key_2048_-4 2000000 810 ns/op 48 B/op 3 allocs/op
BenchmarkGetKeyThatDoesNotExistInParallel-4 100000 23867 ns/op 576 B/op 36 allocs/op
BenchmarkSetKeyIntoCache/Set_key_1_-4 300000 4128 ns/op 255 B/op 7 allocs/op
BenchmarkSetKeyIntoCache/Set_key_2_-4 300000 3972 ns/op 255 B/op 7 allocs/op
BenchmarkSetKeyIntoCache/Set_key_4_-4 500000 4224 ns/op 255 B/op 7 allocs/op
BenchmarkSetKeyIntoCache/Set_key_8_-4 300000 4211 ns/op 255 B/op 7 allocs/op
BenchmarkSetKeyIntoCache/Set_key_16_-4 500000 3835 ns/op 255 B/op 7 allocs/op
BenchmarkSetKeyIntoCache/Set_key_32_-4 500000 3900 ns/op 255 B/op 7 allocs/op
BenchmarkSetKeyIntoCache/Set_key_64_-4 500000 3967 ns/op 255 B/op 7 allocs/op
BenchmarkSetKeyIntoCache/Set_key_128_-4 500000 4085 ns/op 255 B/op 7 allocs/op
BenchmarkSetKeyIntoCache/Set_key_256_-4 300000 3916 ns/op 255 B/op 7 allocs/op
BenchmarkSetKeyIntoCache/Set_key_512_-4 300000 3811 ns/op 255 B/op 7 allocs/op
BenchmarkSetKeyIntoCache/Set_key_1024_-4 500000 3927 ns/op 255 B/op 7 allocs/op
BenchmarkSetKeyIntoCache/Set_key_2048_-4 500000 3887 ns/op 255 B/op 7 allocs/op
BenchmarkSetKeytInParallel-4 50000 38589 ns/op 2856 B/op 79 allocs/op
BenchmarkSetWithContextKeyIntoCache/Set_key_with_context_1_-4 300000 3828 ns/op 255 B/op 7 allocs/op
BenchmarkSetWithContextKeyIntoCache/Set_key_with_context_2_-4 300000 3790 ns/op 255 B/op 7 allocs/op
BenchmarkSetWithContextKeyIntoCache/Set_key_with_context_4_-4 500000 3802 ns/op 255 B/op 7 allocs/op
BenchmarkSetWithContextKeyIntoCache/Set_key_with_context_8_-4 500000 3867 ns/op 255 B/op 7 allocs/op
BenchmarkSetWithContextKeyIntoCache/Set_key_with_context_16_-4 500000 3848 ns/op 255 B/op 7 allocs/op
BenchmarkSetWithContextKeyIntoCache/Set_key_with_context_32_-4 500000 3833 ns/op 255 B/op 7 allocs/op
BenchmarkSetWithContextKeyIntoCache/Set_key_with_context_64_-4 500000 3829 ns/op 255 B/op 7 allocs/op
BenchmarkSetWithContextKeyIntoCache/Set_key_with_context_128_-4 500000 3860 ns/op 255 B/op 7 allocs/op
BenchmarkSetWithContextKeyIntoCache/Set_key_with_context_256_-4 500000 3826 ns/op 255 B/op 7 allocs/op
BenchmarkSetWithContextKeyIntoCache/Set_key_with_context_512_-4 300000 3872 ns/op 255 B/op 7 allocs/op
BenchmarkSetWithContextKeyIntoCache/Set_key_with_context_1024_-4 500000 3797 ns/op 255 B/op 7 allocs/op
BenchmarkSetWithContextKeyIntoCache/Set_key_with_context_2048_-4 500000 3836 ns/op 255 B/op 7 allocs/op
BenchmarkSetWithContextKeytInParallel-4 50000 37627 ns/op 2856 B/op 79 allocs/op

All benchmark tests were made on IMAC Intel(R) Core(TM) i5-7400 CPU @ 3.00GHz

Third Party Libraries
Name Author Description
Msgpack Vladimir Mihailenco MessagePack encoding for Golang

License

MIT Free Software, Hell Yeah!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatKey

func FormatKey(key string, params ...string) string

FormatKey is an helper to build keyValue

Types

type Eleum

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

Eleum is a type representation similar to the L1 cache type

func New

func New(opts ...Options) *Eleum

New returns a singleton instance of cache's concret type it's not mandatory control concrete type instance to avoid memory realocation it is full optimize already

func (*Eleum) Background

func (c *Eleum) Background(t time.Duration)

Background should be used one time preferably. A goroutine is started to combine usage with expire method

func (*Eleum) Del

func (c *Eleum) Del(key string)

Del allow erase a key explicity

func (*Eleum) Expire

func (c *Eleum) Expire(key string, t time.Duration) error

Expire will expire a key based on time duration it must be combine with BackgroundCheck method use expire only set a key to be expired but does not execute in fact this operations is made by BackgroundCheck method

func (*Eleum) Flushall

func (c *Eleum) Flushall()

Flushall erase all keys at once

func (*Eleum) Get

func (c *Eleum) Get(key string, value interface{}) error

Get return a value type converted inplace to expected one an error can be throw if there is no value for the expected key or if converting result into byte returns error...

func (*Eleum) GetWithContext

func (c *Eleum) GetWithContext(parentCtx context.Context, key string, value interface{}) error

GetWithContext use context timeout to avoid operation to take longer than expected

func (*Eleum) Set

func (c *Eleum) Set(key string, value interface{}) error

Set store value into cache an error may happens if converting storing value to byte fails if object get to big and exceed the maximum size determined it will not set more value into it until size get lower

func (*Eleum) SetWithContext

func (c *Eleum) SetWithContext(parentCtx context.Context, key string, value interface{}) error

SetWithContext use context timeout to avoid operation to take longer than expected

func (*Eleum) TotalKeys

func (c *Eleum) TotalKeys() uint64

TotalKeys returns total of keys set on cache it is safe to call with multiple goroutines TotalKey may not represent current value of numKeys once it loads a copy of the current value is returned other goroutine might change while this is happening... it's probably hold more values than expected

type Options

type Options func(eleum *Eleum)

Options is a set of config to be set on cache

func MaxNumOfKeys

func MaxNumOfKeys(size uint64) Options

MaxNumOfKeys determine max number of keys to be set

func ReadTimeout

func ReadTimeout(t time.Duration) Options

ReadTimeout config operation reading time otherwise it will be canceled by context timeout

func WriteTimeout

func WriteTimeout(t time.Duration) Options

WriteTimeout config operation writing time otherwise it will be canceled by context timeout

Jump to

Keyboard shortcuts

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