Skip to content

Commit

Permalink
Merge pull request #101 from ProteinsWebTeam/no_cache_modifiers
Browse files Browse the repository at this point in the history
No cache modifiers
  • Loading branch information
gustavo-salazar authored Feb 22, 2023
2 parents 209e7e7 + 73d32c0 commit 09bc2c7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
37 changes: 36 additions & 1 deletion webfront/tests/tests_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.test import TestCase

from webfront.views.cache import canonical
from webfront.views.common import map_url_to_levels
from webfront.views.cache import canonical, get_timeout_from_path, SHOULD_NO_CACHE, FIVE_DAYS


class CanonicalTestCase(TestCase):
Expand Down Expand Up @@ -54,3 +55,37 @@ def with_query_remove_unneeded_urls(self):
"/api/entry/InterPro/?integrated=pfam",
canonical("/api/entry/InterPro/?integrated=pfam&page_size=20"),
)


class CacheLifespanTestCase(TestCase):
def test_urls_no_cacheable(self):
urls = [
"/entry/InterPro/IPR000001/",
"/protein/uniprot/p99999/?extra_features",
"/entry/InterPro/?page",
]
for url in urls:
levels = map_url_to_levels(url.split('?')[0])
self.assertEqual(SHOULD_NO_CACHE, get_timeout_from_path(url, levels))

def test_urls_short_life(self):
urls = [
"/entry/InterPro/?page=33",
"/entry/InterPro/?page_size=33",
"/entry/InterPro/?format",
]
for url in urls:
levels = map_url_to_levels(url.split('?')[0])
self.assertEqual(FIVE_DAYS, get_timeout_from_path(url, levels))

def test_urls_long_life(self):
urls = [
"/entry/",
"/entry/InterPro/",
"/entry/InterPro/protein",
"/entry/InterPro/IPR000001/protein",
"/protein/uniprot/p99999/?conservation",
]
for url in urls:
levels = map_url_to_levels(url.split('?')[0])
self.assertIsNone(get_timeout_from_path(url, levels))
42 changes: 27 additions & 15 deletions webfront/views/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@

names = [ep["name"].lower() for ep in endpoints]

short_life_parameters = [
"cursor",
"size",
"go_terms",
"ida_ignore",
"ida_search",
"format",
"page_size",
"search",
]

no_cache_modifiers = [
"extra_features",
"residues",
"isoforms",
"ida",
"taxa",
"model:"
"annotation:info",
"subfamilies",
"subfamily",
"page_size",
]

def get_timeout_from_path(path, endpoint_levels):
parsed = urlparse(path)
Expand All @@ -31,11 +54,9 @@ def get_timeout_from_path(path, endpoint_levels):
# it doesn't have modifiers
if len(query.keys()) == 0:
return SHOULD_NO_CACHE
if ( # The only modifier is page_size
len(query.keys()) == 1
and "page_size" in query
):
return SHOULD_NO_CACHE
for parameter in no_cache_modifiers:
if parameter in query:
return SHOULD_NO_CACHE

# order querystring, lowercase keys
query = OrderedDict(
Expand All @@ -49,17 +70,8 @@ def get_timeout_from_path(path, endpoint_levels):
return FIVE_DAYS
except Exception:
return SHOULD_NO_CACHE
short_life_parameters = [
"cursor",
"size",
"go_terms",
"ida_ignore",
"ida_search",
"format",
]
for parameter in short_life_parameters:
value = query.get(parameter)
if value is not None:
if parameter in query:
return FIVE_DAYS
return None

Expand Down
4 changes: 2 additions & 2 deletions webfront/views/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from webfront.views.proteome import ProteomeHandler
from webfront.views.set import SetHandler
from webfront.views.utils import UtilsHandler
from webfront.views.cache import InterProCache, get_timeout_from_path, SHOULD_NO_CACHE
from webfront.views.cache import InterProCache, get_timeout_from_path, canonical, SHOULD_NO_CACHE

from webfront.models import Database
from concurrent.futures import ThreadPoolExecutor, wait, FIRST_COMPLETED
Expand Down Expand Up @@ -191,7 +191,7 @@ def query(args):
drf_request=drf_request,
)
# Got a good response, then save it in cache
timeout = get_timeout_from_path(full_path, endpoint_levels)
timeout = get_timeout_from_path(canonical(full_path), endpoint_levels)
if timeout != SHOULD_NO_CACHE:
self._set_in_cache(caching_allowed, full_path, response, timeout)
# Forcing to close the connection because django is not closing it when this query is ran as future
Expand Down

0 comments on commit 09bc2c7

Please sign in to comment.