Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I keep the logged session and clear cache except session in memcache

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 }
like image 236
newBike Avatar asked Oct 31 '22 20:10

newBike


1 Answers

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.

like image 92
smallbutton Avatar answered Nov 04 '22 08:11

smallbutton