Skip to content

eoecn/activerecord_idnamecache

 
 

Repository files navigation

activerecord_idnamecache

Use Mysql AUTO_INCREMENT to support key value cache, which should be combined by an integer and string. It means to reduce the database storage size, and improve query performance.

Some relatived articles: en.wikipedia.org/wiki/Correlation_database

Usage

setup

create_table :kv_browser_names, :options => 'ENGINE=MyISAM DEFAULT CHARSET=utf8' do |t|
  t.string :name
  t.timestamps
end

class KvBrowserName < ActiveRecord::Base
  include IdNameCache
end

or

create_table :common_tag, :options => 'ENGINE=MyISAM DEFAULT CHARSET=utf8' do |t|
  t.integer :tagid
  t.string  :tagname
end

class CommonTag < ActiveRecord::Base
  self.table_name = :common_tag
  self.primary_key = :tagid

  include IdNameCache; set_key_value :tagid, :tagname
  # include IdNameCache; set_key_value_without_create :tagid, :tagname # if you dont want create it automately
end

use cases

ruby-1.9.3-rc1 :001 > QuizTag

 QuizTag Load (0.3ms)  SELECT `common_tag`.* FROM `common_tag` WHERE `common_tag`.`tagid` = 1 LIMIT 1
=> "Android"

ruby-1.9.3-rc1 :002 > QuizTag

=> "Android"

ruby-1.9.3-rc1 :003 > QuizTag

 QuizTag Load (0.5ms)  SELECT `common_tag`.* FROM `common_tag` WHERE `common_tag`.`tagname` = 'Android' LIMIT 1
=> 1

ruby-1.9.3-rc1 :004 > QuizTag

=> 1

Contributing to activerecord_idnamecache

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.

  • Fork the project.

  • Start a feature/bugfix branch.

  • Commit and push until you are happy with your contribution.

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2013 David Chen at eoe.cn. See LICENSE.txt for further details.

About

Use Mysql AUTO_INCREMENT to support id name cache

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published