recache

package module
v0.0.0-...-0b856bb Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2018 License: MIT Imports: 3 Imported by: 0

README

Build Status GoDoc Go Report Card

About the project

This is an experimental library. It has a simple interface that lets you to cache data using redis and invalidate cache by tags.

If you are not familiar with invalidation by tags, then I suggest you reading this article https://symfony.com/blog/new-in-symfony-3-2-tagged-cache.

How to use

package main

import "github.com/go-redis/redis"
import "github.com/beono/recache"

func main() {

    // initialize go-redis client
    cl = redis.NewClient(&redis.Options{
        Addr: "localhost:6379",
    })

    recache = recache.NewRedisCache(cl)

    result, err := recache.Get("orders_by_user_1");
    if err == cache.ErrKeyNotFound {
        // get data from the database here
        result := getOrdersByUserID(1)

        // we can tag this cache entry.
        // if user bought iPad then we can use this tag,
        // so later we can invalidate this cache when we update ipad entity
        if err := recache.Set("orders_by_user_1", result, 3600, "orders", "ipad"); err != nil {
            t.Errorf("unexpected error: %q", err)
        }
    }

    if err != nil {
        panic(err)
    }

    fmt.Println(result)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrKeyNotFound = errors.New("key doesn't exist")

ErrKeyNotFound says that `key doesn't exist`

Functions

This section is empty.

Types

type RedisCache

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

RedisCache implements cache for redis

func NewRedisCache

func NewRedisCache(redis *redis.Client) RedisCache

NewRedisCache returns new RedisCache

func (*RedisCache) ClearByTag

func (m *RedisCache) ClearByTag(tag string) error

ClearByTag deletes all keys that have the specified tag

func (*RedisCache) Get

func (m *RedisCache) Get(id string) ([]byte, error)

Get returns data from redis by key

func (*RedisCache) Set

func (m *RedisCache) Set(id string, data interface{}, ttl uint, tags ...string) error

Set saves data into redis Use ttl for `SETEX`-like behavior. Zero expiration means the key has no expiration time.

Jump to

Keyboard shortcuts

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