diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 74181411..ac949b45 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,7 +17,17 @@ jobs: - name: Checkout repository 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 + if: steps.check-commoncode.outputs.skip == 'false' id: cache-gradle uses: actions/cache@v2 with: diff --git a/pyMetadata/app.py b/pyMetadata/app.py index 124bfa17..f11192eb 100644 --- a/pyMetadata/app.py +++ b/pyMetadata/app.py @@ -11,6 +11,7 @@ from sources.result import DataResult, Metadata from sources.anii import metadata as AniiMetadata from sources.imdb import metadata as ImdbMetadata from sources.mal import metadata as MalMetadata +from sources.cache import ResultCache # Konfigurer Kafka-forbindelsen 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': data_value = self.message.value['data']["title"] - # Utfør handlingen basert på verdien - result = self.perform_action(title=data_value) + result = None # Will be assigned by either cache_result or sel.perform_action + 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) diff --git a/pyMetadata/sources/cache.py b/pyMetadata/sources/cache.py new file mode 100644 index 00000000..d6c3fb4e --- /dev/null +++ b/pyMetadata/sources/cache.py @@ -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) \ No newline at end of file