entity_storage

github.com/eatenbyagrue/entity_storage

DESCRIPTION:

An easy to use Key/Value store for any Ruby on Rails project. Like Memcache, only persistent. Stores config values & application wide state in the database in order to survive server restarts.

Designed to allow you to add persistent value storage to any Rails project in about 5 minutes.

Additionally, allows users to set a list of default keys that auto-initializes baseline key/value pairs in the database for easy initialization.

SYNOPSIS:

You can use the entity store like so:

# Get key value.
e = EntityStore["testkey"]
e = EntityStore.testkey
e = EntityStore[:testkey]

# sets key named 'happened' to a Time object of now
EntityStore[:happened] = Time.now
EntityStore["happened"] = Time.now
EntityStore.happened = Time.now

# find out it's default, even if it's been changed
e = EntityStore.default(:testkey)

# or
e = EntityStore.defaults[:testkey]

# set it back to default
EntityStore.default!(:testkey)

# delete an item
EntityStore.delete(key)

All EntityStorage operations sync immediately with the database, so a server shutdown will not impact stored values.

If you access a key that doesn’t exist, and is specified in default list, will be initialized and returned. If not in default list, will return nil.

Keys can be up to 250 characters in length. Values can be practically any size, and consist of any object. Objects are marshalled back and forth between database.

REQUIREMENTS:

As of 2.1.5, key length reduced to allow indexing on MySQL 5.6

As of 2.1.4, tested with Ruby 2.1.2.

As of 2.1.0, the MySQL2 gem and Ruby 1.9.3 are now supported.

As of 2.0.0, Rails 3.0.3 gems or above required.

Version up to 1.0.4 requires Ruby 1.8.7 and ActiveRecord 2.2.3 or above (probably works with earlier versions, but has not been tested.) (earlier versions had an incorrect gem name, and are installed with ‘gem install entity_storage’)

INSTALL:

Install the gem(s):

sudo gem install entity_storage

Or download the source above and run, customized for version and environment:

sudo gem install PATH/entity_storage.#.#.#.gem

Put the following in an initializer file named RAILS_ROOT/config/initializers/entity_storage.rb

require 'entity_storage'
DEFAULT_KEYS = { "testkey" => DateTime.parse("1-1-900"), "also test" => 2, "long ass key that I probably wouldn't use" => false }
EntityStore = EntityStorage::Storage.new(DEFAULT_KEYS)

On initialization, if the table “entity_storage” doesn’t exist, it will be created. If you already have a table with that name, it must be in the correct format (previously created by this gem.)

You can pass a hash full of default key/value pairs. If the application accesses one of the keys, and it doesn’t already exist, it will initiliaze that key with the default value. Good for getting your app to an initial starting state.

LICENSE:

(The MIT License)

Copyright © 2009-2014 Joshua Siler

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.