> ## Documentation Index
> Fetch the complete documentation index at: https://docs-unity.molca.id/llms.txt
> Use this file to discover all available pages before exploring further.

# CacheManager

> Local network cache subsystem with disk persistence, validation, and type-based caching selection.

**Namespace:** `Molca.Networking.Utils`\
**Interface:** `ICacheService`\
**File:** `Packages/com.molca.core/Runtime/Networking/Utils/Caching/CacheManager.cs`\
**Type:** `RuntimeSubsystem` (child of [RuntimeManager](/core/runtime-manager) prefab)

## When to use

Use `CacheManager` for caching network responses (textures, audio clips, data) to disk. It manages a persistent cache in `Application.persistentDataPath/Network Cache/` with configurable type selection via `CachingSelection` flags.

> ⚠️ The static API (`CacheManager.Cache`, `CacheManager.IsCached`, etc.) is deprecated. Use the `ICacheService` instance interface.

## Role

* **Disk cache** — stores cached data under `Application.persistentDataPath/Network Cache/`.
* **Type selection** — `CachingSelection` flags control which asset types are cached (`Texture`, `AudioClip`, `Data`).
* **Auto-validate** — on initialization, validates cached files and removes stale or corrupted entries.
* **Android storage permission** — requests `ExternalStorageWrite` permission on Android automatically.

## Inspector configuration

| Field               | Type               | Purpose                                                |
| ------------------- | ------------------ | ------------------------------------------------------ |
| `_cacheFlags`       | `CachingSelection` | Bitmask of asset types to cache.                       |
| `_clearCacheOnExit` | `bool`             | Whether to clear the cache when the application quits. |

## API Reference

### ICacheService (recommended)

Resolve via `RuntimeManager.GetService<ICacheService>()` or `[Inject] ICacheService`.

| Method         | Signature                                                                                          | Description                                                       |
| -------------- | -------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
| `Cache`        | `Awaitable Cache<T>(string id, T data, int durationSec, string group = null, bool persist = true)` | Cache data with a TTL duration and optional group tagging.        |
| `TryGetCache`  | `bool TryGetCache<T>(string id, int maxAgeSec, out T data, bool extendLifetime = true)`            | Try to retrieve cached data. Returns false if missing or expired. |
| `IsCached`     | `bool IsCached(string id)`                                                                         | Check if an item exists in the cache index.                       |
| `ClearCache`   | `void ClearCache()`                                                                                | Clear all cached data and the index.                              |
| `GetCachePath` | `Awaitable<string> GetCachePath(string id)`                                                        | Get the full file path for a cached item.                         |
| `GetTempPath`  | `string GetTempPath()`                                                                             | Get a temporary directory path for scratch files.                 |
| `CachePath`    | `string CachePath { get; }`                                                                        | The root cache directory path.                                    |
| `CacheSize`    | `long CacheSize { get; }`                                                                          | Current size of the cache in bytes.                               |
| `IsReady`      | `bool IsReady { get; }`                                                                            | Whether the cache subsystem is initialized.                       |

## Code

```csharp theme={null}
using Molca;
using Molca.Networking.Utils;
using UnityEngine;

public class DataCacher : MonoBehaviour
{
    [Inject] private ICacheService _cache;

    private async void Start()
    {
        await RuntimeManager.WaitForInitialization();
    }

    public async void CacheResult(string key, string data)
    {
        await _cache.Cache(key, data, 3600); // Cache for 1 hour
    }

    public bool TryGetResult(string key, out string data)
    {
        return _cache.TryGetCache(key, 3600, out data);
    }
}
```

## Related

* [RuntimeManager](/core/runtime-manager) — `CacheManager` is a child subsystem
* [HttpClient](/core/http-client) — network responses can be cached through this manager
* [Encryption](/core/encryption) — cache files can be encrypted at rest
