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

the problem about Query::Cache Middleware

    • Type: Icon: Task Task
    • Resolution: Done
    • 4.0.0 final
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      When I upgraded Mongoid from 3.1.4 to 3.1.6。I use Query::Cache Middleware to cache my query。 But I find that the cache has same problem.

      For examle:

      def index
        @users = UserMongo.all
      end
      

      At the other place(maybe index view), I will use @users

      if @users.present?       #----> flag_A
         ...other operations
         @users.each do |p|    #-----> flag_B
             ....
         end
      end
      

      When the program go to 'flag_B', then it will raise one execption:

      QUERY CACHE  database=tianji collection=users selector={}
      TypeError: no implicit conversion of String into Integer
      from /home/andy/Documents/mongoid/lib/mongoid/factory.rb:40:in `[]'
      

      and the source about cache as follow:

      module Mongoid
        module QueryCache
          module Cacheable
            def with_cache
              return yield unless QueryCache.enabled?
              return yield if system_collection?
              key = cache_key
              if QueryCache.cache_table.has_key?(key)
                instrument(key) { QueryCache.cache_table[key] }
              else
                value = yield
                QueryCache.cache_table[key] = value
              end
            end
        end
      end
      
      def cache_key
          [ operation.database, operation.collection, operation.selector ]
      end
      

      after debug the source code, I find that at 'flag_A', @users.present?, QueryCache.cache_table[key] has be set:

      puts QueryCache.cache_table -->
      {["tianji", "users", {}]=>{"_id"=>158}}

      So when go to flag_B, It will raise error!!!

      My Point

      I think that it is cache key is not clear to flag one query.

      So guys, do you match same problem?

            Assignee:
            Unassigned Unassigned
            Reporter:
            easyhappy easyhappy
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: