From bc1db16351e98afc772905c4cf4e88b02dc812f0 Mon Sep 17 00:00:00 2001 From: Denis Laxalde Date: Mon, 18 Oct 2021 11:31:33 +0200 Subject: [PATCH] Add a rotate() method to PrepareManager --- psycopg/psycopg/_preparing.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/psycopg/psycopg/_preparing.py b/psycopg/psycopg/_preparing.py index 63b84e62a..3a96ce386 100644 --- a/psycopg/psycopg/_preparing.py +++ b/psycopg/psycopg/_preparing.py @@ -127,6 +127,21 @@ class PrepareManager: return True + def rotate(self) -> Optional[bytes]: + """Evict an old value from the cache. + + If it was prepared, deallocate it. Do it only once: if the cache was + resized, deallocate gradually. + """ + if len(self._prepared) <= self.prepared_max: + return None + + old_val = self._prepared.popitem(last=False)[1] + if isinstance(old_val, bytes): + return b"DEALLOCATE " + old_val + else: + return None + def maintain( self, query: PostgresQuery, @@ -155,16 +170,7 @@ class PrepareManager: key, value = cached self._prepared[key] = value - # Evict an old value from the cache; if it was prepared, deallocate it - # Do it only once: if the cache was resized, deallocate gradually - if len(self._prepared) <= self.prepared_max: - return None - - old_val = self._prepared.popitem(last=False)[1] - if isinstance(old_val, bytes): - return b"DEALLOCATE " + old_val - else: - return None + return self.rotate() def clear(self) -> Optional[bytes]: if self._prepared_idx: -- 2.47.2