getting started with couchbase ruby

25
Getting Started with Couchbase Ruby Sergey Avseyev [email protected]

Upload: sergey-avseyev

Post on 26-Jun-2015

2.342 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Getting Started with Couchbase Ruby

Getting Started withCouchbase Ruby

Sergey [email protected]

Page 2: Getting Started with Couchbase Ruby

Couchbase Server

Page 3: Getting Started with Couchbase Ruby

What Is Couchbase Server

• NoSQL database solution

• No fixed schema

• Automatic key sharding

• Automatic replication

• Low latency optimized

• No multi-operation transaction support

Page 4: Getting Started with Couchbase Ruby

Who Uses Couchbase Server: Heroku

• Leading cloud service (PAAS) provider

• Over 1,500,000 hosted applications

• Couchbase Server serving over 11,800 Heroku customers

Page 5: Getting Started with Couchbase Ruby

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

Page 6: Getting Started with Couchbase Ruby

Current Version: 1.1.2

Page 7: Getting Started with Couchbase Ruby

Installation

Install stable version of libcouchbase-dev with dependencies usinginstructions at http://www.couchbase.com/develop/c/current

$ gem install couchbaseFetching: couchbase-1.1.2.gem (100%)Building native extensions. This could take a while...Successfully installed couchbase-1.1.21 gems installed

Page 8: Getting Started with Couchbase Ruby

Connect to the Cluster

Pass connection parameters explicitly

conn = Couchbase.connect(:bucket => ’test’)

Use thread-local connection instance

Couchbase.connection_options = :bucket => ’test’conn = Couchbase.bucket

Page 9: Getting Started with Couchbase Ruby

Simple CRUD

Write 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”)

Page 10: Getting Started with Couchbase Ruby

Optimistic Locks

All 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

Page 11: Getting Started with Couchbase Ruby

Expiration

Write 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)

Page 12: Getting Started with Couchbase Ruby

Miscellaneous

Multi 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”)

Page 13: Getting Started with Couchbase Ruby

Next Version: 1.2.0.dp5

Page 14: Getting Started with Couchbase Ruby

Installation

Install next version of libcouchbase-dev with dependencies usinginstructions at http://www.couchbase.com/develop/c/next

$ gem install couchbase --preFetching: couchbase-1.2.0.dp5.gem (100%)Building native extensions. This could take a while...Successfully installed couchbase-1.2.0.dp51 gems installed

Page 15: Getting Started with Couchbase Ruby

Map/Reduce Analysis

Define 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);

}”},

}}

Page 16: Getting Started with Couchbase Ruby

Executing views

Pick up design document:

ddoc = conn.design_docs[”users”]

Iterate over view results

ddoc.all_by_email(:include_docs => true).each do |doc|puts doc.keyputs doc.doc.inspect

end

Page 17: Getting Started with Couchbase Ruby

Pessimistic Locks

value = conn.get(”key”, :lock => 5.seconds)

All subsequent mutators are forced to use corresponding CAS value or fail

Page 18: Getting Started with Couchbase Ruby

Rack Session Storage

# config.rurequire ’rack/session/couchbase’use Rack::Session::Couchbase,

:expire_after => 5.minutes,:couchbase => {:bucket => ”sessions”}

Page 19: Getting Started with Couchbase Ruby

Rails Session Storage

# config/initializers/session_store.rbrequire \’action_dispatch/middleware/session/couchbase_store’

Name::Application.config.session_store :couchbase_store,:expire_after => 5.minutes,:couchbase => {:bucket => ”sessions”}

Page 20: Getting Started with Couchbase Ruby

Rails Cache Storage

# config/application.rbcache_options = {:bucket => ’protected’,:username => ’protected’,:password => ’secret’,:expires_in => 2.hours

}config.cache_store = :couchbase_store, cache_options

Page 21: Getting Started with Couchbase Ruby

Even More :)

Page 22: Getting Started with Couchbase Ruby

ODM (Object Document Model) for Rails

https://github.com/couchbaselabs/ruby-couchbase-model

$ gem install couchbase-model

class Post < Couchbase::Modelattribute :titleattribute :bodyattribute :created_at, :default => lambda { Time.now }view :by_created_at

end

Page 23: Getting Started with Couchbase Ruby

Couchbase for EventMachine (experimental)

https://github.com/couchbaselabs/couchbase-ruby-client-em

$ gem install em-couchbase

Page 24: Getting Started with Couchbase Ruby

Thanks

Questions?

Page 25: Getting Started with Couchbase Ruby

Find these slides at:github.com/avsej/getting-started-with-couchbase-ruby