Crop Rails - XML โ€‹โ€‹Files?

My Rails application runs on a virtual machine. VM performance is just fine with static pages. In fact, I am launching another site using Apache shared hosting, which just serves static HTML files, and the answer is enough. However, a Rails application that dynamically generates XML files responds very slowly. In fact, it takes about 10 seconds or so for each XML file. These XML files created by Rails do not change more than once a day.

What is the best practice for setting up these XML files for caching?

Change 1:

I should mention that these XML files are not browsable by the browser. They are considered by mobile applications in the "field". Therefore, unfortunately, the transmission "HTTP / 1.0 304 is not changed" will not work.

Edit 2:

If that matters, I use Phusion Passenger to host my Rails application.

+3
source share
2 answers

If you use static rail caching and serve through apache, simply using the explicit xml extension on the URL, you will do it.

if you use only xml and html, you can also edit apache conf by default instead of xml instead of xtml when looking for cached files.

- , , .


, , rails:

,

class XmlThingController < ApplicationController
  caches_page :index, :show, :other_actions

/, , xmls:

class Admin::SomeCrudController < AppplicationController
  cache_sweeper :stupid_master_sweeper, :only => [ :save, :destroy ]

'config/environment/production.rb'

config.action_controller.page_cache_directory = 
  File.join(RAILS_ROOT, 'public', 'cache')

- vhost apache conf:

# 1.4. Handle caching

# 1.4.1. prevent direct cache access
RewriteRule  ^/cache - [F,L]

# 1.4.2. for index
RewriteCond  %{DOCUMENT_ROOT}/cache/index.html -f
RewriteRule  ^/?$ /cache/index.html [L]

# 1.4.3. for explicitly specified extensions
RewriteCond  %{DOCUMENT_ROOT}/cache%{REQUEST_URI} -f
RewriteRule  ^(.*)$ /cache$1 [L]

# 1.4.4. with html extension implied
RewriteCond  %{DOCUMENT_ROOT}/cache%{REQUEST_URI}.html -f
RewriteRule  ^(.*)$ /cache$1.html [L]


# 1.5. Finally, proxy everything else to mongrel
RewriteCond  %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule  ^/(.*)$ balancer://your-website-proxy%{REQUEST_URI} [P,QSA,L]

, :

class StupidMasterSweeper < ActiveRecord::Observer
  observe Foo, Bar # All models that affect caching here

  def after_save(record); end
  def after_destroy(record); end

  def filter(controller)
    # sweep everything.
    `cd #{RAILS_ROOT} && RAILS_ENV=#{ENV['RAILS_ENV']} rake cache:clear`
  end
end

lib/tasks/cache.rake

namespace :cache do 

  desc "Remove all cached files" 
  task :clear do
    puts `rm -rf #{RAILS_ROOT}/public/cache/*`
  end 

end 

xml, 1.4.2 :

# 1.4.4. with html extension implied
RewriteCond  %{DOCUMENT_ROOT}/cache%{REQUEST_URI}.html -f
RewriteRule  ^(.*)$ /cache$1.html [L]

# 1.4.4. with xml extension implied
RewriteCond  %{DOCUMENT_ROOT}/cache%{REQUEST_URI}.xml -f
RewriteRule  ^(.*)$ /cache$1.xml [L]
+7
0

Source: https://habr.com/ru/post/1705726/


All Articles