mongoid-app_settings icon indicating copy to clipboard operation
mongoid-app_settings copied to clipboard

Turning caching off

Open paulewog opened this issue 9 years ago • 8 comments

Is there any plan for an option to turn caching off, by any chance? :)

paulewog avatar Dec 11 '15 18:12 paulewog

(I don't think this is an "Issue" but I couldn't figure out where else to ask it... github newbie as far as commenting/questioning goes...)

paulewog avatar Dec 11 '15 18:12 paulewog

Алё, сыны, как дела?

makshark avatar Jan 28 '16 16:01 makshark

Sorry, I completely missed this in my notification triage after holidays. I guess there could be such an option, but you should be careful what you wish for: if you refer to settings from multiple places in your request/response cycle, you'll get multiple calls to MongoDB. This could quickly cause performance issues.

If you'd still like this, let me know and I can add that and push a release.

marten avatar Jan 28 '16 16:01 marten

could you reset cash, when user update field?

makshark avatar Jan 28 '16 16:01 makshark

@makshark Yes, you can reset the cache by calling .reload on the settings class you've defined. If you're using Rails, I recommend calling reload in a before_action so that each request starts with the latest settings loaded from MongoDB. There is an example in the README for this.

marten avatar Jan 28 '16 17:01 marten

Hey guys, I didn't see any of these notifications, ha.

I did use the reload option in controllers; however, we also read these settings from non-controller parts of the app. What I actually did was ... what is the phrase ... monkey patch? ... and added a get() method (or re-defined it) which reloads and then returns the requested setting. It works. :)

module Mongoid
  module AppSettings
    module ClassMethods
      def get setting
        self.reload
        if self.instance_values["settings"].include? setting then
          self.send(setting)
        else
          raise "SettingDoesNotExist"
        end
      end
    end
  end
end

paulewog avatar Mar 03 '16 23:03 paulewog

... but resetting the cache when it is updated, rather than always reloading when getting a value, seems like a better idea.

paulewog avatar Mar 03 '16 23:03 paulewog

Under the context of distributed systems, one might want to always reload settings, or at least have a cache expiration

Startouf avatar Jan 11 '17 09:01 Startouf