|
18 | 18 | from django.core.cache.backends.base import default_key_func
|
19 | 19 | from django.db import connections
|
20 | 20 | from django.db import router
|
| 21 | +from django.db.models import Model |
21 | 22 | from django.utils.encoding import force_bytes
|
22 | 23 | from django.utils.module_loading import import_string
|
23 | 24 |
|
@@ -61,7 +62,9 @@ def __init__(self, table: str, params: dict[str, Any]) -> None:
|
61 | 62 | super().__init__(params)
|
62 | 63 | self._table = table
|
63 | 64 |
|
64 |
| - class CacheEntry: |
| 65 | + CacheEntry: type[Model] # force Mypy to accept duck typing |
| 66 | + |
| 67 | + class CacheEntry: # type: ignore [no-redef] |
65 | 68 | _meta = Options(table)
|
66 | 69 |
|
67 | 70 | self.cache_model_class = CacheEntry
|
@@ -182,7 +185,7 @@ def get_many(
|
182 | 185 | self, keys: Iterable[str], version: int | None = None
|
183 | 186 | ) -> dict[str, Any]:
|
184 | 187 | made_key_to_key = {self.make_key(key, version=version): key for key in keys}
|
185 |
| - made_keys = list(made_key_to_key.keys()) |
| 188 | + made_keys: list[Any] = list(made_key_to_key.keys()) |
186 | 189 | for key in made_keys:
|
187 | 190 | self.validate_key(key)
|
188 | 191 |
|
@@ -265,7 +268,7 @@ def _base_set(
|
265 | 268 | return True
|
266 | 269 | else: # mode = 'add'
|
267 | 270 | # Use a special code in the add query for "did insert"
|
268 |
| - insert_id = cursor.lastrowid |
| 271 | + insert_id: int = cursor.lastrowid |
269 | 272 | return insert_id != 444
|
270 | 273 |
|
271 | 274 | _set_many_query = collapse_spaces(
|
@@ -415,7 +418,8 @@ def _base_delta(
|
415 | 418 | raise ValueError("Key '%s' not found, or not an integer" % key)
|
416 | 419 |
|
417 | 420 | # New value stored in insert_id
|
418 |
| - return cursor.lastrowid |
| 421 | + result: int = cursor.lastrowid |
| 422 | + return result |
419 | 423 |
|
420 | 424 | # Looks a bit tangled to turn the blob back into an int for updating, but
|
421 | 425 | # it works. Stores the new value for insert_id() with LAST_INSERT_ID
|
@@ -447,7 +451,7 @@ def touch(
|
447 | 451 | db = router.db_for_write(self.cache_model_class)
|
448 | 452 | table = connections[db].ops.quote_name(self._table)
|
449 | 453 | with connections[db].cursor() as cursor:
|
450 |
| - affected_rows = cursor.execute( |
| 454 | + affected_rows: int = cursor.execute( |
451 | 455 | self._touch_query.format(table=table), [exp, key, self._now()]
|
452 | 456 | )
|
453 | 457 | return affected_rows > 0
|
@@ -611,18 +615,20 @@ def delete_with_prefix(self, prefix: str, version: int | None = None) -> int:
|
611 | 615 | prefix = self.make_key(prefix + "%", version=version)
|
612 | 616 |
|
613 | 617 | with connections[db].cursor() as cursor:
|
614 |
| - return cursor.execute( |
| 618 | + result: int = cursor.execute( |
615 | 619 | """DELETE FROM {table}
|
616 | 620 | WHERE cache_key LIKE %s""".format(
|
617 | 621 | table=table
|
618 | 622 | ),
|
619 | 623 | (prefix,),
|
620 | 624 | )
|
| 625 | + return result |
621 | 626 |
|
622 | 627 | def cull(self) -> int:
|
623 | 628 | db = router.db_for_write(self.cache_model_class)
|
624 | 629 | table = connections[db].ops.quote_name(self._table)
|
625 | 630 |
|
| 631 | + num_deleted: int |
626 | 632 | with connections[db].cursor() as cursor:
|
627 | 633 | # First, try just deleting expired keys
|
628 | 634 | num_deleted = cursor.execute(
|
|
0 commit comments