concache

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2021 License: MIT Imports: 4 Imported by: 0

README

concache

MIT License GoDoc Go Report Card Releases

concache is in-memory key:value cache. concache provides thread-safe map[string]interface{} with expiration times. concache a high-performance solution to this by sharding the map with minimal time spent waiting for locks.

Documentation

https://godoc.org/github.com/octu0/concache

Installation

$ go get github.com/octu0/concache

Example

import(
  "time"
  "github.com/octu0/concache"
)

func main(){
  cache := concache.New(
    concache.WithDefaultExpiration(5 * time.Second),
    concache.WithCleanupInterval(10 * time.Minute),
  )

  cache.Set("hello", "123", 1 * time.Second)
  cache.SetDefault("world", "456")
  if value, ok := cache.Get("hello"); ok {
    println("hello " + value.(string))
  }
  if value, ok := cache.Get("world"); ok {
    println("world " + value.(string))
  }

  time.Sleep(1 * time.Second)

  if _, ok := cache.Get("hello"); ok != true {
    println("hello expired")
  }
  if _, ok := cache.Get("world"); ok {
    println("world not expired")
  }
}

License

MIT, see LICENSE file for details.

Documentation

Index

Constants

View Source
const (
	AppName string = "concache"
	Version string = "1.0.3"
)

Variables

This section is empty.

Functions

func WithCacheCapacity added in v1.0.2

func WithCacheCapacity(size int) cacheOptFunc

func WithCleanupInterval added in v1.0.2

func WithCleanupInterval(intval time.Duration) cacheOptFunc

func WithDefaultCacheCapacity added in v1.0.2

func WithDefaultCacheCapacity() cacheOptFunc

func WithDefaultExpiration added in v1.0.2

func WithDefaultExpiration(dur time.Duration) cacheOptFunc

func WithDefaultSlabSize added in v1.0.2

func WithDefaultSlabSize() cacheOptFunc

func WithDeleted added in v1.0.2

func WithDeleted(cb DeletedCb) cacheOptFunc

func WithEvicted added in v1.0.2

func WithEvicted(cb EvictedCb) cacheOptFunc

func WithSlabSize added in v1.0.2

func WithSlabSize(size int) cacheOptFunc

Types

type Cache

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

func New

func New(funcs ...cacheOptFunc) *Cache

func (*Cache) Count

func (c *Cache) Count() int

func (*Cache) Delete

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

func (*Cache) DeleteExpired

func (c *Cache) DeleteExpired()

func (*Cache) Get

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

func (*Cache) Set

func (c *Cache) Set(key string, value interface{}, dur time.Duration)

func (*Cache) SetDefault

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

func (*Cache) SetNoExpire

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

func (*Cache) Upsert

func (c *Cache) Upsert(key string, dur time.Duration, cb UpsertCb)

func (*Cache) UpsertDefault

func (c *Cache) UpsertDefault(key string, cb UpsertCb)

func (*Cache) UpsertNoExpire

func (c *Cache) UpsertNoExpire(key string, cb UpsertCb)

type CacheGetSetDelete

type CacheGetSetDelete interface {
	Set(key string, value interface{}, dur time.Duration)
	SetDefault(key string, value interface{})
	SetNoExpire(key string, value interface{})
	Get(key string) (value interface{}, exist bool)
	Delete(key string) (value interface{}, exist bool)
}

type CacheGetSetUpsertDelete

type CacheGetSetUpsertDelete interface {
	CacheGetSetDelete
	Upsert(key string, dur time.Duration, cb UpsertCb)
}

type CacheItemCount

type CacheItemCount interface {
	Count() int
}

type CacheItemDeleteExpire

type CacheItemDeleteExpire interface {
	DeleteExpired()
}

type DeletedCb

type DeletedCb func(string, interface{})

type EvictedCb

type EvictedCb func(string, interface{})

type UpsertCb

type UpsertCb func(exist bool, oldValue interface{}) (newValue interface{})

Jump to

Keyboard shortcuts

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