@@ -580,12 +580,14 @@ def lru_cache(maxsize=128, typed=False):
580
580
# Negative maxsize is treated as 0
581
581
if maxsize < 0 :
582
582
maxsize = 0
583
+
583
584
elif callable (maxsize ) and isinstance (typed , bool ):
584
585
# The user_function was passed in directly via the maxsize argument
585
586
user_function , maxsize = maxsize , 128
586
587
wrapper = _lru_cache_wrapper (user_function , maxsize , typed , _CacheInfo )
587
588
wrapper .cache_parameters = lambda : {'maxsize' : maxsize , 'typed' : typed }
588
589
return update_wrapper (wrapper , user_function )
590
+
589
591
elif maxsize is not None :
590
592
raise TypeError (
591
593
'Expected first argument to be an integer, a callable, or None' )
@@ -617,6 +619,7 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
617
619
def wrapper (* args , ** kwds ):
618
620
# No caching -- just a statistics update
619
621
nonlocal misses
622
+
620
623
misses += 1
621
624
result = user_function (* args , ** kwds )
622
625
return result
@@ -626,6 +629,7 @@ def wrapper(*args, **kwds):
626
629
def wrapper (* args , ** kwds ):
627
630
# Simple caching without ordering or size limit
628
631
nonlocal hits , misses
632
+
629
633
key = make_key (args , kwds , typed )
630
634
result = cache_get (key , sentinel )
631
635
if result is not sentinel :
@@ -641,7 +645,9 @@ def wrapper(*args, **kwds):
641
645
def wrapper (* args , ** kwds ):
642
646
# Size limited caching that tracks accesses by recency
643
647
nonlocal root , hits , misses , full
648
+
644
649
key = make_key (args , kwds , typed )
650
+
645
651
with lock :
646
652
link = cache_get (key )
647
653
if link is not None :
@@ -656,19 +662,23 @@ def wrapper(*args, **kwds):
656
662
hits += 1
657
663
return result
658
664
misses += 1
665
+
659
666
result = user_function (* args , ** kwds )
667
+
660
668
with lock :
661
669
if key in cache :
662
670
# Getting here means that this same key was added to the
663
671
# cache while the lock was released. Since the link
664
672
# update is already done, we need only return the
665
673
# computed result and update the count of misses.
666
674
pass
675
+
667
676
elif full :
668
677
# Use the old root to store the new key and result.
669
678
oldroot = root
670
679
oldroot [KEY ] = key
671
680
oldroot [RESULT ] = result
681
+
672
682
# Empty the oldest link and make it the new root.
673
683
# Keep a reference to the old key and old result to
674
684
# prevent their ref counts from going to zero during the
@@ -679,20 +689,25 @@ def wrapper(*args, **kwds):
679
689
oldkey = root [KEY ]
680
690
oldresult = root [RESULT ]
681
691
root [KEY ] = root [RESULT ] = None
692
+
682
693
# Now update the cache dictionary.
683
694
del cache [oldkey ]
695
+
684
696
# Save the potentially reentrant cache[key] assignment
685
697
# for last, after the root and links have been put in
686
698
# a consistent state.
687
699
cache [key ] = oldroot
700
+
688
701
else :
689
702
# Put result in a new link at the front of the queue.
690
703
last = root [PREV ]
691
704
link = [last , root , key , result ]
692
705
last [NEXT ] = root [PREV ] = cache [key ] = link
706
+
693
707
# Use the cache_len bound method instead of the len() function
694
708
# which could potentially be wrapped in an lru_cache itself.
695
709
full = (cache_len () >= maxsize )
710
+
696
711
return result
697
712
698
713
def cache_info ():
@@ -703,6 +718,7 @@ def cache_info():
703
718
def cache_clear ():
704
719
"""Clear the cache and cache statistics"""
705
720
nonlocal hits , misses , full
721
+
706
722
with lock :
707
723
cache .clear ()
708
724
root [:] = [root , root , None , None ]
0 commit comments