Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5431

Deprecate criteria/contextual caching

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.5.0
    • Affects Version/s: None
    • Component/s: None
    • None
    • Fully Compatible

      This ticket has now been changed to deprecate the use of the criteria cache. The Mongoid::QueryCache is more effective in caching queries and all caching functionality should be delegated there.

      Original ticket:
      ===========

      Mongo context has this usage of try_cache for example:

            def count(options = {}, &block)
              return super(&block) if block_given?
              try_cache(:count) { view.count_documents(options) }
            end
      

      This seems to imply that the options to count aren't part of the cache key. However count can be limited, suggesting that if I call count with limit 1 and then count with no limit I may receive wrong result (count=1 instead of actual count).

      To reproduce this, criteria must be explicitly cached:

      irb(main):026:0> c = Band.all
      => 
      #<Mongoid::Criteria
      ...
      irb(main):027:0> cc = c.cache
      => 
      #<Mongoid::Criteria
      ...
      irb(main):028:0> cc.count(limit: 4)
      => 4
      irb(main):029:0> cc.count(limit: 5)
      => 4
      

      To fix this, try_cache calls must pass in options applicable to the query in each invocation, for example limit in the case of count above.

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: