Skip to content

Commit

Permalink
fix: remove CachedStore use in products.py
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 committed Jan 3, 2023
1 parent 9a558f1 commit 57b0970
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
11 changes: 5 additions & 6 deletions robotoff/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
import tempfile
from typing import Iterable, Iterator, Optional, Union

import cachetools
import requests
from pymongo import MongoClient

from robotoff import settings
from robotoff.mongo import MONGO_CLIENT_CACHE
from robotoff.types import JSONType
from robotoff.utils import get_logger, gzip_jsonl_iter, http_session, jsonl_iter
from robotoff.utils.cache import CachedStore

logger = get_logger(__name__)

Expand Down Expand Up @@ -499,9 +499,11 @@ def iter_product(self, projection: Optional[list[str]] = None):
yield from (Product(p) for p in self.collection.find(projection=projection))


def load_min_dataset() -> ProductStore:
@cachetools.cached(cachetools.LRUCache(maxsize=1))
def get_min_product_store() -> ProductStore:
logger.info("Loading product store in memory...")
ps = MemoryProductStore.load_min()
logger.info("product store loaded ({} items)".format(len(ps)))
logger.info("product store loaded (%s items)", len(ps))
return ps


Expand All @@ -522,6 +524,3 @@ def get_product(
"""
mongo_client = MONGO_CLIENT_CACHE.get()
return mongo_client.off.products.find_one({"code": barcode}, projection)


CACHED_PRODUCT_STORE = CachedStore(load_min_dataset)
4 changes: 2 additions & 2 deletions robotoff/scheduler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
from robotoff.models import ProductInsight, db, with_db
from robotoff.prediction.category.matcher import predict_from_dataset
from robotoff.products import (
CACHED_PRODUCT_STORE,
Product,
ProductDataset,
fetch_dataset,
get_min_product_store,
has_dataset_changed,
)
from robotoff.utils import get_logger
Expand Down Expand Up @@ -80,7 +80,7 @@ def process_insights():
def refresh_insights(with_deletion: bool = False):
deleted = 0
updated = 0
product_store = CACHED_PRODUCT_STORE.get()
product_store = get_min_product_store()

datetime_threshold = datetime.datetime.utcnow().replace(
hour=0, minute=0, second=0, microsecond=0
Expand Down

0 comments on commit 57b0970

Please sign in to comment.