Search code examples
ruby-on-railsrubyruby-on-rails-4rackrack-cache

Invalidating individual keys from rack-cache?


Let's say a path on my rails app got stuck in rack cache. Is there a way to say: "/games/zelda" should be removed/invalidated from rack-cache?


Solution

  • Assumtions

    1. Your rails app is named MyApp
    2. the complete url you wish to purge is http://www.myapp.com/games/zelda

    Step 1 obtain a normalized key

    mock_request = Rack::MockRequest.env_for('/games/zelda', {"SERVER_NAME"=>"www.myapp.com"})
    key = Rack::Cache::Key.call(Rack::Cache::Request.new(mock_request))
    

    Step 2 retrieve storage objects

    metastore_uri = MyApp::Application.config.action_dispatch.rack_cache[:metastore]
    entitystore_uri = MyApp::Application.config.action_dispatch.rack_cache[:entitystore]
    
    metastore = Rack::Cache::Storage.instance.resolve_metastore_uri(metastore_uri)
    entitystore = Rack::Cache::Storage.instance.resolve_entitystore_uri(entitystore_uri)
    

    Step 3 retrieve the metadata

    stored_meta = metastore.read(key)
    

    Step 4 purge the entity store for each compression type

    stored_meta.each do |sm|
      entitystore.purge(sm[1]["X-Content-Digest"])
    end
    

    Step 5 purge the metastore

    metastore.purge(key)
    

    I hope this helps.