Skip to content

Commit

Permalink
Simple insert cache
Browse files Browse the repository at this point in the history
  • Loading branch information
grigi committed Oct 4, 2018
1 parent 02d2096 commit 4ed63c2
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions tortoise/backends/base/executor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from typing import Callable, Dict, List, Type # noqa
from typing import Callable, Dict, List, Tuple, Type # noqa

from pypika import Table

from tortoise import fields
from tortoise.exceptions import OperationalError

INSERT_CACHE = {} # type: Dict[str, Tuple[list, list, str]]


class BaseExecutor:
TO_DB_OVERRIDE = {} # type: Dict[Type[fields.Field], Callable]
Expand Down Expand Up @@ -58,9 +60,13 @@ def _prepare_insert_statement(self, columns: List[str]) -> str:

async def execute_insert(self, instance):
self.connection = await self.db.get_single_connection()

regular_columns, columns = self._prepare_insert_columns()
query = self._prepare_insert_statement(columns)
key = '%s:%s' % (self.db.connection_name, self.model._meta.table)
if key not in INSERT_CACHE:
regular_columns, columns = self._prepare_insert_columns()
query = self._prepare_insert_statement(columns)
INSERT_CACHE[key] = regular_columns, columns, query
else:
regular_columns, columns, query = INSERT_CACHE[key]

values = self._prepare_insert_values(
instance=instance,
Expand Down

0 comments on commit 4ed63c2

Please sign in to comment.