cache.patch

Massimo Zaniboni, 2010-12-31 03:47

Download (4.82 KB)

View differences:

app/controllers/application_controller.rb
37 37
  end
38 38
  
39 39
  before_filter :user_setup, :check_if_login_required, :set_localization
40

  
41
  before_filter :maybe_return_cached_page
42

  
43
  # return 0 if the page is not cacheable, 
44
  # the cache refresh rate otherwise
45
  def page_cache_refresh
46
    if User.current.logged?
47
      return 0
48
    else
49
      cache_refresh = Setting.cache_refresh_rate_in_minutes.to_i
50
      if cache_refresh.nil?
51
        return 0
52
      else
53
        return cache_refresh.to_i
54
      end
55
    end
56
  end
57
 
58
  # return the two keys to use for retrieving in the cache 
59
  # the page content and page timestamp.
60
  def request_cache_key
61
    l = "page__" + I18n.locale.to_s + "__"
62
    return l  + request.path, l + "t__" + request.path
63
  end
64
  
65
  # return the cached page
66
  def maybe_return_cached_page 
67
    cache_refresh = page_cache_refresh
68
    if cache_refresh > 0
69
      cache = ActionController::Base.cache_store
70
      key, key_t = request_cache_key
71
      cache_timestamp = cache.read(key_t)
72
      if ! cache_timestamp.nil?
73
        now_timestamp = Time.now.to_i
74
        if (now_timestamp - cache_timestamp) < (cache_refresh * 60)
75
          logger.info('Return cached '  + key + ' with time stamp ' + cache_timestamp.to_s() + ".")
76
          t = cache.read(key)
77
          if ! t.nil?
78
            render :text => t
79
          end
80
        end
81
      end
82
    end
83
  end
84

  
85
  after_filter :maybe_write_page_inside_cache
86

  
87
  # cache the page
88
  def maybe_write_page_inside_cache
89
    cache_refresh = page_cache_refresh
90
    if cache_refresh > 0
91
      c = response.body
92
      if page_can_be_cached(c)
93
        cache = ActionController::Base.cache_store      
94
        key, key_t = request_cache_key
95
        cache.write(key, c)
96
        cache.write(key_t, Time.now.to_i)
97
        logger.info('Wrote ' + key + " in the cache, using " + cache.class.name + " cache method.")
98
      end
99
    end
100
  end
101

  
102
  # return false if it can not be cached. 
103
  # For example forms with authenticity_token can not be cached
104
  # because they must be uniquely generated for each request. 
105
  def page_can_be_cached(c)
106
    i = c.index('<form')    
107
    if ! i.nil?
108
      i = c.index('<input name="authenticity_token"', i)
109
      if ! i.nil?
110
        return false
111
      end
112
    end
113
    return true
114
  end
115

  
116

  
40 117
  filter_parameter_logging :password
41 118
  protect_from_forgery
42 119
  
app/views/settings/_general.rhtml
22 22

  
23 23
<p><%= setting_check_box :cache_formatted_text %></p>
24 24

  
25
<p><%= setting_text_field :cache_refresh_rate_in_minutes, :size => 3 %> <%= l(:label_minute_plural) %><br/>
26
<em><%= l(:text_cache_refresh_rate_in_minutes) %></em></p>
27

  
25 28
<p><%= setting_select :wiki_compression, [['Gzip', 'gzip']], :blank => :label_none %></p>
26 29

  
27 30
<p><%= setting_text_field :feeds_limit, :size => 6 %></p>
config/locales/en.yml
362 362
  setting_commit_logtime_enabled: Enable time logging
363 363
  setting_commit_logtime_activity_id: Activity for logged time
364 364
  setting_gantt_items_limit: Maximum number of items displayed on the gantt chart
365
  setting_cache_refresh_rate_in_minutes: Refresh cached pages after
365 366
  
366 367
  permission_add_project: Create project
367 368
  permission_add_subprojects: Create subprojects
......
793 794
  label_project_copy_notifications: Send email notifications during the project copy
794 795
  label_principal_search: "Search for user or group:"
795 796
  label_user_search: "Search for user:"
797
  label_minute_plural: "minutes"
796 798
  
797 799
  button_login: Login
798 800
  button_submit: Submit
......
908 910
  text_own_membership_delete_confirmation: "You are about to remove some or all of your permissions and may no longer be able to edit this project after that.\nAre you sure you want to continue?"
909 911
  text_zoom_in: Zoom in
910 912
  text_zoom_out: Zoom out
913
  text_cache_refresh_rate_in_minutes: "Enable caching of pages, only for anonymous users. 0 for no caching. It uses the cache method specified in environment configuration." 
911 914
  
912 915
  default_role_manager: Manager
913 916
  default_role_developer: Developer
config/settings.yml
196 196
  default: 'only_my_events'
197 197
emails_header:
198 198
  default: ''
199
cache_refresh_rate_in_minutes:
200
  format: int
201
  default: 0