88from random import random
99from textwrap import dedent
1010from time import time
11- from typing import Any
12- from typing import Callable
13- from typing import Literal
14- from typing import cast
15-
16- from django .core .cache .backends .base import DEFAULT_TIMEOUT
17- from django .core .cache .backends .base import BaseCache
18- from django .core .cache .backends .base import default_key_func
19- from django .db import connections
20- from django .db import router
11+ from typing import Any , Callable , Literal , cast
12+
13+ from django .core .cache .backends .base import DEFAULT_TIMEOUT , BaseCache , default_key_func
14+ from django .db import connections , router
2115from django .utils .encoding import force_bytes
2216from django .utils .module_loading import import_string
2317
24- from django_mysql .utils import collapse_spaces
25- from django_mysql .utils import get_list_sql
18+ from django_mysql .utils import collapse_spaces , get_list_sql
2619
2720_EncodedKeyType = Literal ["i" , "p" , "z" ]
2821
@@ -146,7 +139,7 @@ def __init__(self, table: str, params: dict[str, Any]) -> None:
146139 "KEY_PREFIX."
147140 )
148141 else :
149- reverse_key_func = params .get ("REVERSE_KEY_FUNCTION" , None )
142+ reverse_key_func = params .get ("REVERSE_KEY_FUNCTION" )
150143 self .reverse_key_func = get_reverse_key_func (reverse_key_func )
151144
152145 # Django API + helpers
@@ -412,7 +405,7 @@ def _base_delta(
412405 )
413406
414407 if not updated :
415- raise ValueError ("Key '%s ' not found, or not an integer" % key )
408+ raise ValueError (f "Key '{ key } ' not found, or not an integer" )
416409
417410 # New value stored in insert_id
418411 return cursor .lastrowid
@@ -549,11 +542,9 @@ def keys_with_prefix(
549542
550543 with connections [db ].cursor () as cursor :
551544 cursor .execute (
552- """SELECT cache_key FROM {table}
545+ f """SELECT cache_key FROM { table }
553546 WHERE cache_key LIKE %s AND
554- expires >= %s""" .format (
555- table = table
556- ),
547+ expires >= %s""" ,
557548 (prefix , self ._now ()),
558549 )
559550 rows = cursor .fetchall ()
@@ -584,12 +575,10 @@ def get_with_prefix(
584575
585576 with connections [db ].cursor () as cursor :
586577 cursor .execute (
587- """SELECT cache_key, value, value_type
578+ f """SELECT cache_key, value, value_type
588579 FROM { table }
589580 WHERE cache_key LIKE %s AND
590- expires >= %s""" .format (
591- table = table
592- ),
581+ expires >= %s""" ,
593582 (prefix , self ._now ()),
594583 )
595584 rows = cursor .fetchall ()
@@ -612,10 +601,8 @@ def delete_with_prefix(self, prefix: str, version: int | None = None) -> int:
612601
613602 with connections [db ].cursor () as cursor :
614603 return cursor .execute (
615- """DELETE FROM {table}
616- WHERE cache_key LIKE %s""" .format (
617- table = table
618- ),
604+ f"""DELETE FROM { table }
605+ WHERE cache_key LIKE %s""" ,
619606 (prefix ,),
620607 )
621608
@@ -646,19 +633,15 @@ def cull(self) -> int:
646633 else :
647634 cull_num = num // self ._cull_frequency
648635 cursor .execute (
649- """SELECT cache_key FROM {table}
636+ f """SELECT cache_key FROM { table }
650637 ORDER BY cache_key
651- LIMIT 1 OFFSET %s""" .format (
652- table = table
653- ),
638+ LIMIT 1 OFFSET %s""" ,
654639 (cull_num ,),
655640 )
656641 max_key = cursor .fetchone ()[0 ]
657642 num_deleted += cursor .execute (
658- """DELETE FROM {table}
659- WHERE cache_key < %s""" .format (
660- table = table
661- ),
643+ f"""DELETE FROM { table }
644+ WHERE cache_key < %s""" ,
662645 (max_key ,),
663646 )
664647 return num_deleted
0 commit comments