cache

package
v0.0.0-...-e7b3743 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2020 License: MIT Imports: 7 Imported by: 0

README

Redis cache library for Go

Installation

go get gopkg.in/go-redis/cache.v1

Quickstart

package cache_test

import (
	"fmt"
	"time"

	"gopkg.in/go-redis/cache.v1"
	"gopkg.in/redis.v3"
	"gopkg.in/vmihailenco/msgpack.v2"
)

type Object struct {
	Str string
	Num int
}

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

		DialTimeout:  3 * time.Second,
		ReadTimeout:  time.Second,
		WriteTimeout: time.Second,
	})

	codec := &cache.Codec{
		Ring: 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}
}

Documentation

Overview

Example (Caching)
package main

import (
	"fmt"
	"time"

	"github.com/danjac/podbaby/cache/Godeps/_workspace/src/gopkg.in/go-redis/cache.v1"
	"github.com/danjac/podbaby/cache/Godeps/_workspace/src/gopkg.in/redis.v3"
	"github.com/danjac/podbaby/cache/Godeps/_workspace/src/gopkg.in/vmihailenco/msgpack.v2"
)

type Object struct {
	Str string
	Num int
}

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

		DialTimeout:  3 * time.Second,
		ReadTimeout:  time.Second,
		WriteTimeout: time.Second,
	})

	codec := &cache.Codec{
		Ring: 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}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrCacheMiss = errors.New("rediscache: cache miss")
)

Functions

This section is empty.

Types

type Codec

type Codec struct {
	Ring *redis.Ring
	// Local LRU cache for hot items.
	Cache *lrucache.Cache

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

func (*Codec) Delete

func (cd *Codec) Delete(key string) error

func (*Codec) Get

func (cd *Codec) Get(key string, v interface{}) error

func (*Codec) Hits

func (cd *Codec) Hits() int

func (*Codec) Misses

func (cd *Codec) Misses() int

func (*Codec) Set

func (cd *Codec) Set(item *Item) error

type Item

type Item struct {
	Key    string
	Object interface{}

	// Expiration is the cache expiration time.
	// Zero means the Item has no expiration time.
	Expiration time.Duration

	// Disables local LRU cache when set to true.
	DisableLocalCache bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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