Couchbase memcached expiration

Hi all,

I am working Couchbase with Ruby on Rails, after googling, I have written below code base on what I've found from the Internet.

config/environments, development.rb, production.rb and test.rb
config.cache_store = :dalli_store, 'localhost:11211', {:namespace => 'TestServer', :expires_in => 5.seconds, :compress => true}

Then in config/initializers/session_store.rb
Rails.application.config.session_store ActionDispatch::Session::CacheStore, :expire_after => 5.seconds

When I am storing data to the memcached, I use below coding:

    $mc_server = Dalli::Client.new('localhost:11211', username: 'TestMemcached', password: '123123')
    $mc_server.set('testdata', '111', 3.seconds)

After this, I can see that in the Couchbase web console, the item count under my TestMemcached bucket increased to 1, but after a long long time, the the item is still here; which I expect should be expired and removed back to 0.

Did I misunderstand something or done something wrong? I'd like to have memcached store some of my data for a short period, i.e. 30 seconds, and expire and remove it afterward. Could anyone kindly help advise on this?

Many thanks!

1 Answer

« Back to question.

Hey there,

This actually sounds like it is working as expected, although I can understand that it might seem odd at first.

Couchbase expires items lazily. So, even after their expiry time has passed, your documents will remain in the cache. They will be deleted when either:

* they are accessed: Couchbase will check if the document has expired before returning it; if it has expired it will delete and return a "not found"
* you hit the watermark of your cache: i.e. you start to run out of RAM.

Try accessing an expired document and see how that affects the document count for your bucket.

Thanks,

Matthew.