42
42
43
43
44
44
def wants_args (f ):
45
- """Check if the function wants any arguments
46
- """
45
+ """Check if the function wants any arguments"""
47
46
48
47
argspec = inspect .getfullargspec (f )
49
48
@@ -299,7 +298,7 @@ def cached(
299
298
hash_method = hashlib .md5 ,
300
299
cache_none = False ,
301
300
make_cache_key = None ,
302
- source_check = None
301
+ source_check = None ,
303
302
):
304
303
"""Decorator. Use this to cache a function. By default the cache key
305
304
is `view/request.path`. You are able to use this decorator with any
@@ -417,8 +416,9 @@ def decorated_function(*args, **kwargs):
417
416
if make_cache_key is not None and callable (make_cache_key ):
418
417
cache_key = make_cache_key (* args , ** kwargs )
419
418
else :
420
- cache_key = _make_cache_key (args , kwargs , use_request = True )
421
-
419
+ cache_key = _make_cache_key (
420
+ args , kwargs , use_request = True
421
+ )
422
422
423
423
if (
424
424
callable (forced_update )
@@ -535,14 +535,17 @@ def _make_cache_key(args, kwargs, use_request):
535
535
if use_request :
536
536
cache_key = key_prefix % request .path
537
537
else :
538
- cache_key = key_prefix % url_for (f .__name__ , ** kwargs )
538
+ cache_key = key_prefix % url_for (
539
+ f .__name__ , ** kwargs
540
+ )
539
541
else :
540
542
cache_key = key_prefix
541
543
542
544
if source_check and callable (f ):
543
545
func_source_code = inspect .getsource (f )
544
546
func_source_hash = hash_method (
545
- func_source_code .encode ("utf-8" ))
547
+ func_source_code .encode ("utf-8" )
548
+ )
546
549
func_source_hash = str (func_source_hash .hexdigest ())
547
550
548
551
cache_key += func_source_hash
@@ -572,6 +575,7 @@ def _memoize_version(
572
575
delete = False ,
573
576
timeout = None ,
574
577
forced_update = False ,
578
+ args_to_ignore = None ,
575
579
):
576
580
"""Updates the hash version associated with a memoized function or
577
581
method.
@@ -580,6 +584,10 @@ def _memoize_version(
580
584
version_key = self ._memvname (fname )
581
585
fetch_keys = [version_key ]
582
586
587
+ args_to_ignore = args_to_ignore or []
588
+ if "self" in args_to_ignore :
589
+ instance_fname = None
590
+
583
591
if instance_fname :
584
592
instance_version_key = self ._memvname (instance_fname )
585
593
fetch_keys .append (instance_version_key )
@@ -633,14 +641,20 @@ def _memoize_make_cache_key(
633
641
timeout = None ,
634
642
forced_update = False ,
635
643
hash_method = hashlib .md5 ,
636
- source_check = False
644
+ source_check = False ,
645
+ args_to_ignore = None ,
637
646
):
638
647
"""Function used to create the cache_key for memoized functions."""
639
648
640
649
def make_cache_key (f , * args , ** kwargs ):
641
650
_timeout = getattr (timeout , "cache_timeout" , timeout )
642
651
fname , version_data = self ._memoize_version (
643
- f , args = args , kwargs = kwargs , timeout = _timeout , forced_update = forced_update
652
+ f ,
653
+ args = args ,
654
+ kwargs = kwargs ,
655
+ timeout = _timeout ,
656
+ forced_update = forced_update ,
657
+ args_to_ignore = args_to_ignore ,
644
658
)
645
659
646
660
#: this should have to be after version_data, so that it
@@ -649,7 +663,7 @@ def make_cache_key(f, *args, **kwargs):
649
663
650
664
if callable (f ):
651
665
keyargs , keykwargs = self ._memoize_kwargs_to_args (
652
- f , * args , ** kwargs
666
+ f , * args , ** kwargs , args_to_ignore = args_to_ignore
653
667
)
654
668
else :
655
669
keyargs , keykwargs = args , kwargs
@@ -680,6 +694,7 @@ def _memoize_kwargs_to_args(self, f, *args, **kwargs):
680
694
#: 1, b=2 is equivilant to a=1, b=2, etc.
681
695
new_args = []
682
696
arg_num = 0
697
+ args_to_ignore = kwargs .pop ("args_to_ignore" , None ) or []
683
698
684
699
# If the function uses VAR_KEYWORD type of parameters,
685
700
# we need to pass these further
@@ -689,7 +704,10 @@ def _memoize_kwargs_to_args(self, f, *args, **kwargs):
689
704
690
705
for i in range (args_len ):
691
706
arg_default = get_arg_default (f , i )
692
- if i == 0 and arg_names [i ] in ("self" , "cls" ):
707
+ if arg_names [i ] in args_to_ignore :
708
+ arg = None
709
+ arg_num += 1
710
+ elif i == 0 and arg_names [i ] in ("self" , "cls" ):
693
711
#: use the id func of the class instance
694
712
#: this supports instance methods for
695
713
#: the memoized functions, giving more
@@ -769,7 +787,8 @@ def memoize(
769
787
response_filter = None ,
770
788
hash_method = hashlib .md5 ,
771
789
cache_none = False ,
772
- source_check = None
790
+ source_check = None ,
791
+ args_to_ignore = None ,
773
792
):
774
793
"""Use this to cache the result of a function, taking its arguments
775
794
into account in the cache key.
@@ -847,9 +866,17 @@ def big_foo(a, b):
847
866
formed with the function's source code hash in
848
867
addition to other parameters that may be included
849
868
in the formation of the key.
869
+ :param args_to_ignore: List of arguments that will be ignored while
870
+ generating the cache key. Default to None.
871
+ This means that those arguments may change
872
+ without affecting the cache value that will be
873
+ returned.
850
874
851
875
.. versionadded:: 0.5
852
876
params ``make_name``, ``unless``
877
+
878
+ .. versionadded:: 1.10
879
+ params ``args_to_ignore``
853
880
"""
854
881
855
882
def memoize (f ):
@@ -928,7 +955,8 @@ def decorated_function(*args, **kwargs):
928
955
timeout = decorated_function ,
929
956
forced_update = forced_update ,
930
957
hash_method = hash_method ,
931
- source_check = source_check
958
+ source_check = source_check ,
959
+ args_to_ignore = args_to_ignore ,
932
960
)
933
961
decorated_function .delete_memoized = lambda : self .delete_memoized (f )
934
962
0 commit comments