Module: Plugins::FrontCache::FrontCacheHelper

Included in:
AdminController
Defined in:
app/apps/plugins/front_cache/front_cache_helper.rb

Instance Method Summary collapse

Instance Method Details

#front_cache_before_loadObject

expire cache for a page after comment registered or updated



97
98
# File 'app/apps/plugins/front_cache/front_cache_helper.rb', line 97

def front_cache_before_load
end

#front_cache_cleanObject

clear all frontend cache files



113
114
115
# File 'app/apps/plugins/front_cache/front_cache_helper.rb', line 113

def front_cache_clean
  FileUtils.rm_rf(Rails.root.join("tmp", "cache", "pages", current_site.id.to_s)) # clear site pages cache
end

#front_cache_front_after_loadObject



38
39
40
41
42
43
44
45
46
# File 'app/apps/plugins/front_cache/front_cache_helper.rb', line 38

def front_cache_front_after_load
  cache_key = front_cache_get_key
  if @_plugin_do_cache && !flash.keys.present?
    front_cache_create(cache_key, response.body
                   .gsub(/csrf-token" content="(.*?)"/, 'csrf-token" content="{{form_authenticity_token}}"')
                   .gsub(/name="authenticity_token" value="(.*?)"/, 'name="authenticity_token" value="{{form_authenticity_token}}"'))
    Rails.logger.info "============================================== cache saved as: #{cache_key}"
  end
end

#front_cache_front_before_loadObject

save as cache all pages configured on settings of this plugin for public users



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/apps/plugins/front_cache/front_cache_helper.rb', line 4

def front_cache_front_before_load
  if current_site.get_option("refresh_cache") # clear cache every restart server
    front_cache_clean
    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
  # return if signin? # avoid cache if current visitor is logged in or development environment

  cache_key = front_cache_get_key
  if !flash.keys.present? && front_cache_exist?(cache_key) # recover cache file
    Rails.logger.info "============================================== readed cache: #{cache_key}"
    render text: front_cache_get(cache_key).gsub("{{form_authenticity_token}}", form_authenticity_token)
    return
  end

  @caches = current_site.get_meta("front_cache_elements")
  @_plugin_do_cache = false
  if match_path_patterns? || (params[:action] == "index" && @caches[:home].present?) # cache paths and home page
    @_plugin_do_cache = true
  elsif params[:action] == "post" && !params[:draft_id].present?
    begin
      post = current_site.the_posts.find_by_slug(params[:slug]).decorate
      if post.can_visit?
        @post = post
        @post_type = post.the_post_type
        @_plugin_do_cache = true if can_cache_page?
      end
    rescue # skip post not found
    end
  end
end

#front_cache_on_active(plugin) ⇒ Object

on install plugin



49
50
51
52
53
54
55
56
# File 'app/apps/plugins/front_cache/front_cache_helper.rb', line 49

def front_cache_on_active(plugin)
  current_site.set_meta("front_cache_elements", {paths: [],
      posts: [],
      post_types: [current_site.post_types.where(slug: "page").first.id],
      skip_posts: [],
      home: true,
      cache_login: true}) unless current_site.get_meta("front_cache_elements", nil).present?
end

#front_cache_on_inactive(plugin) ⇒ Object

on uninstall plugin



59
60
61
# File 'app/apps/plugins/front_cache/front_cache_helper.rb', line 59

def front_cache_on_inactive(plugin)
  # current_site.delete_meta("front_cache_elements")
end

#front_cache_on_render(args) ⇒ Object

cache actions (for logged users)



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/apps/plugins/front_cache/front_cache_helper.rb', line 64

def front_cache_on_render(args)
  return nil
  return if args[:options].include?(:skip_cache_action) || !signin? # avoid recursive calling
  if params[:controller] == "frontend"
    do_cache = false
    @caches = current_site.get_meta("front_cache_elements")
    return unless @caches[:cache_login]
    if @caches[:paths].include?(front_request_key) || (params[:action] == "index" && @caches[:home].present?) # cache paths and home page
      do_cache = true
    elsif params[:action] == "post" && !params[:draft_id].present?
      do_cache = true if can_cache_page?
    end

    cache_key = front_cache_get_key("___")
    if do_cache # save or recovery cache
      if args[:context].controller.page_cache_exist?(cache_key) # recover cache file
        args[:options][:skip_cache_action] = true
        args[:options][:text] = File.read(args[:context].controller.page_cache_get(cache_key))
        args[:options].delete(:file)
        return
      end
      Thread.abort_on_exception=true
      Thread.new do
        options = args[:options].dup
        options[:layout] = false
        options[:skip_cache_action] = true
        args[:context].controller.cache_page(args[:context].controller.render_to_string(options), cache_key, false)
      end
    end
  end
end

#front_cache_plugin_options(arg) ⇒ Object



100
101
102
103
# File 'app/apps/plugins/front_cache/front_cache_helper.rb', line 100

def front_cache_plugin_options(arg)
  arg[:links] << link_to(t('plugin.front_cache.settings'), admin_plugins_front_cache_settings_path)
  arg[:links] << link_to(t('plugin.front_cache.clean_cache'), admin_plugins_front_cache_clean_path)
end

#front_cache_post_requestsObject

save as cache all post requests



106
107
108
109
110
# File 'app/apps/plugins/front_cache/front_cache_helper.rb', line 106

def front_cache_post_requests
  if (request.post? || request.patch?)
    front_cache_clean()
  end
end