Skip to content

Commit

Permalink
#209: Add client caching function
Browse files Browse the repository at this point in the history
- This uses an LRU caching algorithm to determine which client objects should remain cached when the cache is full
  • Loading branch information
root authored and MRichards99 committed Mar 15, 2021
1 parent 268dac3 commit d1559a6
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions datagateway_api/common/icat/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timedelta
from functools import wraps
from functools import lru_cache, wraps
import logging

import icat.client
Expand Down Expand Up @@ -54,8 +54,8 @@ def requires_session_id(method):
@wraps(method)
def wrapper_requires_session(*args, **kwargs):
try:
client = create_client()
client.sessionId = args[1]
client = get_cached_client(args[1])

# Client object put into kwargs so it can be accessed by backend functions
kwargs["client"] = client

Expand All @@ -72,6 +72,18 @@ def wrapper_requires_session(*args, **kwargs):
return wrapper_requires_session


@lru_cache(maxsize=28)
def get_cached_client(session_id):
"""
TODO - Add docstring
"""
log.debug(f"Caching, session ID: {session_id}")
client = create_client()
client.sessionId = session_id

return client


def create_client():
client = icat.client.Client(
config.get_icat_url(), checkCert=config.get_icat_check_cert(),
Expand Down

0 comments on commit d1559a6

Please sign in to comment.