Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
close files after open, category cache
  • Loading branch information
WillGITCode committed Apr 9, 2022
commit 8bcf806f7712092090e5cf864b615b779c12a91c
9 changes: 7 additions & 2 deletions newspaper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,17 @@ def inner_function(*args, **kwargs):
modified = os.path.getmtime(filepath)
age_seconds = time.time() - modified
if age_seconds < seconds:
return pickle.load(open(filepath, "rb"))
cache_file = open(filepath, "rb")
content = pickle.load(cache_file)
cache_file.close()
return content

# call the decorated function...
result = function(*args, **kwargs)
# ... and save the cached object for next time
pickle.dump(result, open(filepath, "wb"))
cache_file = open(filepath, "wb")
pickle.dump(result, cache_file)
cache_file.close()
return result
return inner_function
return do_cache
Expand Down