Getting Started with Couchbase Ruby

3,037 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,037
On SlideShare
0
From Embeds
0
Number of Embeds
39
Actions
Shares
0
Downloads
17
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Getting Started with Couchbase Ruby

  1. 1. Getting Started with Couchbase Ruby Sergey Avseyev sergey@couchbase.com
  2. 2. Couchbase Server
  3. 3. What Is Couchbase Server • NoSQL database solution • No fixed schema • Automatic key sharding • Automatic replication • Low latency optimized • No multi-operation transaction support
  4. 4. Who Uses Couchbase Server: Heroku • Leading cloud service (PAAS) provider • Over 1,500,000 hosted applications • Couchbase Server serving over 11,800 Heroku customers
  5. 5. Who Uses Couchbase Server: Zynga • Social game leader — FarmVille, Mafia Wars, Empires and Allies, Café World, FishVille • Over 230 million monthly users • Couchbase Server is the primary database behind key Zynga properties
  6. 6. Current Version: 1.1.2
  7. 7. InstallationInstall stable version of libcouchbase-dev with dependencies usinginstructions at http://www.couchbase.com/develop/c/current $ gem install couchbase Fetching: couchbase-1.1.2.gem (100%) Building native extensions. This could take a while... Successfully installed couchbase-1.1.2 1 gems installed
  8. 8. Connect to the ClusterPass connection parameters explicitly conn = Couchbase.connect(:bucket => ’test’)Use thread-local connection instance Couchbase.connection_options = :bucket => ’test’ conn = Couchbase.bucket
  9. 9. Simple CRUDWrite key or fail it it is exists already cas = conn.add(”key”, ”value”)Write key unconditionally cas = conn.set(”key”, ”value”)Write key only if it is exists cas = conn.replace(”key”, ”value”) value = conn.get(”key”)
  10. 10. Optimistic LocksAll mutators accepts CAS value (some kind of version or checksum of thekey) value, flags, cas = conn.get(”key”, ”value”, :extended => true)Setting with wrong CAS value will raise Couchbase::Error::KeyExists conn.set(”key”, ”newvalue”, :cas => 1234) #=> Couchbase::Error::KeyExists: failed to store value
  11. 11. ExpirationWrite key and set expiration conn.set(”key”, ”value”, :ttl => 1.minute)Read key and set expiration conn.get(”key”, :ttl => 1.minute)Only update expiration conn.touch(”key”, :ttl => 1.minute)
  12. 12. MiscellaneousMulti get foo, bar = conn.get(”foo”, ”bar”)Multi touch conn.touch(”foo” => 30.seconds, ”bar” => 10.minutes)Increment/Decrement conn.incr(”counter”) conn.decr(”counter”)
  13. 13. Next Version: 1.2.0.dp5
  14. 14. InstallationInstall next version of libcouchbase-dev with dependencies usinginstructions at http://www.couchbase.com/develop/c/next $ gem install couchbase --pre Fetching: couchbase-1.2.0.dp5.gem (100%) Building native extensions. This could take a while... Successfully installed couchbase-1.2.0.dp5 1 gems installed
  15. 15. Map/Reduce AnalysisDefine design document containing single map function (using adminpanel or Bucket#save_design_doc): { ”_id”: ”_design/users”, ”views”: { ”all_by_email”: { ”map”: ”function(doc) { if (doc.type == ”User”) emit(doc.email, null); }” }, } }
  16. 16. Executing viewsPick up design document: ddoc = conn.design_docs[”users”]Iterate over view results ddoc.all_by_email(:include_docs => true).each do |doc| puts doc.key puts doc.doc.inspect end
  17. 17. Pessimistic Locks value = conn.get(”key”, :lock => 5.seconds)All subsequent mutators are forced to use corresponding CAS value or fail
  18. 18. Rack Session Storage # config.ru require ’rack/session/couchbase’ use Rack::Session::Couchbase, :expire_after => 5.minutes, :couchbase => {:bucket => ”sessions”}
  19. 19. Rails Session Storage # config/initializers/session_store.rb require ’action_dispatch/middleware/session/couchbase_store’ Name::Application.config.session_store :couchbase_store, :expire_after => 5.minutes, :couchbase => {:bucket => ”sessions”}
  20. 20. Rails Cache Storage # config/application.rb cache_options = { :bucket => ’protected’, :username => ’protected’, :password => ’secret’, :expires_in => 2.hours } config.cache_store = :couchbase_store, cache_options
  21. 21. Even More :)
  22. 22. ODM (Object Document Model) for Railshttps://github.com/couchbaselabs/ruby-couchbase-model $ gem install couchbase-model class Post < Couchbase::Model attribute :title attribute :body attribute :created_at, :default => lambda { Time.now } view :by_created_at end
  23. 23. Couchbase for EventMachine (experimental)https://github.com/couchbaselabs/couchbase-ruby-client-em $ gem install em-couchbase
  24. 24. ThanksQuestions?
  25. 25. Find these slides at:github.com/avsej/getting-started-with-couchbase-ruby

×