Couchbase memcached

Hi all,

I am totally new to Couchbase and memcached, yet I have a project need using Couchbase and memcached, written in ROR.

I have tried my best to read through the codes, the tutorial and completed the article tutorial project as well. However, I still have a few questions seeking help and direction…

  1. When creating a new data bucket, there is an option to choose whether as Couchbase or Memcached. I have tried Couchbase and with help from some previous sample project, I can view and see my updates in the Views and Document. But when I tried to play with Memcache, at the web console, there seems nothing I can view and see.

  2. Says if I have Memcache bucket and Couchbase bucket both ready, how can I tell which bucket my ROR is writing to?

  3. How can I view any data I written to Memcache bucket? Unlike Couchbase, there is no Document or View button on the web console where i can filter and list my result.

Sorry for being a noob, I do appreciate if anyone could help me and give me direction to look into.
Thanks in advance!

No problem always happy to answer questions so here we go:

  1. The big difference between the couchbase and a memcache bucket is that the couchbase bucket gives you all the power couchbase has, which the memcache bucket exposes a straight memcache compatible instance. This means that for the webinterface there is really no good way to show you what is available, you can however just use any memcache client from ruby and connect to it and use it exactly like you would use memcache. So you could for example use dalli and it will just work, try the examples.

The big thing is that you won’t have the power of views, and your data will not be persisted to disk. It is “just memcache”

  1. In case of couchbase it is defined when establishing the connection, so take a look at the documentation for the connection. By default couchbase connects to a bucket called “default” otherwise you pass a name. It’s always 1 connection per bucket.

For memcache you specify the bucket as the “username” so

require “dalli”

localhost:11211 is the default port for memcache

username maps to the couchbase bucket name

password maps to the password specified in couchbase for this bucket

client = “localhost:11211”, username: “iamacache”, password: “iamacache”

client.set “iamakey”, "i am a value"
puts client.get “iamakey” # => “i am a value”

  1. Well personally I just use a ruby with dalli loaded in, to get some stats about the server you can call stats, which will show you how many keys are written etc.
require "dalli" client = "localhost:11211", username: "iamacache", password: "iamacache" puts client.stats

Hope this helps!

Thanks for the answer, it helps a lot!

For the connection part, when following tutorial and some previous project sample, I see that they have files config/database.yml or config/couchbase.yml, hence I’d like to ask…

  1. Do I define my connection here?
  2. And if I defined my connection here, can I still uses the approach you mentioned?
  3. Does memcached has a different config file? config/memcached.yml?

Thanks again!

This assumes you are using Couchbase Ruby Model which integrates couchbase nicely into RoR. In this case the connection details get specified in the couchbase.yml file and you don’t have to worry about the connection used by your models.

Memcache in Rails is often used with Dalli as the gem to connect to Memcache, and it also works with couchbase to use Memcache buckets, in this case you configure it via your config/environments/development.rb or production.rb and so on. You can find all the details on the dalli project page, there you can also just pass configurations like username and password as I wrote earlier.

Thanks pfehre!!

Just maybe one more question on this topic,

does memcache have any MVC in ROR? Or i just simply implement the code in controller whenever I am needed to use the memcache?


You need to setup Dalli with the configuration inside your config/environments/*.rb files and then Rails will use it as a cache. For more on rails caching as well as how to configure it I recommend reading through the Rails Guides, not just the caching one.

I actually have read that page before but yet too much to digest, still trying my best to understand all of them. Thanks anyway! Just another question pops up…

I tend to have memcached storessome of my app settings (eg. item prizes, item list etc) that are most of the time static and remain the same, so user don’t have to load from bucket each time they connect to the server. Is this feasible?

Loading from a memcache system is really fast so really no need to optimize at this point. If the content is static you might want to put it in an initializer (a piece of ruby code which is run at app boot and can set variables and so forth), but then you can only change it at app boot time. The idea behind Memcache like system is that you can share information between app server instances without much overhead, so keeping stuff like this in memcache / couchbase is actually a really good way to go.

As initializers do you mean my_app_name/config/initializers/?
currently I’ve written something in my login controller, so that when user logs in it calls the model and run the memcaching. Not sure if this is the right way to write?

class Iapshop < CouchbaseModel

attribute :version
attribute :count
attribute :count2

def self.initialize
mc =‘localhost:11211’, username: ‘TestMemcached’, password: ‘zaq12wsx’)

  iap = find('iapshop')
  ver = mc.get('version_iap')

  if iap then

    # Compare the version
    if (ver != iap.version)    # version not match
      # update the memcache
      mc.set('version_iap', iap.version)
      mc.set('count', iap.count)
      mc.set('count2', iap.count2)

      _ver = mc.get('version_iap')
      _c1 = mc.get('count')
      _c2 = mc.get('count2')

      puts 'From DB'
      puts _ver
      puts _c1
      puts _c2

      # puts the value
      _ver = mc.get('version_iap')
      _c1 = mc.get('count')
      _c2 = mc.get('count2')

      puts 'From MC'
      puts _ver
      puts _c1
      puts _c2


So you were saying this code I could’ve put it under initializer instead?
Thanks again in advance! :smiley: