Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Rails cache store in Front Cache plugin #662

Merged
merged 1 commit into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/apps/plugins/front_cache/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def save_settings
post_types: (params[:cache][:post_type]||[]),
skip_posts: (params[:cache][:skip_posts]||[]),
cache_login: params[:cache][:cache_login],
home: params[:cache][:home]
home: params[:cache][:home],
preserve_cache_on_restart: params[:cache][:preserve_cache_on_restart]
})
flash[:notice] = "#{t('plugin.front_cache.message.settings_saved')}"
redirect_to action: :settings
Expand All @@ -20,7 +21,7 @@ def save_settings
def clean_cache
flash[:notice] = "#{t('plugin.front_cache.message.cache_destroyed')}"
front_cache_clean()
if Rails.version.to_s[0].to_i < 5
if Rails.version.to_i < 5
redirect_to :back
else
redirect_back(fallback_location: '/admin/plugins')
Expand Down
4 changes: 2 additions & 2 deletions app/apps/plugins/front_cache/config/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Front Cache",
"descr": "Please check documentation <a href='http://camaleon.tuzitio.com/store/plugins/3'>here.</a>",
"version": "0.1",
"version": "0.2",
"key": "front_cache",
"position": 1,
"helpers": [
Expand All @@ -18,4 +18,4 @@
"front_after_load": ["front_cache_front_after_load"]
//here you can add all your hooks (read documentation)
}
}
}
26 changes: 10 additions & 16 deletions app/apps/plugins/front_cache/front_cache_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ module Plugins::FrontCache::FrontCacheHelper

# save as cache all pages configured on settings of this plugin for public users
def front_cache_front_before_load
if current_site.get_option("refresh_cache") # clear cache every restart server
front_cache_clean
if current_site.get_option("refresh_cache") # clear cache every restart server unless option checked in settings
front_cache_clean unless current_site.get_meta("front_cache_elements")[:preserve_cache_on_restart]
current_site.set_option("refresh_cache", false)
end

return if signin? || Rails.env == "development" || Rails.env == "test" || !request.get? # avoid cache if current visitor is logged in or development environment

cache_key = request.fullpath.parameterize
if !flash.keys.present? && front_cache_exist?(cache_key) # recover cache file
if !flash.keys.present? && front_cache_exist?(cache_key) # recover cache item
Rails.logger.info "Camaleon CMS - readed cache: #{front_cache_plugin_get_path(cache_key)}"
response.headers['PLUGIN_FRONT_CACHE'] = 'TRUE'
args = {data: front_cache_get(cache_key).gsub("{{form_authenticity_token}}", form_authenticity_token)}; hooks_run('front_cache_reading_cache', args)
Expand Down Expand Up @@ -87,38 +87,32 @@ def front_cache_post_requests
end
end

# clear all frontend cache files
# clear all frontend cache items
def front_cache_clean
FileUtils.rm_rf(front_cache_plugin_get_path) # clear site pages cache
Rails.cache.clear
end

private

def front_cache_exist?(key)
File.exist?(front_cache_plugin_get_path(key))
!(Rails.cache.read(key).nil?)
end

def front_cache_get(key)
File.read(front_cache_plugin_get_path(key))
end

def front_cache_destroy(key)
FileUtils.rm_f(front_cache_plugin_get_path(key)) # clear site pages cache
Rails.cache.read(key)
end

def front_cache_plugin_cache_create(key, content)
FileUtils.mkdir_p(front_cache_plugin_get_path) unless Dir.exist?(front_cache_plugin_get_path)
File.open(front_cache_plugin_get_path(key), 'wb'){ |fo| fo.write(content) }
content
Rails.cache.write(front_cache_plugin_get_path(key), content)
end

# return the physical path of cache directory
# key: (string, optional) the key of the cached page
def front_cache_plugin_get_path(key = nil)
unless key.nil?
Rails.root.join("tmp", "cache", "pages", current_site.id.to_s, "#{key}.html").to_s
"pages/#{current_site.id.to_s}/#{key}"
else
Rails.root.join("tmp", "cache", "pages", current_site.id.to_s).to_s
"pages/#{current_site.id.to_s}"
end

end
Expand Down
5 changes: 5 additions & 0 deletions app/apps/plugins/front_cache/views/admin/settings.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
</div>
<div class="panel-body">
<div class="alert alert-info"><%= t('plugin.front_cache.message.please_checkpost_need_cache') %></div>
<div class="form-group">
<label>Preserve cache on restart</label><br>
<%= check_box_tag("cache[preserve_cache_on_restart]", 1, @caches[:preserve_cache_on_restart]) %>
</div>

<div class="form-group">
<label><%= t('plugin.front_cache.home_page') %></label><br>
<%= check_box_tag("cache[home]", 1, @caches[:home]) %>
Expand Down