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