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 f8c6cfa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 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
13 changes: 12 additions & 1 deletion fflogsapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import os
import pickle
import tempfile
from copy import deepcopy
from functools import wraps
from time import time
Expand All @@ -25,6 +26,8 @@
from .user_auth import UserModeAuthMixin
from .world.client_extensions import WorldMixin

from warnings import warn


def ensure_token(func):
'''
Expand Down Expand Up @@ -127,9 +130,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
26 changes: 12 additions & 14 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,25 +14,22 @@ 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)

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

# The cache directory must be empty before starting these tests (if it exists)
if os.path.exists(cls.client.cache_dir):
for fn in os.listdir(cls.client.cache_dir):
os.remove(os.path.join(cls.client.cache_dir, fn))
os.rmdir(cls.client.cache_dir)

@classmethod
def tearDownClass(cls) -> None:
cls.client.close()
Expand All @@ -54,8 +52,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 +63,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 +77,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 +90,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 f8c6cfa

Please sign in to comment.