Introducing Kue - A Rails Ready Key Value Store That Uses ActiveRecord Under The Hood

A few weeks ago I wrote a post on how I had implemented a simple key value store using activerecord.

I received a lot of feedback not only via the comment's on the post but also by email. As so many people showed interest I decided to create an activerecord backed key value store gem called kue encompassing some of the idea's and thought's that people had brought to my attention.

Getting setup with kue is easy as it has a rails generator to copy over the migration needed in order to setup the underlying key value storage table.

            rails generate kue:install
            rake db:migrate
            

When you want to set or retrieve any arbitrary keys value then you can do so by calling on the KueStore class.

            #Set the value of the key
            KueStore[:any_key_name_you_can_think_of] = "Any object you can dream up"
            #Retrieve the value of the key 
            KueStore[:any_key_name_you_can_think_of] 
            

There is also a helper method to check if a key already exists.

            KueStore.exists?(:my_class_instance)
            

One of the nice things about kue is it can store any object you can think of complex or simple!

             KueStore[:my_class_instance] = Foo.new(:name => 1)
            

Let me know what you think. Pull requests are always welcome!

Tweet Me | Link To Facebook

ActiveRecord - Simple Key Value Store

Over the last few weeks I have been playing with the idea of having a simple database backed key value store using Rails ActiveRecord. The ActiveRecord class should act like a singleton where arbitrary keys can be set with a value of any type (simple values, hashes, arrays or complex objects). I have come up with the idea of using method missing to set and retrieve the key / value pair where the value's are saved as yaml and then cast back to the correct type when retrieved.

            class Application < ActiveRecord::Base
              set_table_name "application_settings"
              
              def self.method_missing(method, *args, &block)
                return self.send method, *args, &block if self.respond_to? method
                method_name = method.to_s
                if method_name =~ /=/
                  return self.set method_name.gsub("=", ""), args.first
                else
                  return self.get method_name
                end
              end
              
              private 
              def self.get setting
                entry = Application.where(:key => setting).first
                entry.nil? ? nil : YAML.load(entry.value)
              end
              
              def self.set key, value
                setting = Application.where(:key => key).first || Application.new(:key => key)
                setting.update_attribute(:value, value.to_yaml)
              end
            end
            

Below is the migration needed to setup the database table for your application settings class.

            class CreateApplicationSettings < ActiveRecord::Migration
              def up
                create_table :application_settings, :id => false do |t|
                  t.string :key
                  t.text :value
            
                  t.timestamps
                end
                add_index :application_settings, :key, :unique => true
              end
              
              def down
                drop_table :application_settings
              end
            end
            

Using the class it is possible to set a value to any arbitrary key by calling the setting name setter method just like it is a defined class attribute.

            Application.some_arbitrary_setting = 12903
            Application.my_hash_foo = { :foo => "bar" }
            

To retrieve the setting make a method call to the key name as if it were a defined class attribute.

            Application.some_arbitrary_setting
            => 12903 
            
            Application.my_hash_foo 
            => {:foo=>"bar"}
            
Tweet Me | Link To Facebook

Month List

  • 2011-December (3)
  • 2011-November (2)
  • 2011-October (1)
  • 2011-September (1)
  • 2011-August (1)
  • 2011-June (2)
  • 2011-May (4)
  • 2011-April (1)
  • 2011-March (3)
  • 2011-February (2)
  • 2011-January (2)
  • 2010-December (1)
  • 2010-November (1)
  • 2010-September (1)
  • 2010-June (3)
  • 2010-May (1)
  • 2010-April (3)
  • 2010-March (2)
  • 2010-February (2)
  • 2010-January (2)
  • 2009-December (2)
  • 2009-November (2)
  • 2009-October (2)
  • 2009-September (2)
  • 2009-July (2)
  • 2009-June (2)
  • 2009-May (2)
  • 2009-April (2)

Tag Cloud

  • activerecord
  • annotate
  • appsettings
  • ashx
  • asp.net
  • asp.net mvc
  • asp.netmvc
  • blogging
  • book review
  • bookreview
  • c#
  • caching
  • calender
  • cancer
  • data annotations
  • datamapper
  • dependency injection
  • development
  • errors
  • fluent nhibernate
  • funny
  • hardware
  • http
  • httpruntime
  • iis7
  • image resize
  • irb
  • jacob
  • javascript
  • jquery
  • jruby
  • jsshell
  • kue
  • life
  • linq 2 nhibernate
  • linq 2 sql
  • master pages
  • mini_record
  • model
  • mvc
  • mysql
  • nhibernate
  • nunit
  • patterns
  • performance testing
  • powershell
  • productivity
  • pry
  • rapuncel
  • ravendb
  • ruby
  • ruby on rails
  • rubyonrails
  • rvm
  • security
  • selectlist
  • session
  • shout mouth
  • specs
  • sqlite
  • stored procedures
  • structuremap
  • theming
  • tools
  • tribe.cache
  • ultimate dev machine
  • unit testing
  • wcat
  • web guy
  • web service
  • webforms
  • xmlrpc
  • xunit
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.