|
19 | 19 | from django.core.cache.backends.base import DEFAULT_TIMEOUT
|
20 | 20 | from django.db import connections
|
21 | 21 | from django.db import router
|
| 22 | +from django.db.models import Model |
22 | 23 | from django.utils.encoding import force_bytes
|
23 | 24 | from django.utils.module_loading import import_string
|
24 | 25 |
|
@@ -69,7 +70,9 @@ def __init__(self, table: str, params: dict[str, Any]) -> None:
|
69 | 70 | super().__init__(params)
|
70 | 71 | self._table = table
|
71 | 72 |
|
72 |
| - class CacheEntry: |
| 73 | + CacheEntry: type[Model] # force Mypy to accept duck typing |
| 74 | + |
| 75 | + class CacheEntry: # type: ignore [no-redef] |
73 | 76 | _meta = Options(table)
|
74 | 77 |
|
75 | 78 | self.cache_model_class = CacheEntry
|
@@ -197,7 +200,7 @@ def get_many(
|
197 | 200 | self, keys: Iterable[str], version: int | None = None
|
198 | 201 | ) -> dict[str, Any]:
|
199 | 202 | made_key_to_key = {self.make_key(key, version=version): key for key in keys}
|
200 |
| - made_keys = list(made_key_to_key.keys()) |
| 203 | + made_keys: list[Any] = list(made_key_to_key.keys()) |
201 | 204 | for key in made_keys:
|
202 | 205 | self.validate_key(key)
|
203 | 206 |
|
@@ -281,7 +284,7 @@ def _base_set(
|
281 | 284 | return True
|
282 | 285 | else: # mode = 'add'
|
283 | 286 | # Use a special code in the add query for "did insert"
|
284 |
| - insert_id = cursor.lastrowid |
| 287 | + insert_id: int = cursor.lastrowid |
285 | 288 | return insert_id != 444
|
286 | 289 |
|
287 | 290 | _set_many_query = collapse_spaces(
|
@@ -431,7 +434,8 @@ def _base_delta(
|
431 | 434 | raise ValueError("Key '%s' not found, or not an integer" % key)
|
432 | 435 |
|
433 | 436 | # New value stored in insert_id
|
434 |
| - return cursor.lastrowid |
| 437 | + result: int = cursor.lastrowid |
| 438 | + return result |
435 | 439 |
|
436 | 440 | # Looks a bit tangled to turn the blob back into an int for updating, but
|
437 | 441 | # it works. Stores the new value for insert_id() with LAST_INSERT_ID
|
@@ -463,7 +467,7 @@ def touch(
|
463 | 467 | db = router.db_for_write(self.cache_model_class)
|
464 | 468 | table = connections[db].ops.quote_name(self._table)
|
465 | 469 | with connections[db].cursor() as cursor:
|
466 |
| - affected_rows = cursor.execute( |
| 470 | + affected_rows: int = cursor.execute( |
467 | 471 | self._touch_query.format(table=table), [exp, key, self._now()]
|
468 | 472 | )
|
469 | 473 | return affected_rows > 0
|
@@ -627,18 +631,20 @@ def delete_with_prefix(self, prefix: str, version: int | None = None) -> int:
|
627 | 631 | prefix = self.make_key(prefix + "%", version=version)
|
628 | 632 |
|
629 | 633 | with connections[db].cursor() as cursor:
|
630 |
| - return cursor.execute( |
| 634 | + result: int = cursor.execute( |
631 | 635 | """DELETE FROM {table}
|
632 | 636 | WHERE cache_key LIKE %s""".format(
|
633 | 637 | table=table
|
634 | 638 | ),
|
635 | 639 | (prefix,),
|
636 | 640 | )
|
| 641 | + return result |
637 | 642 |
|
638 | 643 | def cull(self) -> int:
|
639 | 644 | db = router.db_for_write(self.cache_model_class)
|
640 | 645 | table = connections[db].ops.quote_name(self._table)
|
641 | 646 |
|
| 647 | + num_deleted: int |
642 | 648 | with connections[db].cursor() as cursor:
|
643 | 649 | # First, try just deleting expired keys
|
644 | 650 | num_deleted = cursor.execute(
|
|
0 commit comments