]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] Use `time.monotonic` in OrderedDict LRU cache example (GH-150986) (#150992)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 5 Jun 2026 22:59:42 +0000 (00:59 +0200)
committerGitHub <noreply@github.com>
Fri, 5 Jun 2026 22:59:42 +0000 (01:59 +0300)
Use `time.monotonic` in OrderedDict LRU cache example (GH-150986)
(cherry picked from commit ea4c85552bb7883e1d6c808281c1f46aca86aeab)

Co-authored-by: Ilya Nikolaev <65247719+ilya-nikolaev@users.noreply.github.com>
Doc/library/collections.rst

index c37cb6ba78667272f59025fb0af849bc47c2f8c0..cfae81040624181ef04921e3ba0e83b6af2d1fe3 100644 (file)
@@ -1229,7 +1229,7 @@ variants of :func:`functools.lru_cache`:
 .. testcode::
 
     from collections import OrderedDict
-    from time import time
+    from time import monotonic
 
     class TimeBoundedLRU:
         "LRU Cache that invalidates and refreshes old entries."
@@ -1244,10 +1244,10 @@ variants of :func:`functools.lru_cache`:
             if args in self.cache:
                 self.cache.move_to_end(args)
                 timestamp, result = self.cache[args]
-                if time() - timestamp <= self.maxage:
+                if monotonic() - timestamp <= self.maxage:
                     return result
             result = self.func(*args)
-            self.cache[args] = time(), result
+            self.cache[args] = monotonic(), result
             if len(self.cache) > self.maxsize:
                 self.cache.popitem(last=False)
             return result