Giter VIP home page Giter VIP logo

settings-in-redis's Introduction

settings-in-redis

This gem provides a Settings module that is written to be API-compatible with the v1.x ledermann-rails-settings gem for the management of global settings.

The Settings module manages key-value pairs that are stored in Redis. Each setting is stored as a key with a string value in Redis. You can store any kind of object which can be serialized as YAML.

Installation

Include the gem in your Gemfile

gem 'settings-in-redis'

The connection to Redis must be specified for Settings, for example in an initializer:

Settings.redis = Redis.new

Alternatively, a ConnectionPool can be used to share Redis connections among threads:

Settings.redis_pool = ConnectionPool.new { Redis.new }

Setting values are stored in Redis with a default prefix of "settings:".

Usage

The syntax is easy. First create some settings to keep track of:

Settings.admin_password = 'supersecret'
Settings.date_format    = '%m %d, %Y'
Settings.cocktails      = ['Martini', 'Screwdriver', 'White Russian']
Settings.foo            = 123
Settings.credentials    = { :username => 'tom', :password => 'secret' }

Then read them back:

Settings.foo
# => 123

Changing an existing setting is the same as creating a new setting:

Settings.foo = 'super duper bar'

To change an existing setting which is a Hash, you can merge new values with existing ones:

Settings.merge! :credentials, :password => 'topsecret'
Settings.credentials
# => { :username => 'tom', :password => 'topsecret' }

Decide that a particular setting is no longer needed?

Settings.destroy :foo
Settings.foo
# => nil

Want a list of all the settings?

Settings.all
# => { 'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y' }

If you want to group settings and list the settings for a particular namespace, then choose your preferred namespace delimiter and use Settings.all like this:

Settings['preferences.color'] = :blue
Settings['preferences.size'] = :large
Settings['license.key'] = 'ABC-DEF'
Settings.all('preferences.')
# => { 'preferences.color' => :blue, 'preferences.size' => :large }

You can set defaults for certain settings of your app. This will cause the defined settings to return with the specified value even if they are not in Redis. Make a new file in config/initializers/settings.rb with the following:

Settings.defaults[:some_setting] = 'footastic'

Now even if there are not settings in Redis, the app will have some intelligent defaults:

Settings.some_setting
# => 'footastic'

For better performance, you can enable caching, e.g.:

Settings.cache = ActiveSupport::Cache::MemoryStore.new
Settings.cache_options = { :expires_in => 5.minutes }

In our experience, this 5 minute caching was always necessary in a production Rails application where a setting may be used in every request.

settings-in-redis's People

Contributors

alexjwayne avatar cubicfeat avatar faustus7 avatar ledermann avatar lmelnyk-va avatar mceachen avatar michaelklishin avatar mkostick avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.