rails-cache.diff

Toshi MARUYAMA, 2011-05-28 10:16

Download (9.87 KB)

View differences:

app/models/setting.rb
85 85
  validates_inclusion_of :name, :in => @@available_settings.keys
86 86
  validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' }
87 87

  
88
  # Hash used to cache setting values
89
  @cached_settings = {}
90
  @cached_cleared_on = Time.now
91

  
92 88
  def value
93 89
    v = read_attribute(:value)
94 90
    # Unserialize serialized settings
......
104 100

  
105 101
  # Returns the value of the setting named name
106 102
  def self.[](name)
107
    v = @cached_settings[name]
108
    v ? v : (@cached_settings[name] = find_or_default(name).value)
103
    Marshal.load(Rails.cache.fetch("chiliproject/setting/#{name}") {
104
        Marshal.dump(find_or_default(name).value)
105
      }).freeze
109 106
  end
110 107

  
111 108
  def self.[]=(name, v)
112 109
    setting = find_or_default(name)
113 110
    setting.value = (v ? v : "")
114
    @cached_settings[name] = nil
111
    Rails.cache.delete "chiliproject/setting/#{name}"
115 112
    setting.save
116
    setting.value
113
    setting.value.freeze
117 114
  end
118 115

  
119 116
  # Defines getter and setter for each setting
......
150 147
  # Called once per request
151 148
  def self.check_cache
152 149
    settings_updated_on = Setting.maximum(:updated_on)
153
    if settings_updated_on && @cached_cleared_on <= settings_updated_on
154
      @cached_settings.clear
155
      @cached_cleared_on = Time.now
156
      logger.info "Settings cache cleared." if logger
150
    cache_cleared_on = Rails.cache.read('chiliproject/setting-cleared_on')
151
    cache_cleared_on = cache_cleared_on ? Marshal.load(cache_cleared_on) : Time.now
152
    if settings_updated_on && cache_cleared_on <= settings_updated_on
153
      Rails.cache.delete_matched( /^chiliproject\/setting\/.+$/ )
154
      Rails.cache.write('chiliproject/setting-cleared_on', Marshal.dump(Time.now))
155
      logger.info 'Settings cache cleared.' if logger
157 156
    end
158 157
  end
159 158

  
test/functional/documents_controller_test.rb
67 67

  
68 68
  def test_new_with_one_attachment
69 69
    ActionMailer::Base.deliveries.clear
70
    Setting.notified_events << 'document_added'
70
    Setting.notified_events = Setting.notified_events.dup << 'document_added'
71 71
    @request.session[:user_id] = 2
72 72
    set_tmp_attachments_directory
73 73

  
test/functional/news_controller_test.rb
67 67
  
68 68
  def test_post_create
69 69
    ActionMailer::Base.deliveries.clear
70
    Setting.notified_events << 'news_added'
70
    Setting.notified_events = Setting.notified_events.dup << 'news_added'
71 71

  
72 72
    @request.session[:user_id] = 2
73 73
    post :create, :project_id => 1, :news => { :title => 'NewsControllerTest',
test/functional/repositories_filesystem_controller_test.rb
35 35
    @request    = ActionController::TestRequest.new
36 36
    @response   = ActionController::TestResponse.new
37 37
    User.current = nil
38
    Setting.enabled_scm << 'Filesystem' unless Setting.enabled_scm.include?('Filesystem')
38
    Setting.enabled_scm = Setting.enabled_scm.dup << 'Filesystem' unless Setting.enabled_scm.include?('Filesystem')
39 39
    @repository = Repository::Filesystem.create(
40 40
                      :project       => Project.find(PRJ_ID),
41 41
                      :url           => REPOSITORY_PATH,
test/unit/comment_test.rb
33 33
  end
34 34
  
35 35
  def test_create_should_send_notification
36
    Setting.notified_events << 'news_comment_added'
36
    Setting.notified_events = Setting.notified_events.dup << 'news_comment_added'
37 37
    Watcher.create!(:watchable => @news, :user => @jsmith)
38 38
    
39 39
    assert_difference 'ActionMailer::Base.deliveries.size' do
test/unit/document_test.rb
27 27

  
28 28
  def test_create_should_send_email_notification
29 29
    ActionMailer::Base.deliveries.clear
30
    Setting.notified_events << 'document_added'
30
    Setting.notified_events = Setting.notified_events.dup << 'document_added'
31 31
    doc = Document.new(:project => Project.find(1), :title => 'New document', :category => Enumeration.find_by_name('User documentation'))
32 32

  
33 33
    assert doc.save
test/unit/news_test.rb
30 30
  
31 31
  def test_create_should_send_email_notification
32 32
    ActionMailer::Base.deliveries.clear
33
    Setting.notified_events << 'news_added'
33
    Setting.notified_events = Setting.notified_events.dup << 'news_added'
34 34
    news = Project.find(:first).news.new(valid_news)
35 35

  
36 36
    assert news.save
test/unit/repository_filesystem_test.rb
25 25

  
26 26
  def setup
27 27
    @project = Project.find(3)
28
    Setting.enabled_scm << 'Filesystem' unless Setting.enabled_scm.include?('Filesystem')
28
    unless Setting.enabled_scm.include?('Filesystem')
29
      Setting.enabled_scm = Setting.enabled_scm.dup << 'Filesystem'
30
    end
29 31
    @repository = Repository::Filesystem.create(
30 32
                               :project => @project,
31 33
                               :url     => REPOSITORY_PATH
app/models/setting.rb
100 100

  
101 101
  # Returns the value of the setting named name
102 102
  def self.[](name)
103
    Marshal.load(Rails.cache.fetch("chiliproject/setting/#{name}") {
103
    Marshal.load(Rails.cache.fetch("redmine/setting/#{name}") {
104 104
        Marshal.dump(find_or_default(name).value)
105 105
      }).freeze
106 106
  end
......
108 108
  def self.[]=(name, v)
109 109
    setting = find_or_default(name)
110 110
    setting.value = (v ? v : "")
111
    Rails.cache.delete "chiliproject/setting/#{name}"
111
    Rails.cache.delete "redmine/setting/#{name}"
112 112
    setting.save
113 113
    setting.value.freeze
114 114
  end
......
147 147
  # Called once per request
148 148
  def self.check_cache
149 149
    settings_updated_on = Setting.maximum(:updated_on)
150
    cache_cleared_on = Rails.cache.read('chiliproject/setting-cleared_on')
150
    cache_cleared_on = Rails.cache.read('redmine/setting-cleared_on')
151 151
    cache_cleared_on = cache_cleared_on ? Marshal.load(cache_cleared_on) : Time.now
152 152
    if settings_updated_on && cache_cleared_on <= settings_updated_on
153
      Rails.cache.delete_matched( /^chiliproject\/setting\/.+$/ )
154
      Rails.cache.write('chiliproject/setting-cleared_on', Marshal.dump(Time.now))
153
      Rails.cache.delete_matched( /^redmine\/setting\/.+$/ )
154
      Rails.cache.write('redmine/setting-cleared_on', Marshal.dump(Time.now))
155 155
      logger.info 'Settings cache cleared.' if logger
156 156
    end
157 157
  end
app/models/setting.rb
150 150
    cache_cleared_on = Rails.cache.read('redmine/setting-cleared_on')
151 151
    cache_cleared_on = cache_cleared_on ? Marshal.load(cache_cleared_on) : Time.now
152 152
    if settings_updated_on && cache_cleared_on <= settings_updated_on
153
      Rails.cache.delete_matched( /^redmine\/setting\/.+$/ )
154
      Rails.cache.write('redmine/setting-cleared_on', Marshal.dump(Time.now))
155
      logger.info 'Settings cache cleared.' if logger
153
      clear_cache
156 154
    end
157 155
  end
158 156

  
157
  # Clears all of the Setting caches
158
  def self.clear_cache
159
    Rails.cache.delete_matched( /^redmine\/setting\/.+$/ )
160
    Rails.cache.write('redmine/setting-cleared_on', Marshal.dump(Time.now))
161
    logger.info 'Settings cache cleared.' if logger
162
  end
163

  
159 164
private
160 165
  # Returns the Setting instance for the setting named name
161 166
  # (record found in database or new record with default value)
test/test_helper.rb
47 47
  self.use_instantiated_fixtures  = false
48 48

  
49 49
  # Add more helper methods to be used by all tests here...
50
  def setup
51
    super
52
    Setting.clear_cache
53
  end
50 54
  
51 55
  def log_user(login, password)
52 56
    User.anonymous