cache

package module
v0.0.0-...-63369d7 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2021 License: MIT Imports: 15 Imported by: 2

README

Before tests

sudo apt-get install libcanberra-gtk-module
sudo apt-get install libcanberra-gtk-module libcanberra-gtk3-module
sudo apt-get install graphviz

Tests

About tools: https://blog.golang.org/pprof

go test -bench=. -benchmem -benchtime=10s -cpuprofile=cpu.out -memprofile=mem.out
go tool pprof ./mem.out
go tool pprof ./cpu.out
top10
web mallocgc

Tests results

Intel® Core™ i5 CPU 760 @ 2.80GHz × 4 (https://www.cpubenchmark.net/cpu.php?id=782)

JSON vs GOB

BenchmarkJSON-4   	  770727	     15493 ns/op
BenchmarkGOB-4   	  145520	     84069 ns/op

Table of compare performance ns/op

JSON GOB
Benchmark/4-4 15 493 84 069

Tests Storages

go test -bench=. -benchmem -benchtime=10s -cpuprofile=cpu.out -memprofile=mem.out

BenchmarkMap-4               373377 	     2888 ns/op

BenchmarkMutexMap/1-4  	 3241263	      3660 ns/op	     552 B/op	       6 allocs/op
BenchmarkMutexMap/2-4  	 3252266	      3741 ns/op	     552 B/op	       6 allocs/op
BenchmarkMutexMap/4-4  	 3148250	      3801 ns/op	     552 B/op	       6 allocs/op
BenchmarkMutexMap/8-4  	 3098610	      3909 ns/op	     552 B/op	       6 allocs/op

BenchmarkSyncMap/1-4   	 2677140	      4553 ns/op	     616 B/op	       9 allocs/op
BenchmarkSyncMap/2-4   	 2502535	      4958 ns/op	     616 B/op	       9 allocs/op
BenchmarkSyncMap/4-4   	 2506807	      4824 ns/op	     616 B/op	       9 allocs/op
BenchmarkSyncMap/8-4   	 2471282	      5186 ns/op	     616 B/op	       9 allocs/op

BenchmarkAerospike/1-4 	  100814	    116237 ns/op	    3973 B/op	     111 allocs/op
BenchmarkAerospike/2-4 	  131563	     94596 ns/op	    3972 B/op	     111 allocs/op
BenchmarkAerospike/4-4 	  137200	     87570 ns/op	    3971 B/op	     111 allocs/op
BenchmarkAerospike/8-4 	  138630	     86481 ns/op	    3971 B/op	     111 allocs/op

BenchmarkRedis/1-4     	  105042	    122826 ns/op	    2236 B/op	      33 allocs/op
BenchmarkRedis/2-4     	  111357	    104896 ns/op	    2233 B/op	      33 allocs/op
BenchmarkRedis/4-4     	  131476	     94707 ns/op	    2233 B/op	      33 allocs/op
BenchmarkRedis/8-4     	  131264	     88499 ns/op	    2233 B/op	      33 allocs/op

BenchmarkPostgreSQL/1-4      493	   2210216 ns/op
BenchmarkPostgreSQL/2-4 	 494	   2259960 ns/op
BenchmarkPostgreSQL/4-4 	 458	   2378314 ns/op
BenchmarkPostgreSQL/8-4 	 441	   2533566 ns/op

Table of compare performance ns/op

Only map map with sync.RWMutex sync.Map Aerospike Redis PostgreSQL
Benchmark/1-4 2 888 3 660 4 553 116 237 122 826 2 210 216
Benchmark/2-4 3 741 4 958 94 596 104 896 2 259 960
Benchmark/4-4 3 801 4 824 87 570 94 707 2 378 314
Benchmark/8-4 3 909 5 186 86 481 88 499 2 533 566

Documentation

Index

Constants

View Source
const (
	// For use with functions that take an expiration time.
	NoExpiration time.Duration = -1
	// For use with functions that take an expiration time. Equivalent to
	// passing in the same expiration duration as was given to New() or
	// NewFrom() when the cache was created (e.g. 5 minutes.)
	DefaultExpiration time.Duration = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

func (*Cache) Check

func (c *Cache) Check(k string) bool

func (*Cache) Clear

func (c *Cache) Clear()

func (*Cache) Close

func (c *Cache) Close()

func (*Cache) Count

func (c *Cache) Count() int64

func (*Cache) Get

func (c *Cache) Get(k string, obj interface{}) (interface{}, bool)

func (*Cache) GetMode

func (c *Cache) GetMode() string

func (*Cache) HasError

func (c *Cache) HasError() bool

func (*Cache) Remove

func (c *Cache) Remove(k string)

func (*Cache) Set

func (c *Cache) Set(k string, obj interface{})

type CacheAerospike

type CacheAerospike struct {
	Cache
	MaxConnections int
	URL            string
	Namespace      string
	// contains filtered or unexported fields
}

func (*CacheAerospike) Check

func (c *CacheAerospike) Check(key string) bool

func (*CacheAerospike) Clear

func (c *CacheAerospike) Clear()

func (*CacheAerospike) Close

func (c *CacheAerospike) Close()

func (*CacheAerospike) Count

func (c *CacheAerospike) Count() int64

func (*CacheAerospike) Get

func (c *CacheAerospike) Get(key string, obj interface{}) (interface{}, bool)

func (*CacheAerospike) GetAll2JSON

func (c *CacheAerospike) GetAll2JSON(x interface{}) []byte

func (*CacheAerospike) GetMode

func (c *CacheAerospike) GetMode() string

func (*CacheAerospike) HasError

func (c *CacheAerospike) HasError() bool

func (*CacheAerospike) Remove

func (c *CacheAerospike) Remove(key string)

func (*CacheAerospike) Set

func (c *CacheAerospike) Set(key string, x interface{})

type CacheConfig

type CacheConfig struct {
	Mode           string `yaml:"mode"`
	ExpiryTime     int64  `yaml:"expiry_time"`
	Url            string `yaml:"url"`
	MaxConnections int    `yaml:"max_connections"`
}

type CacheMap

type CacheMap struct {
	Cache
	// contains filtered or unexported fields
}

func (*CacheMap) Check

func (c *CacheMap) Check(k string) bool

func (*CacheMap) Clear

func (c *CacheMap) Clear()

func (*CacheMap) ClearOld

func (c *CacheMap) ClearOld()

func (*CacheMap) Count

func (c *CacheMap) Count() int64

func (*CacheMap) Get

func (c *CacheMap) Get(k string, obj interface{}) (interface{}, bool)

func (*CacheMap) GetAll2JSON

func (c *CacheMap) GetAll2JSON(x interface{}) []byte

func (*CacheMap) GetMode

func (c *CacheMap) GetMode() string

func (*CacheMap) HasError

func (c *CacheMap) HasError() bool

func (*CacheMap) Remove

func (c *CacheMap) Remove(k string)

func (*CacheMap) Set

func (c *CacheMap) Set(k string, x interface{})

type CacheMutexMap

type CacheMutexMap struct {
	Cache
	// contains filtered or unexported fields
}

func (*CacheMutexMap) Check

func (c *CacheMutexMap) Check(k string) bool

func (*CacheMutexMap) Clear

func (c *CacheMutexMap) Clear()

func (*CacheMutexMap) ClearOld

func (c *CacheMutexMap) ClearOld()

func (*CacheMutexMap) Count

func (c *CacheMutexMap) Count() int64

func (*CacheMutexMap) Get

func (c *CacheMutexMap) Get(k string, obj interface{}) (interface{}, bool)

func (*CacheMutexMap) GetAll2JSON

func (c *CacheMutexMap) GetAll2JSON(x interface{}) []byte

func (*CacheMutexMap) GetMode

func (c *CacheMutexMap) GetMode() string

func (*CacheMutexMap) HasError

func (c *CacheMutexMap) HasError() bool

func (*CacheMutexMap) Remove

func (c *CacheMutexMap) Remove(k string)

func (*CacheMutexMap) Set

func (c *CacheMutexMap) Set(k string, x interface{})

type CachePostgreSQL

type CachePostgreSQL struct {
	Cache
	MaxConnections int
	URL            string
	DBName         string
	TableName      string
	// contains filtered or unexported fields
}

func (*CachePostgreSQL) Check

func (c *CachePostgreSQL) Check(key string) bool

func (*CachePostgreSQL) Clear

func (c *CachePostgreSQL) Clear()

func (*CachePostgreSQL) Close

func (c *CachePostgreSQL) Close()

func (*CachePostgreSQL) Count

func (c *CachePostgreSQL) Count() int64

func (*CachePostgreSQL) Get

func (c *CachePostgreSQL) Get(key string, obj interface{}) (interface{}, bool)

func (*CachePostgreSQL) GetAll2JSON

func (c *CachePostgreSQL) GetAll2JSON(x interface{}) []byte

func (*CachePostgreSQL) GetMode

func (c *CachePostgreSQL) GetMode() string

func (*CachePostgreSQL) HasError

func (c *CachePostgreSQL) HasError() bool

func (*CachePostgreSQL) Remove

func (c *CachePostgreSQL) Remove(key string)

func (*CachePostgreSQL) Set

func (c *CachePostgreSQL) Set(key string, x interface{})

type CacheRedis

type CacheRedis struct {
	Cache
	MaxConnections int
	URL            string
	// contains filtered or unexported fields
}

func (*CacheRedis) Check

func (c *CacheRedis) Check(key string) bool

func (*CacheRedis) Clear

func (c *CacheRedis) Clear()

func (*CacheRedis) Close

func (c *CacheRedis) Close()

func (*CacheRedis) Count

func (c *CacheRedis) Count() int64

func (*CacheRedis) Get

func (c *CacheRedis) Get(key string, obj interface{}) (interface{}, bool)

func (*CacheRedis) GetAll2JSON

func (c *CacheRedis) GetAll2JSON(x interface{}) []byte

func (*CacheRedis) GetMode

func (c *CacheRedis) GetMode() string

func (*CacheRedis) GetStr

func (c *CacheRedis) GetStr(key string) (string, bool)

func (*CacheRedis) HasError

func (c *CacheRedis) HasError() bool

func (*CacheRedis) Remove

func (c *CacheRedis) Remove(key string)

func (*CacheRedis) Set

func (c *CacheRedis) Set(key string, x interface{})

func (*CacheRedis) SetStr

func (c *CacheRedis) SetStr(key string, x string)

type CacheSyncMap

type CacheSyncMap struct {
	Cache
	// contains filtered or unexported fields
}

func (*CacheSyncMap) Check

func (c *CacheSyncMap) Check(k string) bool

func (*CacheSyncMap) Clear

func (c *CacheSyncMap) Clear()

func (*CacheSyncMap) ClearOld

func (c *CacheSyncMap) ClearOld()

func (*CacheSyncMap) Count

func (c *CacheSyncMap) Count() int64

func (*CacheSyncMap) Get

func (c *CacheSyncMap) Get(k string, obj interface{}) (interface{}, bool)

func (*CacheSyncMap) GetAll2JSON

func (c *CacheSyncMap) GetAll2JSON(x interface{}) []byte

func (*CacheSyncMap) GetMode

func (c *CacheSyncMap) GetMode() string

func (*CacheSyncMap) HasError

func (c *CacheSyncMap) HasError() bool

func (*CacheSyncMap) Remove

func (c *CacheSyncMap) Remove(k string)

func (*CacheSyncMap) Set

func (c *CacheSyncMap) Set(k string, x interface{})

type ICache

type ICache interface {
	HasError() bool
	GetMode() string

	Set(k string, obj interface{})
	Get(k string, obj interface{}) (interface{}, bool)
	Check(k string) bool
	Remove(k string)

	Clear()
	Count() int64

	Close()
}

func New

func New(mode string, expiryTime int64, url string, maxConnections int) ICache

func NewConfig

func NewConfig(cfg *CacheConfig) ICache

Init

type Item

type Item struct {
	Object     interface{}
	Expiration int64
}

type ItemPostgreSQL

type ItemPostgreSQL struct {
	Key       string    `db:"id"                         json:"id"                                     gorm:"column:id;primary_key;"`
	UpdatedAt time.Time `db:"updated_at;default: null"   json:"updated_at"    sql:"default: now()"     gorm:"type:timestamp with time zone"`
	Object    []byte    `db:"object"                     json:"object,ommitempty"                      `
}

type ItemStr

type ItemStr struct {
	Object     string
	Expiration int64
}

Jump to

Keyboard shortcuts

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