Git Product home page Git Product logo

rediscache's Introduction

rediscache GoDoc

import "github.com/guregu/rediscache"

rediscache is a small library for caching data in Redis, similar to how one might define a groupcache topic. When getting a value, it will automatically convert the stringly-typed Redis data to whatever you pass to it, kind of like how json.Unmarshal works. I'm still playing around with this, so consider it unstable.

Rationale

I found myself writing code that repeats these actions over and over:

  • Turn some kind of an ID into a Redis key
  • Try to get the value from Redis
  • If missing from the cache, calculate the value and set it in Redis
  • Convert the result string into something usable

This is a generic way to do the above, inspired by groupcache and the standard JSON package.

Usage

// Cache represents one Redis-cached value.
// It will try to get the value from Redis, updating the cahe using the getter if necessary.
// If the Field is empty, it will use GET and SET operations.
// If the Field is not empty, it will use HGET and HSET. HGET/HSET don't support TTLs.
type Cache struct {
	Key    interface{}
	Field  interface{}
	Getter func() (string, error)
	Client *redis.Client

	TTL time.Duration // optional
}

Example

var redisClient *redis.Client
var thingCache = rediscache.Cache{
	Client: redisClient,
	Key:    "my:thing",
	Getter: fetchThing, // func() (string, error)

	TTL: 1 * time.Hour, // optional
}

// Redis is stringly-typed, so you have to give rediscache a string or an error.
func fetchThing() (string, error) {
	var thing int
	// do some expensive calculations
	return strconv.Itoa(thing), nil
}

func doSomethingWithTheData() {
	var thing int
	err := thingCache.Get(&thing) // works similar to the json package
	if err != nil {
		...
	}
	// use thing
}

Polymorphism

In the world of Redis, everything is a string, but that is not so true in the real world. Key and Field take an interface{} and try their best to turn it into a string, including checking for fmt.Stringer and encoding.TextMarshaler. You can also give them a func() String.

	dailyCache := rediscache.Cache{
		Client: client,
		Key:    func() string {
			date := time.Now().Format("2006-01-02")
			return "thing:" + date
		},
		Getter: func() (string, error) {
			var todaysThing string
			// ...
			return todaysThing, nil
		},
	}

Get

var thing myCoolType
thingCache.Get(&thing)

Get works similar to package json's Unmarshal. Pass it a pointer to the variable you want to be set and it will do its best to match up.

Get works for *string, *int, *int{64,32} right now. I will add support for more basic types soon.

Get treats encoding.TextUnmarshaler and json.Unmarshaler properly, in that order of priority.

As a last ditch effort, it will try calling json.Unmarshal() on the Redis data. You can take advantage of this by passing pointers to arbitrary objects if you store JSON data in Redis.

rediscache's People

Contributors

guregu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.