perf: Cache get_tables values

- To avoid redundant db calls
This commit is contained in:
Suraj Shetty 2020-04-29 12:28:08 +05:30
parent d850a8ee4b
commit 031aad0d85
2 changed files with 11 additions and 1 deletions

View file

@ -769,7 +769,16 @@ class Database(object):
return ("tab" + doctype) in self.get_tables()
def get_tables(self):
return [d[0] for d in self.sql("select table_name from information_schema.tables where table_schema not in ('pg_catalog', 'information_schema')")]
tables = frappe.cache().get_value('db_tables')
if not tables:
table_rows = self.sql("""
SELECT table_name
FROM information_schema.tables
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
""")
tables = set([d[0] for d in table_rows])
frappe.cache().set_value('db_tables', tables)
return tables
def a_row_exists(self, doctype):
"""Returns True if atleast one row exists."""

View file

@ -31,6 +31,7 @@ class DBTable:
def sync(self):
if self.is_new():
frappe.cache().delete_key('db_tables')
self.create()
else:
frappe.cache().hdel('table_columns', self.table_name)