Skip to content

Commit b19355c

Browse files
authored
Refactor file_cache function to include func_source_code_hash (#3332)
1 parent bdcd119 commit b19355c

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

docs/pages/blogs/file-cache.mdx

+5-1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ We have another problem - we want to miss our cache under two conditions:
153153
To handle 2. we used `inspect.getsource(func)` to add the function's source code to the hash, correctly missing the cache when the the code changes.
154154

155155
```python /hash_code(inspect.getsource(func))/
156+
func_source_code_hash = hash_code(inspect.getsource(func))
157+
```
158+
159+
```python /func_source_code_hash/
156160
args_names = func.__code__.co_varnames[: func.__code__.co_argcount]
157161
args_dict = dict(zip(args_names, args))
158162

@@ -166,7 +170,7 @@ To handle 2. we used `inspect.getsource(func)` to add the function's source code
166170
arg_hash = (
167171
recursive_hash(args_dict, ignore_params=ignore_params)
168172
+ recursive_hash(kwargs_clone, ignore_params=ignore_params)
169-
+ hash_code(inspect.getsource(func))
173+
+ func_source_code_hash
170174
)
171175
cache_file = os.path.join(
172176
cache_dir, f"{func.__module__}_{func.__name__}_{arg_hash}.pickle"

docs/public/file_cache.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def decorator(func):
5757
if verbose:
5858
print("Cache is disabled for function: " + func.__name__)
5959
return func
60+
func_source_code_hash = hash_code(inspect.getsource(func))
6061

6162
def wrapper(*args, **kwargs):
6263
cache_dir = "/tmp/file_cache"
@@ -76,7 +77,7 @@ def wrapper(*args, **kwargs):
7677
arg_hash = (
7778
recursive_hash(args_dict, ignore_params=ignore_params)
7879
+ recursive_hash(kwargs_clone, ignore_params=ignore_params)
79-
+ hash_code(inspect.getsource(func))
80+
+ func_source_code_hash
8081
)
8182
cache_file = os.path.join(
8283
cache_dir, f"{func.__module__}_{func.__name__}_{arg_hash}.pickle"
@@ -103,4 +104,4 @@ def wrapper(*args, **kwargs):
103104

104105
return wrapper
105106

106-
return decorator
107+
return decorator

sweepai/logn/cache.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def file_cache(ignore_params=[], verbose=False):
5656
def decorator(func):
5757
if DEBUG:
5858
return func
59+
func_source_code_hash = hash_code(inspect.getsource(func))
5960

6061
def wrapper(*args, **kwargs):
6162
cache_dir = "/tmp/file_cache"
@@ -75,7 +76,7 @@ def wrapper(*args, **kwargs):
7576
arg_hash = (
7677
recursive_hash(args_dict, ignore_params=ignore_params)
7778
+ recursive_hash(kwargs_clone, ignore_params=ignore_params)
78-
+ hash_code(inspect.getsource(func))
79+
+ func_source_code_hash
7980
)
8081
cache_file = os.path.join(
8182
cache_dir, f"{func.__module__}_{func.__name__}_{arg_hash}.pickle"

0 commit comments

Comments
 (0)