Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Redis Caching #10

Closed
mgutz opened this issue Jun 18, 2015 · 2 comments
Closed

Add Redis Caching #10

mgutz opened this issue Jun 18, 2015 · 2 comments

Comments

@mgutz
Copy link
Owner

mgutz commented Jun 18, 2015

Implement caching using Redis.

type Cacher interface {
   // Cache caches the result of a Select or SelectDoc. If id is not provided, an FNV checksum
   // of the SQL is used as the id. (If interpolation is set, arguments are hashed). Use invalidate to 
   // immediately invalidate the cache to force setting its value.
    Cache(id string, duration time.Duration, invalidate bool)
}

// cache pre-defined game information for a week
DB.SelectDoc("*").
     From("games").
     Cache("games", 7 * 24 * time.Hour, false).
     QueryStruct(&games)

// cache user information for 30 seconds
DB.SelectDoc("*").
     From("users").
     Where("user_id = $1", id).
     Cache("", 30 * time.Second, false).
     QueryStruct(&user)

// invalidates cache for key "games"
DB.InvalidateCache("games")

// invalidates all 
DB.InvalidateCache(")
@elithrar
Copy link

Neat idea. I assume the implementation of Cache is to fall back to querying directly if the hash of the query (or query + args) does not exist in Redis?

I'd be curious to see the benchmarks for a "big set of cached results from Redis" vs. "big set of cached results from the DB" (given that Postgres caches too).

@mgutz
Copy link
Owner Author

mgutz commented Jun 19, 2015

Correct, the cache will get updated:

  • entry does not exist (either new entry or redis expired it)
  • invalidate is true

@mgutz mgutz closed this as completed Jun 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants