Sfoglia il codice sorgente

Do not cache statements that modify data

Andrew Kane 8 anni fa
parent
commit
d45a2f2230

+ 4 - 0
lib/blazer/adapters/base_adapter.rb

@@ -31,6 +31,10 @@ module Blazer
         # optional
       end
 
+      def cachable?(statement)
+        true # optional
+      end
+
       protected
 
       def settings

+ 5 - 1
lib/blazer/adapters/sql_adapter.rb

@@ -40,7 +40,7 @@ module Blazer
       end
 
       def tables
-        result = data_source.run_statement(connection_model.send(:sanitize_sql_array, ["SELECT table_name FROM information_schema.tables WHERE table_schema IN (?) ORDER BY table_name", schemas]))
+        result = data_source.run_statement(connection_model.send(:sanitize_sql_array, ["SELECT table_name FROM information_schema.tables WHERE table_schema IN (?) ORDER BY table_name", schemas]), refresh_cache: true)
         result.rows.map(&:first)
       end
 
@@ -66,6 +66,10 @@ module Blazer
         nil
       end
 
+      def cachable?(statement)
+        !%w[CREATE ALTER UPDATE INSERT DELETE].include?(statement.split.first.to_s.upcase)
+      end
+
       protected
 
       def postgresql?

+ 1 - 1
lib/blazer/data_source.rb

@@ -164,7 +164,7 @@ module Blazer
         cache_data = Marshal.dump([columns, rows, error, cache ? Time.now : nil]) rescue nil
       end
 
-      if cache && cache_data
+      if cache && cache_data && @adapter_instance.cachable?(statement)
         Blazer.cache.write(statement_cache_key(statement), cache_data, expires_in: cache_expires_in.to_f * 60)
       end