Back to godoc.org
github.com/gadelkareem/cachita

package cachita

v0.2.1
Latest Go to latest
Published: May 22, 2020 | License: MIT | Module: github.com/gadelkareem/cachita

Index

Examples

Constants

const FileIndex = "github.com/gadelkareem/cachita/file-index"

Variables

var (
	ErrNotFound = errors.New("cachita: cache not found")
	ErrExpired  = errors.New("cachita: cache expired")
)

func Id

func Id(params ...string) string

func IsErrorOk

func IsErrorOk(err error) bool

func TypeAssert

func TypeAssert(source, target interface{}) (err error)

type Cache

type Cache interface {
	Get(key string, i interface{}) error
	Put(key string, i interface{}, ttl time.Duration) error // ttl 0:default ttl, -1: keep forever
	Incr(key string, ttl time.Duration) (int64, error)
	Tag(key string, tags ...string) error
	Exists(key string) bool
	Invalidate(key string) error
	InvalidateMulti(keys ...string) error
	InvalidateTags(tags ...string) error
}
Example

Code:

cache := cachita.Memory()
err := cache.Put("cache_key", "some data", 1*time.Minute)
if err != nil {
	panic(err)
}

if cache.Exists("cache_key") {
	// do something
}

var holder string
err = cache.Get("cache_key", &holder)
if err != nil && err != cachita.ErrNotFound {
	panic(err)
}

fmt.Printf("%s", holder) // prints "some data"

err = cache.Invalidate("cache_key")
if err != nil {
	panic(err)
}

Output:

some data

func File

func File() (Cache, error)
Example

Code:

cache, err := cachita.File()
if err != nil {
	panic(err)
}

err = cache.Put("cache_key", "some data", 1*time.Minute)
if err != nil {
	panic(err)
}

var holder string
err = cache.Get("cache_key", &holder)
if err != nil && err != cachita.ErrNotFound {
	panic(err)
}

fmt.Printf("%s", holder) // prints "some data"

Output:

some data

func Memory

func Memory() Cache
Example

Code:

var u url.URL
cacheId := cachita.Id(u.Scheme, u.Host, u.RequestURI())
obj := make(map[string]interface{})
obj["test"] = "data"
err := cachita.Memory().Put(cacheId, obj, 0)
if err != nil {
	panic(err)
}

var cacheObj map[string]interface{}
err = cachita.Memory().Get(cacheId, &cacheObj)
if err != nil && err != cachita.ErrNotFound && err != cachita.ErrExpired {
	panic(err)
}
fmt.Printf("%+v", cacheObj)

Output:

map[test:data]

func NewFileCache

func NewFileCache(dir string, ttl, tickerTtl time.Duration) (Cache, error)

func NewMemoryCache

func NewMemoryCache(ttl, tickerTtl time.Duration) Cache
Example

Code:

cache := cachita.NewMemoryCache(1*time.Millisecond, 1*time.Minute) // default ttl 1 millisecond

err := cache.Put("cache_key", "some data", 0) // ttl = 0 means use default
if err != nil {
	panic(err)
}

time.Sleep(2 * time.Millisecond)
fmt.Printf("%t", cache.Exists("cache_key"))

Output:

false

func NewRedisCache

func NewRedisCache(ttl time.Duration, poolSize int, prefix, addr string) (Cache, error)

func NewSqlCache

func NewSqlCache(ttl, tickerTtl time.Duration, sql *sql.DB, tableName string, isPostgres ...bool) (Cache, error)

func Redis

func Redis(addr string) (Cache, error)

func Sql

func Sql(driverName, dataSourceName string) (Cache, error)
Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
f or F : Jump to identifier