欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在Ruby把MySQL做NoSQL用 Friendly简介 博客分类: RubyOnRails RubyMySQLNoSQLmemcachedRails 

程序员文章站 2024-03-22 12:59:46
...
  Friendly是一个github上的一个插件,起源是因为FriendFeed把MySQL做NoSQL的用,具体参见这篇:
http://bret.appspot.com/entry/how-friendfeed-uses-mysql


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



安装friendly


	sudo gem install friendly


在Rails里



	#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"


创建Model


	class BlogPost
	  include Friendly::Document
	   
	  attribute :author, String
	  attribute :title,  String
	  attribute :body,   String
	end


创建Table

#script/console:

	Friendly.create_tables!


索引

	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!


创建Objects

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


查询

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


#Most recent posts:

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


缓存

#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