From: Denis Laxalde Date: Mon, 18 Oct 2021 09:31:33 +0000 (+0200) Subject: Add a rotate() method to PrepareManager X-Git-Tag: pool-3.1~98^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bc1db16351e98afc772905c4cf4e88b02dc812f0;p=thirdparty%2Fpsycopg.git Add a rotate() method to PrepareManager --- 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: