Giter Site home page Giter Site logo

fresh-redis's Introduction

Fresh Redis

A simple, JSON-oriented wrapper around Redis that provides easy mechanisms for keeping your cache fresh.

Config

Set up a Redis client instance as $redis in your config files either as an initializer or in your environments files.

$redis = Redis.new(:host => 'localhost', :port => 6379)

This is where you'd set up a namespace, if you want one.

Retrieve a cached value stored under key "foo"

FreshRedis.get("foo")

Set a cached an evaluated value under key "foo" with an expiration time of 5 minutes

FreshRedis.freshen("foo", 5.minutes, :ruby) do
  {your_age: 30}
end

This will return the value as a Ruby object (Hash, Array, etc)

  • 'freshen' will freshen the value if the expiration time has expired and will return the cached value if it hasn't.
  • 'freshen' will always return the value whether it's retrieved from the cache or it's evaluated in the block. That means it's a great choice for wrapping existing expensive code in a block for out-of-the-box caching.

If you know you'll be returning JSON, you can skip the parsing of the stored JSON into a Ruby object by specifying the format as :json for extra performance.

FreshRedis.freshen("foo", 5.minutes, :json) do
  {your_age: 30}
end

You can set the expire time to 0.minutes to effectively disable caching.

Set a value under key "foo" with no expiration

  FreshRedis.set("foo", {your_age: 30})

Unset the value under key "foo"

  FreshRedis.unset("foo")

That's it!

My most common use-case is this:

response.content_type = "application/json"

return render text: FreshRedis.freshen("dude_this_is_expensive", 5.minute, :json) do
  calculate_and_return_some_expensive_thing
end

fresh-redis's People

Contributors

onyxrev avatar

Stargazers

Imran Ansari avatar

Watchers

Imran Ansari avatar  avatar James Cloos 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.