cache

package module
v5.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2016 License: BSD-2-Clause Imports: 7 Imported by: 0

README

Redis cache library for Golang Build Status

Installation

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

Quickstart

package cache_test

import (
	"fmt"
	"time"

	"gopkg.in/go-redis/cache.v5"

	"gopkg.in/redis.v5"
	"gopkg.in/vmihailenco/msgpack.v2"
)

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)
		},
	}

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

Documentation

Overview

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)
	},
}

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

&{mystring 42}
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}

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrCacheMiss = errors.New("cache: keys is missing")

Functions

This section is empty.

Types

type Codec

type Codec struct {
	Redis rediser

	// Local LRU cache for super hot items.
	LocalCache *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) Do

func (cd *Codec) Do(item *Item) (interface{}, error)

Do gets the item.Object for the given item.Key from the cache or executes, caches, and returns the results of the given item.Func, making sure that only one execution is in-flight for a given item.Key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results.

func (*Codec) Get

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

Get gets the object for the given key.

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

Set caches the item.

type Item

type Item struct {
	Key    string
	Object interface{}

	// Func returns object to cache.
	Func func() (interface{}, error)

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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