```python:inference/core/cache/memory.py
import threading
import time
from typing import Any

from inference.core.cache.base import BaseCache


class MemoryCache(BaseCache):

    def __init__(self) -> None:
        """
        Initializes a new instance of the MemoryCache class.
        """
        self.cache = dict()
        self.expires = dict()
        self.zexpires = dict()

        self._expire_thread = threading.Thread(target=self._expire)
        self._expire_thread.daemon = True
        self._expire_thread.start()

    def zadd(self, key: str, value: Any, score: float, expire: float = None):
        """
        Adds a member with the specified score to the sorted set stored at key.

        Args:
            key (str): The key of the sorted set.
            value (str): The value to add to the sorted set.
            score (float): The score associated with the value.
            expire (float, optional): The time, in seconds, after which the key will expire. Defaults to None.
        """
        if key not in self.cache:
            self.cache[key] = dict()
        self.cache[key][score] = value
        if expire:
            self.zexpires[(key, score)] = expire + time.time()
```
```python:inference/core/cache/serializers.py
from typing import List, Union

from fastapi.encoders import jsonable_encoder
from inference.core.devices.utils import GLOBAL_INFERENCE_SERVER_ID
from inference.core.entities.requests.inference import InferenceRequest
from inference.core.entities.responses.inference import InferenceResponse
from inference.core.env import TINY_CACHE
from inference.core.version import __version__


def to_cachable_inference_item(
    infer_request: InferenceRequest,
    infer_response: Union[InferenceResponse, List[InferenceResponse]],
) -> dict:
    if not TINY_CACHE:
        return {
            "inference_id": infer_request.id,
            "inference_server_version": __version__,
            "inference_server_id": GLOBAL_INFERENCE_SERVER_ID,
            "request": jsonable_encoder(infer_request),
            "response": jsonable_encoder(infer_response),
        }

    included_request_fields = {
        "api_key",
        "confidence",
        "model_id",
        "model_type",
        "source",
        "source_info",
    }
    request = infer_request.dict(include=included_request_fields)
    response = build_condensed_response(infer_response)
    return {
        "inference_id": infer_request.id,
        "inference_server_version": __version__,
        "inference_server_id": GLOBAL_INFERENCE_SERVER_ID,
        "request": request,
        "response": response,
    }
```
```python:inference/core/managers/base.py
import time
from threading import Lock
from typing import Dict, List, Optional, Union

from fastapi.encoders import jsonable_encoder
from inference.core.cache import cache
from inference.core.cache.serializers import to_cachable_inference_item
from inference.core.devices.utils import GLOBAL_INFERENCE_SERVER_ID
from inference.core.entities.requests.inference import InferenceRequest
from inference.core.entities.responses.inference import InferenceResponse
from inference.core.env import (DISABLE_INFERENCE_CACHE, METRICS_ENABLED,
                                METRICS_INTERVAL)
from inference.core.logger import logger
from inference.core.models.base import Model
from inference.core.registries.base import ModelRegistry
from inference.core.telemetry import record_error, record_inference, start_span


class ModelManager:

    def __init__(self, model_registry: ModelRegistry, models: Optional[dict] = None):
        self.model_registry = model_registry
        self._models: Dict[str, Model] = models if models is not None else {}
        self.pingback = None
        self._state_lock = Lock()
        self._models_state_locks: Dict[str, Lock] = {}

    def infer_from_request_sync(
        self, model_id: str, request: InferenceRequest, **kwargs
    ) -> InferenceResponse:
        """Runs inference on the specified model with the given request.

        Args:
            model_id (str): The identifier of the model.
            request (InferenceRequest): The request to process.

        Returns:
            InferenceResponse: The response from the inference.
        """
        logger.debug(
            f"ModelManager - inference from request started for model_id={model_id}."
        )
        enable_model_monitoring = not getattr(
            request, "disable_model_monitoring", False
        )
        if METRICS_ENABLED and self.pingback and enable_model_monitoring:
            logger.debug("ModelManager - setting pingback fallback api key...")
            self.pingback.fallback_api_key = request.api_key
        with start_span("model.infer", {"model.id": model_id}):
            try:
                t_infer_start = time.perf_counter()
                rtn_val = self.model_infer_sync(
                    model_id=model_id, request=request, **kwargs
                )
                record_inference(model_id, time.perf_counter() - t_infer_start)
                logger.debug(
                    f"ModelManager - inference from request finished for model_id={model_id}."
                )
                finish_time = time.time()
                if not DISABLE_INFERENCE_CACHE and enable_model_monitoring:
                    try:
                        logger.debug(
                            f"ModelManager - caching inference request started for model_id={model_id}"
                        )
                        cache.zadd(
                            f"models",
                            value=f"{GLOBAL_INFERENCE_SERVER_ID}:{request.api_key}:{model_id}",
                            score=finish_time,
                            expire=METRICS_INTERVAL * 2,
                        )
                        if (
                            hasattr(request, "image")
                            and hasattr(request.image, "type")
                            and request.image.type == "numpy"
                        ):
                            request.image.value = str(request.image.value)
                        cache.zadd(
                            f"inference:{GLOBAL_INFERENCE_SERVER_ID}:{model_id}",
                            value=to_cachable_inference_item(request, rtn_val),
                            score=finish_time,
                            expire=METRICS_INTERVAL * 2,
                        )
                        logger.debug(
                            f"ModelManager - caching inference request finished for model_id={model_id}"
                        )
                    except Exception as cache_error:
                        logger.warning(
                            f"Failed to cache inference data for model {model_id}: {cache_error}"
                        )
                return rtn_val
            except Exception as e:
                record_error(e)
                finish_time = time.time()
                if not DISABLE_INFERENCE_CACHE and enable_model_monitoring:
                    try:
                        cache.zadd(
                            f"models",
                            value=f"{GLOBAL_INFERENCE_SERVER_ID}:{request.api_key}:{model_id}",
                            score=finish_time,
                            expire=METRICS_INTERVAL * 2,
                        )
                        cache.zadd(
                            f"error:{GLOBAL_INFERENCE_SERVER_ID}:{model_id}",
                            value={
                                "request": jsonable_encoder(
                                    request.dict(exclude={"image", "subject", "prompt"})
                                ),
                                "error": str(e),
                            },
                            score=finish_time,
                            expire=METRICS_INTERVAL * 2,
                        )
                    except Exception as cache_error:
                        logger.warning(
                            f"Failed to cache error data for model {model_id}: {cache_error}"
                        )
                raise

    def model_infer_sync(
        self, model_id: str, request: InferenceRequest, **kwargs
    ) -> Union[List[InferenceResponse], InferenceResponse]:
        model = self._get_model_reference(model_id=model_id)
        return model.infer_from_request(request)
```