# Class ModelCache

java.lang.Object
com.codename1.ai.inference.ModelCache

public final class ModelCache extends Object

Downloads a large model into app-private storage and exposes it as a file-backed `ModelSource`. The initial request requires HTTPS. Ports that expose redirect responses reject any redirect to HTTP. Because iOS follows redirects below the portable network layer, iOS downloads require a SHA-256 digest so an unseen redirect cannot substitute the executable model payload. Downloads use a temporary file and are promoted only after optional digest verification.

Supply a SHA-256 digest for third-party or remotely mutable models. Without a digest HTTPS authenticates the connection but does not pin the executable model payload. Small first-party models can instead be packaged with `ModelSource.resource(String)`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static AsyncResource<ModelSource>`
  `fetch(String url, String cacheKey)`
  Fetches a model without content pinning on ports that expose redirect responses to the portable network layer.
  `static AsyncResource<ModelSource>`
  `fetch(String url, String cacheKey, String sha256)`
  Fetches a model into the app-private `ai-models` directory.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### fetch

    public static AsyncResource<ModelSource> fetch(String url, String cacheKey, String sha256)
    Fetches a model into the app-private `ai-models` directory. A stale `.download` file is deleted and restarted rather than resumed because the portable network layer cannot prove that a server's partial response still represents the pinned model. Concurrent requests for the same cache entry and content identity share one underlying operation, but each caller receives an independent resource. Canceling one caller's resource suppresses only that caller's notification and does not cancel the shared download or other callers. A different URL or digest using that cache key while the first operation is active fails instead of racing on the temporary file.
    Parameters:
      `url` - initial HTTPS URL of the model; observable redirects must also use HTTPS
      `cacheKey` - stable cache name, independent of the URL
      `sha256` - lowercase or uppercase SHA-256 hex digest; optional on ports that expose redirects and required on iOS
    Returns:
      asynchronous cached model source
    Throws:
      `IllegalArgumentException` - if the URL, cache key, or digest is invalid, or if the digest is omitted on iOS

  - ### fetch

    public static AsyncResource<ModelSource> fetch(String url, String cacheKey)
    Fetches a model without content pinning on ports that expose redirect responses to the portable network layer. Prefer the three-argument overload for any model that is not versioned by the app itself. iOS rejects this overload because its native network stack follows redirects before Codename One can validate their schemes. Concurrent unpinned calls share an operation only when their URL and cache key are identical.
    Parameters:
      `url` - HTTPS model URL
      `cacheKey` - stable cache name
    Returns:
      asynchronous cached file source
    Throws:
      `IllegalArgumentException` - if the URL or cache key is invalid, or when called on iOS, where a digest is required