在Ruby把MySQL做NoSQL用 Friendly简介

[url=http://github.com/jamesgolick/friendly]Friendly[/url]是一个github上的一个插件,起源是因为FriendFeed把MySQL做NoSQL的用,具体参见这篇:
[url]http://bret.appspot.com/entry/how-friendfeed-uses-mysql[/url]


Friendly就是这样的作用,把MySQL变成一个文件数据库来用。NoSQL日趋狂热,schemaless是其中一个原因。客户端的改变可以将MySQL达到同样目的。下面是简单的介绍:


[size=large]安装friendly[/size]


	sudo gem install friendly


[size=large]在Rails里[/size]


	#environment.rb:
config.gem "friendly"


#and create a config/friendly.yml:

development:
:adapter: "mysql"
:host: "localhost"
:user: "root"
:password: "swordfish"
:database: "friendly_development"


没有使用Rails:

Friendly.configure :adapter => "mysql",
:host => "localhost",
:user => "root",
:password => "swordfish",
:database => "playing_with_friendly"


[size=large]创建Model[/size]


	class BlogPost
include Friendly::Document

attribute :author, String
attribute :title, String
attribute :body, String
end


[size=large]创建Table[/size]

#script/console:

Friendly.create_tables!


[size=large]索引[/size]


class BlogPost
include Friendly::Document

attribute :author, String
attribute :title, String
attribute :body, String

indexes :author
indexes :created_at
end


#Run create_tables again and the index tables will be created for you.
Friendly.create_tables!


[size=large]创建Objects[/size]

#With familiar ActiveRecord syntax:
BlogPost.create :author => "James Golick",
:title => "Friendly has familiar syntax.",
:body => "So, there's very little learning curve."


[size=large]查询[/size]

	BlogPost.all(:author => "James Golick")


#Most recent posts:

BlogPost.all(:order! => :created_at.desc)

[size=large]
缓存[/size]

#Install the memcached gem:

sudo gem install memcached

#配置 Friendly:

Friendly.cache = Friendly::Memcached.new(Memcached.new)

#Configure your model to cache:

class BlogPost
include Friendly::Document

attribute :author, String
attribute :title, String
attribute :body, String

indexes :author
indexes :created_at

caches_by :id
end

你可能感兴趣的:(RubyOnRails)