lib/enrichment_db/db.rb in enrichment_db-0.1.10 vs lib/enrichment_db/db.rb in enrichment_db-0.1.11

- old
+ new

@@ -1,6 +1,7 @@ require 'pg' +require 'cache' module EnrichmentDb def self.db_connection @__db_connection ||= PGconn.connect( @@ -38,19 +39,27 @@ def self.request(db, query, values = nil) type = request_type(query) case type when :select, 'select', '\d' - result = if values - EnrichmentDb::db_connection.exec(query, values) - else - EnrichmentDb::db_connection.exec(query) - end + get_or_request(query, values) when :insert, :drop, :delete, 'insert', 'drop', 'delete' fail EnrichmentDb::RequestError, "Postgres method '#{type}' is unsupported" else fail EnrichmentDb::RequestError, "Postgres query '#{query}' is unsupported" end + end + + def self.get_or_request(query, values = nil) + result = EnrichmentDb::Cache.get(query, values) + return result unless result.nil? + + result = if values + EnrichmentDb::db_connection.exec(query, values) + else + EnrichmentDb::db_connection.exec(query) + end + EnrichmentDb::Cache.set(query, values, result) end def self.request_type(query) result = query.downcase.match(/(^(?:drop|select|insert|delete|\\d))/) return unless result