Giter VIP home page Giter VIP logo

redis_support's Introduction

Redis Support

redis_support is a small library which provides simple support for common actions for the Redis key-value store. It is not an object-relational mapper, does not attempt to comprehensively solve all of your storage problems, and does not make julienne fries.

Redis Addition

Provides a connection to redis which is the same across all classes which use RedisSupport as a mixin. This connection can be set by calling, for example:

RedisSupport.redis = "localhost:6379"

Key Support

Redis provides a global keyspace. Most projects specify their keys using namespaces delimited by colons (:), with variables mixed in at particular points. For example:

users:1:email

redis_support, when used as a mixin, will provide a simple method to declare that namespace and access it later.

class User
  include RedisSupport
  redis_key :email, "users:USER_ID:email"

  attr_accessor :id

  def email
    redis.get Keys.email( self.id )
  end
end

Helpful exceptions are raised if you try to declare the same namespace twice - RedisSupport keeps track of all the key definitions your app wants to use.

Locking Support

There is also a simple locking mechanism based on SETNX. Locking is usually unnecessary since Redis operations are atomic and Redis' MULTI/EXEC/DISCARD gives you the ability to make multiple operations atomic. But sometimes it's useful - a contrived example - major_operation will block for up to 30 seconds if another process attempts to run it at the same time:

class User
  include RedisSupport
  redis_key :email, "users:USER_ID:email"

  attr_accessor :id

  def major_operation
    redis_lock Keys.email( self.id ) do
      # do my expensive stuff
    end
  end
end

Issues

  • When RedisSupport.redis is called but not yet set, there is a stack-level too deep error though it should just return nil.

redis_support's People

Contributors

bpo avatar

Stargazers

 avatar

Watchers

 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.