lcache

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2019 License: MIT Imports: 4 Imported by: 2

README

lcache

Embedded caching library for Golang

Summary

  • Require Go >= 1.12
  • Written on Go
  • Embedded library
  • Store data as is (key is string, value - interface)
  • Don't create new go-routines
  • Thread-safe
  • Support TTL for cache values
  • Access through cache
  • Caching single object
  • Does not support versioned values
  • Easy to use

Install

go get github.com/belfinor/lcache

Usage

Import
import "github.com/belfinor/lcache"
Create cache
var cache *lcache.Cache = lcache.New("size=102400 nodes=16 ttl=86400")

In this example we create cache object. The object can be used in a multi-threaded access environment. The cache stores data in 16 buckets and expected values limit is 102400 (in practice, one and a half times more). 86400 - TTL in seconds for all created objects. New returns nil if the dsn (size=102400 nodes=16 ttl=86400) is invalid.

You can create as many cache objects as you need in the program (one cache per object class).

Cache values

Cache key is a string value.

Set/Get

Get - get value from cache; Set - save data to cache. If value not found, return value is nil.

cache.Set("1", "11")
cache.Set("2",int64(22))

fmt.Println( cache.Get("1").(string), cache.Get("2").(int64), cache.Get("3") == nil )
Access through cache (Fetch)
tm := cache.Fetch( "123", func(k string) interface{} {
  return time.Now().Unix()
  }).(int64)

Method Fetch return value if it's found in cache. Otherwise call func to get value and store result to cache. If func could not get value then return value is nil.

Delete

Delete key from cache.

cache.Delete("123")
Remove all data
cache.Flush()
Increment/Decrement
cache.Inc("inc")
cache.Inc("inc")
cache.Inc("inc")

fmt.Println( cache.Get("inc").(int64)) // 3

cache.Dec("inc")
cache.Dec("inc")

fmt.Println(cache.Get("inc").(int64)) // 1

fmt.Println(cache.IncBy("inc",12)) // 13
fmt.Println(cache.DecBy("inc",8)) // 5
fmt.Println(cache.DecBy("inc",8)) // 0

int64 is type of result value. Min Dec/DecBy return value is int64(0).

Caching single object

If you need to cache only one object then you can use lcache.Atom. An object can be used in a multi-threaded access environment. Example how to use below:

package main

import "fmt"
import "github.com/belfinor/lcache"

func main() {

  atom := lcache.NewAtom(600) // TTL = 600 second

  fmt.Println( atom.Get() ) // <nil>

  atom.Set("12")

  fmt.Println( atom.Get().(string) ) // 12

  atom.Set(nil)
  fmt.Println( atom.Get() ) // <nil>

  res := atom.Fetch( func() interface{} {
    return []int{1,2,3}
  } )

  fmt.Println( res.([]int) ) // [1 2 3]

}

Used in:

  • LiveJournal recommender system and stat services

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ATOM_FETCH_FUNC

type ATOM_FETCH_FUNC func() interface{}

type Atom

type Atom struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewAtom

func NewAtom(ttl int64) *Atom

func (*Atom) Fetch

func (a *Atom) Fetch(fn ATOM_FETCH_FUNC) interface{}

func (*Atom) Get

func (a *Atom) Get() interface{}

func (*Atom) Reset

func (a *Atom) Reset()

func (*Atom) Set

func (a *Atom) Set(v interface{})

type Cache

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

func New

func New(dsn string) *Cache

cache := New("size=1024 nodes=4 ttl=3600")

func (*Cache) Dec added in v1.0.3

func (c *Cache) Dec(key string) int64

func (*Cache) DecBy added in v1.0.3

func (c *Cache) DecBy(key string, val int64) int64

func (*Cache) Delete

func (c *Cache) Delete(key string)

func (*Cache) Fetch

func (c *Cache) Fetch(key string, f FetchFunc) interface{}

func (*Cache) Flush

func (c *Cache) Flush()

func (*Cache) Get

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

func (*Cache) Inc

func (c *Cache) Inc(key string) int64

func (*Cache) IncBy added in v1.0.3

func (c *Cache) IncBy(key string, val int64) int64

func (*Cache) Set

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

func (*Cache) Size

func (c *Cache) Size() int

type FetchFunc added in v1.0.4

type FetchFunc func(key string) interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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