Added result caching to pyMetadata + Update to G.A

This commit is contained in:
Brage 2023-07-20 23:43:23 +02:00
parent c2ac823e25
commit 36dda6d04e
3 changed files with 38 additions and 2 deletions

View File

@ -17,7 +17,17 @@ jobs:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Check if code has changed
id: check-commoncode
run: |
if [[ $(git diff --name-only ${{ github.event.before }} ${{ github.event.after }} CommonCode/) ]]; then
echo "::set-output name=skip::false"
else
echo "::set-output name=skip::true"
fi
- name: Cache CommonCode Gradle dependencies - name: Cache CommonCode Gradle dependencies
if: steps.check-commoncode.outputs.skip == 'false'
id: cache-gradle id: cache-gradle
uses: actions/cache@v2 uses: actions/cache@v2
with: with:

View File

@ -11,6 +11,7 @@ from sources.result import DataResult, Metadata
from sources.anii import metadata as AniiMetadata from sources.anii import metadata as AniiMetadata
from sources.imdb import metadata as ImdbMetadata from sources.imdb import metadata as ImdbMetadata
from sources.mal import metadata as MalMetadata from sources.mal import metadata as MalMetadata
from sources.cache import ResultCache
# Konfigurer Kafka-forbindelsen # Konfigurer Kafka-forbindelsen
bootstrap_servers = os.environ.get("KAFKA_BOOTSTRAP_SERVER") or "127.0.0.1:9092" bootstrap_servers = os.environ.get("KAFKA_BOOTSTRAP_SERVER") or "127.0.0.1:9092"
@ -123,8 +124,19 @@ class MessageHandlerThread(threading.Thread):
if status_type == 'SUCCESS': if status_type == 'SUCCESS':
data_value = self.message.value['data']["title"] data_value = self.message.value['data']["title"]
# Utfør handlingen basert på verdien result = None # Will be assigned by either cache_result or sel.perform_action
result = self.perform_action(title=data_value) print(f"Checking cache for offloading")
cache_result = ResultCache.get(data_value)
if cache_result:
print(f"Cache hit for {data_value}")
result = cache_result
else:
print(f"Searching in sources for infomration about {data_value}")
result = self.perform_action(title=data_value)
if (result.statusType == "SUCCESS"):
print(f"Storing response for {data_value} in-memory cache")
ResultCache.add(data_value, result)
producerMessage = self.compose_message(referenceId=self.message.value["referenceId"], result=result) producerMessage = self.compose_message(referenceId=self.message.value["referenceId"], result=result)

View File

@ -0,0 +1,14 @@
from typing import Optional
from .result import DataResult
class ResultCache:
_cache = {}
@classmethod
def add(cls, title: str, result: DataResult):
cls._cache[title] = result
@classmethod
def get(cls, title) -> Optional[DataResult]:
return cls._cache.get(title)