Skip to content

Commit

Permalink
use tempfiles for query caching
Browse files Browse the repository at this point in the history
  • Loading branch information
halworsen committed Apr 3, 2024
1 parent 97982ae commit f7e8938
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Backwards compatibility is not guaranteed in versions <1.0.0.
* Phase information is exposed at the fight level in this client (`FFLogsFight.phases`),
*not* at the report level
* Fix quotes not being properly escaped in query filters
* System temporary directories are now used for query caches
* As a result, the `cache_directory` param on the client is now deprecated
* Bump cryptography dependency (vulnerability fix)

## v2.0.2
Expand Down
12 changes: 11 additions & 1 deletion fflogsapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

import os
import pickle
import tempfile
from copy import deepcopy
from functools import wraps
from time import time
from typing import Any
from warnings import warn

from gql import Client as GQLClient
from gql import gql
Expand Down Expand Up @@ -127,9 +129,17 @@ def __init__(
self._query_cache = {}
self.cache_expiry = cache_expiry
self.cache_queries = enable_caching
self.cache_dir = cache_directory
self.ignore_cache_expiry = ignore_cache_expiry

# deprecation warning for cache_directory use
if cache_directory != './fflogs-querycache':
warn('Custom cache directories are deprecated in favor of system temp dirs.'
' Consider removing usage of cache_directory when instantiating FFLogsClient.',
category=FutureWarning)
else:
# future behavior
self.cache_dir = os.path.join(tempfile.gettempdir(), 'fflogsapi')

if enable_caching:
if not os.path.exists(self.cache_dir):
os.makedirs(self.cache_dir)
Expand Down
23 changes: 11 additions & 12 deletions tests/client/test_caching.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import unittest
import tempfile
from time import sleep, time

from fflogsapi.client import FFLogsClient
Expand All @@ -13,22 +14,20 @@ class CacheTest(unittest.TestCase):
WARNING: Running these tests will delete all existing query caches!
'''

CACHE_DIR = './querycache'
CACHE_EXPIRY = 2 # seconds

@classmethod
def setUpClass(cls) -> None:
# The cache directory must be empty before starting these tests (if it exists)
if os.path.exists(cls.CACHE_DIR):
for fn in os.listdir(cls.CACHE_DIR):
os.remove(os.path.join(cls.CACHE_DIR, fn))
os.rmdir(cls.CACHE_DIR)
cache_dir = os.path.join(tempfile.gettempdir(), 'fflogsapi')
if os.path.exists(cache_dir):
for fn in os.listdir(cache_dir):
os.remove(os.path.join(cache_dir, fn))
os.rmdir(cache_dir)

cls.client = FFLogsClient(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
cache_directory=cls.CACHE_DIR,
cache_expiry=cls.CACHE_EXPIRY
)

Expand All @@ -54,8 +53,8 @@ def test_cache_saving(self) -> None:
The client should be able to save a file containing cached query results
'''
self.client.save_cache(silent=False)
self.assertTrue(os.path.exists(self.CACHE_DIR))
cache_expiry = list(map(lambda f: float(f[:-4]), os.listdir(self.CACHE_DIR)))[0]
self.assertTrue(os.path.exists(self.client.cache_dir))
cache_expiry = list(map(lambda f: float(f[:-4]), os.listdir(self.client.cache_dir)))[0]
self.assertAlmostEqual(time() + self.CACHE_EXPIRY, cache_expiry, places=1)

def test_extend_cache(self) -> None:
Expand All @@ -65,7 +64,7 @@ def test_extend_cache(self) -> None:
self.client.extend_cache(2)
self.client.save_cache()

expiries = list(map(lambda f: float(f[:-4]), os.listdir(self.CACHE_DIR)))
expiries = list(map(lambda f: float(f[:-4]), os.listdir(self.client.cache_dir)))
ok = False
for expiry in expiries:
if round(time() + self.CACHE_EXPIRY + 2, 1) == round(expiry, 1):
Expand All @@ -79,7 +78,7 @@ def test_clean_cache(self) -> None:
'''
self.client.save_cache()

expiries = list(map(lambda f: float(f[:-4]), os.listdir(self.CACHE_DIR)))
expiries = list(map(lambda f: float(f[:-4]), os.listdir(self.client.cache_dir)))
# is soonest even a word?
soonest_expire = min(expiries)

Expand All @@ -92,7 +91,7 @@ def test_clean_cache(self) -> None:
self.assertGreaterEqual(time(), soonest_expire)

self.client.clean_cache()
expiries = list(map(lambda f: float(f[:-4]), os.listdir(self.CACHE_DIR)))
expiries = list(map(lambda f: float(f[:-4]), os.listdir(self.client.cache_dir)))

self.assertNotIn(soonest_expire, expiries)

Expand Down

0 comments on commit f7e8938

Please sign in to comment.