]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Add a rotate() method to PrepareManager
authorDenis Laxalde <denis.laxalde@dalibo.com>
Mon, 18 Oct 2021 09:31:33 +0000 (11:31 +0200)
committerDenis Laxalde <denis.laxalde@dalibo.com>
Mon, 29 Nov 2021 08:50:50 +0000 (09:50 +0100)
psycopg/psycopg/_preparing.py

index 63b84e62af6936b4e828a2e00f35f301e7ca9c43..3a96ce38690bce241e4e46f668b2f61e4668e5d4 100644 (file)
@@ -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: