cache

package module
v8.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2019 License: BSD-2-Clause Imports: 9 Imported by: 0

README

Redis cache library for Golang

Build Status GoDoc

Installation

go-redis/cache requires a Go version with Modules support and uses import versioning. So please make sure to initialize a Go module before installation:

go get -u github.com/coinread/twotier-cache/v8

Quickstart

package cache_test

import (
	"fmt"
	"time"

	"github.com/go-redis/redis/v7"
	"github.com/vmihailenco/msgpack/v4"

	"github.com/coinread/twotier-cache/v8"
)

type Object struct {
	Str string
	Num int
}

func Example_basicUsage() {
	ring := redis.NewRing(&redis.RingOptions{
		Addrs: map[string]string{
			"server1": ":6379",
			"server2": ":6380",
		},
	})

	codec := &cache.Codec{
		Redis: ring,

		Marshal: func(v interface{}) ([]byte, error) {
			return msgpack.Marshal(v)
		},
		Unmarshal: func(b []byte, v interface{}) error {
			return msgpack.Unmarshal(b, v)
		},
	}

	key := "mykey"
	obj := &Object{
		Str: "mystring",
		Num: 42,
	}

	codec.Set(&cache.Item{
		Key:        key,
		Object:     obj,
		Expiration: time.Hour,
	})

	var wanted Object
	if err := codec.Get(key, &wanted); err == nil {
		fmt.Println(wanted)
	}

	// Output: {mystring 42}
}

func Example_advancedUsage() {
	ring := redis.NewRing(&redis.RingOptions{
		Addrs: map[string]string{
			"server1": ":6379",
			"server2": ":6380",
		},
	})

	codec := &cache.Codec{
		Redis: ring,

		Marshal: func(v interface{}) ([]byte, error) {
			return msgpack.Marshal(v)
		},
		Unmarshal: func(b []byte, v interface{}) error {
			return msgpack.Unmarshal(b, v)
		},
	}

	obj := new(Object)
	err := codec.Once(&cache.Item{
		Key:    "mykey",
		Object: obj, // destination
		Func: func() (interface{}, error) {
			return &Object{
				Str: "mystring",
				Num: 42,
			}, nil
		},
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(obj)
	// Output: &{mystring 42}
}

Documentation

Overview

Example (AdvancedUsage)
codec := newTieredCache()
key := "advancedUsage"

obj := new(Object)
err := codec.SetStatic(key, time.Hour, &Object{
	Str: "mystring",
	Num: 42,
})

if err != nil {
	panic(err)
}

fmt.Println(obj)
Output:

&{mystring 42}
Example (BasicUsage)
codec := newTieredCache()

key := "basicUsage"
obj := &Object{
	Str: "mystring",
	Num: 42,
}

_ = codec.SetStatic(key, time.Hour, obj)

var wanted Object
if err := codec.Get(key, &wanted); err == nil {
	fmt.Println(wanted)
}
Output:

{mystring 42}

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrCacheMiss = errors.New("cache: key is missing")
View Source
var ErrNilValueProvided = errors.New("cache: nil value(s) can NOT be stored")

Functions

This section is empty.

Types

type Redis

type Redis interface {
	Set(key string, value interface{}, expiration time.Duration) *redis.StatusCmd
	Get(key string) *redis.StringCmd
	Del(keys ...string) *redis.IntCmd
	PTTL(key string) *redis.DurationCmd
}

type Stats

type Stats struct {
	Hits        uint64
	Misses      uint64
	LocalHits   uint64
	LocalMisses uint64
}

type TwoTier

type TwoTier struct {
	R Redis
	L *inmemory.Cache2Go

	Marshal   func(interface{}) ([]byte, error)
	Unmarshal func([]byte, interface{}) error
	// contains filtered or unexported fields
}

func New

func New() *TwoTier

func (*TwoTier) Delete

func (tt *TwoTier) Delete(key string) error

func (*TwoTier) Exists

func (tt *TwoTier) Exists(key string) bool

func (*TwoTier) Get

func (tt *TwoTier) Get(key string, target interface{}) error

func (*TwoTier) Set

func (tt *TwoTier) Set(key string, expiresIn time.Duration, cb func() (interface{}, error)) (interface{}, error)

func (*TwoTier) SetStatic

func (tt *TwoTier) SetStatic(key string, expiresIn time.Duration, value interface{}) error

Funny implementation

func (*TwoTier) Stats

func (tt *TwoTier) Stats() Stats

func (*TwoTier) UseLocalCache added in v8.0.2

func (tt *TwoTier) UseLocalCache(name string, defaultExpiry time.Duration)

func (*TwoTier) UseRedisCache added in v8.0.2

func (tt *TwoTier) UseRedisCache(redis Redis)

Directories

Path Synopsis
singleflight
Package singleflight provides a duplicate function call suppression mechanism.
Package singleflight provides a duplicate function call suppression mechanism.

Jump to

Keyboard shortcuts

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