Coder Social home page Coder Social logo

activerecord_idnamecache's Introduction

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.

All cache will store in process memory, and will never be expired, until the process dies, so the less kvs you use, the better performance you will get. BTW, 100,000 general strings use 10MB memory.

Some relatived articles: http://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[1]
  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[1]
 => "Android"
ruby-1.9.3-rc1 :003 > QuizTag['Android']
  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['Android']
 => 1

== Copyright MIT, David Chen at eoe.cn

activerecord_idnamecache's People

Contributors

dchentech avatar

Watchers

 avatar James Cloos avatar

Forkers

eoecn

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.