|
12 | 12 |
|
13 | 13 | from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache, default_key_func
|
14 | 14 | from django.db import connections, router
|
| 15 | +from django.db.models import Model |
15 | 16 | from django.utils.encoding import force_bytes
|
16 | 17 | from django.utils.module_loading import import_string
|
17 | 18 |
|
@@ -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
|
@@ -189,7 +192,7 @@ def get_many(
|
189 | 192 | self, keys: Iterable[str], version: int | None = None
|
190 | 193 | ) -> dict[str, Any]:
|
191 | 194 | made_key_to_key = {self.make_key(key, version=version): key for key in keys}
|
192 |
| - made_keys = list(made_key_to_key.keys()) |
| 195 | + made_keys: list[Any] = list(made_key_to_key.keys()) |
193 | 196 | for key in made_keys:
|
194 | 197 | self.validate_key(key)
|
195 | 198 |
|
@@ -273,7 +276,7 @@ def _base_set(
|
273 | 276 | return True
|
274 | 277 | else: # mode = 'add'
|
275 | 278 | # Use a special code in the add query for "did insert"
|
276 |
| - insert_id = cursor.lastrowid |
| 279 | + insert_id: int = cursor.lastrowid |
277 | 280 | return insert_id != 444
|
278 | 281 |
|
279 | 282 | _set_many_query = collapse_spaces(
|
@@ -423,7 +426,8 @@ def _base_delta(
|
423 | 426 | raise ValueError("Key '%s' not found, or not an integer" % key)
|
424 | 427 |
|
425 | 428 | # New value stored in insert_id
|
426 |
| - return cursor.lastrowid |
| 429 | + result: int = cursor.lastrowid |
| 430 | + return result |
427 | 431 |
|
428 | 432 | # Looks a bit tangled to turn the blob back into an int for updating, but
|
429 | 433 | # it works. Stores the new value for insert_id() with LAST_INSERT_ID
|
@@ -455,7 +459,7 @@ def touch(
|
455 | 459 | db = router.db_for_write(self.cache_model_class)
|
456 | 460 | table = connections[db].ops.quote_name(self._table)
|
457 | 461 | with connections[db].cursor() as cursor:
|
458 |
| - affected_rows = cursor.execute( |
| 462 | + affected_rows: int = cursor.execute( |
459 | 463 | self._touch_query.format(table=table), [exp, key, self._now()]
|
460 | 464 | )
|
461 | 465 | return affected_rows > 0
|
@@ -619,18 +623,20 @@ def delete_with_prefix(self, prefix: str, version: int | None = None) -> int:
|
619 | 623 | prefix = self.make_key(prefix + "%", version=version)
|
620 | 624 |
|
621 | 625 | with connections[db].cursor() as cursor:
|
622 |
| - return cursor.execute( |
| 626 | + result: int = cursor.execute( |
623 | 627 | """DELETE FROM {table}
|
624 | 628 | WHERE cache_key LIKE %s""".format(
|
625 | 629 | table=table
|
626 | 630 | ),
|
627 | 631 | (prefix,),
|
628 | 632 | )
|
| 633 | + return result |
629 | 634 |
|
630 | 635 | def cull(self) -> int:
|
631 | 636 | db = router.db_for_write(self.cache_model_class)
|
632 | 637 | table = connections[db].ops.quote_name(self._table)
|
633 | 638 |
|
| 639 | + num_deleted: int |
634 | 640 | with connections[db].cursor() as cursor:
|
635 | 641 | # First, try just deleting expired keys
|
636 | 642 | num_deleted = cursor.execute(
|
|
0 commit comments