Skip to content

Commit

Permalink
Update write_cache, test
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanpulver committed Jul 3, 2024
1 parent 54acd42 commit 49007d5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
30 changes: 18 additions & 12 deletions safety/safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,26 @@ def write_to_cache(db_name, data):
if exc.errno != errno.EEXIST:
raise

with open(DB_CACHE_FILE, "r") as f:
try:
cache = json.loads(f.read())
except json.JSONDecodeError:
LOG.debug('JSONDecodeError in the local cache, dumping the full cache file.')
cache_file_lock = f"{DB_CACHE_FILE}.lock"
lock = FileLock(cache_file_lock, timeout=10)
with lock:
if os.path.exists(DB_CACHE_FILE):
with open(DB_CACHE_FILE, "r") as f:
try:
cache = json.loads(f.read())
except json.JSONDecodeError:
LOG.debug('JSONDecodeError in the local cache, dumping the full cache file.')
cache = {}
else:
cache = {}

with open(DB_CACHE_FILE, "w") as f:
cache[db_name] = {
"cached_at": time.time(),
"db": data
}
f.write(json.dumps(cache))
LOG.debug('Safety updated the cache file for %s database.', db_name)
with open(DB_CACHE_FILE, "w") as f:
cache[db_name] = {
"cached_at": time.time(),
"db": data
}
f.write(json.dumps(cache))
LOG.debug('Safety updated the cache file for %s database.', db_name)


def fetch_database_url(session, mirror, db_name, cached, telemetry=True,
Expand Down
3 changes: 3 additions & 0 deletions tests/test_safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ def test_check_live(self):
def test_check_live_cached(self):
from safety.constants import DB_CACHE_FILE

# Ensure the cache directory and file exist
os.makedirs(os.path.dirname(DB_CACHE_FILE), exist_ok=True)

# lets clear the cache first
try:
with open(DB_CACHE_FILE, 'w') as f:
Expand Down

0 comments on commit 49007d5

Please sign in to comment.