5

In my system,there are many web servers share one cache(memcache) server.

Currently, it will clear all the data in memcache on every new deployment.

by running rake memcached:flush

What's more,I can saw the user session in the cache server,

But every time, when I close the browser on my iPhone, I need to re-login again and again (I must get something wrong).

I set up my server in the back of AWS ELB and auto scaling

How could I keep the users' session among every server behind ELB

To keep the user in logged status every they comes back.

cache server

|       8 |    2016-03-01 10:07:59 +0000 |          291 | _session_id:08f1d7e8e82055367c44372d431b7f23              |
|       8 |    2016-03-01 10:07:22 +0000 |          291 | _session_id:3553ad00c578b175d789f02dc696dd95              |
|       8 |    2016-03-01 10:04:22 +0000 |          291 | _session_id:5cc2302455981a8a5d3cea98deb80acb              |

confi/initialize/session.rb (I save cache with Dalli and memcache)

Rails.application.config.session_store :cookie_store, key: '_sampleA_session'
Rails.application.config.session_store ActionDispatch::Session::CacheStore, :expire_after => 6.month

view caches / model caches

- cache("common_header", skip_digest: true) do
- cache("footer", skip_digest: true) do
...

cache.rake (rake task)

require 'socket' 
namespace :memcached do
  desc 'Flushes whole memcached local instance'
  task :flush do
    server  = ENV['MEMCACHE_DB']
    port    = 11211
    command = "flush_all\r\n"
    socket = TCPSocket.new(server, port)
    socket.write(command)
    result = socket.recv(2)
    if result != 'OK'
      STDERR.puts "Error flushing memcached: #{result}"
    end
    socket.close
  end
end

production.rb

  config.action_controller.perform_caching = true
  config.cache_store = :dalli_store, ENV['MEMCACHE_DB'], { :pool_size => 10 ,compress: true }
newBike
  • 14,385
  • 29
  • 109
  • 192

1 Answers1

2

For your two view fragment caches you can do (docs):

expire_fragment("common_header")
expire_fragment("footer")

This should invalidate the cached fragment and therefore update values you read from the model in this fragment (since the fragment is rerendered). If this is what you mean with model cache you are good to go. If you want to clear the SQL query cache as well (although I do not know why you would want to do so, since Rails invalidates this automatically) you can refer to this blog post.

smallbutton
  • 3,377
  • 15
  • 27
  • it didn't work if i push cached stuff on remote memcache :( – newBike Sep 03 '15 at 09:43
  • Can you be more specific ? I have an app with a remote memecache using nearly the same configuration and expiring caches with the rails mechanics works perfectly fine. – smallbutton Sep 03 '15 at 09:46
  • after running your command the cache still exsists on remote cache server `| id | expires | bytes | cache_key | | 4 | 2016-03-01 09:40:15 +0000 | 74 | _session_id:c388b2a5568dfd2917d0796b30154c68 | | 12 | 2015-09-03 09:37:59 +0000 | 1070 | views/footer | | 13 | 2015-09-03 09:37:59 +0000 | 1163 | views/common_header |` – newBike Sep 03 '15 at 09:48
  • i've tried this on rails console too. `irb(main):004:0> action_controller.expire_fragment("footer") => nil` – newBike Sep 03 '15 at 09:48
  • The strage thing is that your key is named "views/footer" instead of "footer". Mybe try `expire_fragment("views/footer")` – smallbutton Sep 03 '15 at 09:55
  • http://stackoverflow.com/questions/756888/how-do-i-expire-a-view-cached-fragment-from-console Apperently the controller here works different from the console. In the controller it should work without the "views/" – smallbutton Sep 03 '15 at 09:59