# How m Caching Works for Available MongoDB Versions: Complete Technical Guide

> Discover how m caching works for MongoDB versions. Learn to control caching with M_CACHE, M_CACHE_SRC, and M_CACHE_EXPIRY environment variables to optimize your workflow.

- Repository: [Aaron Heckmann/m](https://github.com/aheckmann/m)
- Tags: deep-dive
- Published: 2026-02-23

---

**The `m` version manager caches the MongoDB version metadata JSON—not the actual binaries—to avoid repeated network requests, using the `M_CACHE`, `M_CACHE_SRC`, and `M_CACHE_EXPIRY` environment variables to control caching behavior.**

The `m` tool by Aaron Heckmann is a lightweight MongoDB version manager implemented as a Bash script. Unlike version managers that cache binary artifacts, `m` implements an opt-in caching mechanism specifically for the version list metadata retrieved from `https://downloads.mongodb.org/full.json`. This design minimizes network overhead when querying available versions without consuming disk space for redundant binary storage.

## How m Caches MongoDB Version Metadata

The caching logic resides entirely within the core script **`bin/m`**. Rather than caching MongoDB binaries themselves, the tool caches the JSON response containing all available versions, release channels, and download URLs.

### Cache Configuration Environment Variables

Three shell variables control the caching behavior:

- **`M_CACHE`** – Enables or disables caching. Defaults to `0` (disabled).
- **`M_CACHE_SRC`** – Specifies the cache file path. Defaults to `$M_DIR/cache-src.json`.
- **`M_CACHE_EXPIRY`** – Defines cache validity in seconds. Defaults to **3600** (1 hour).

These variables are initialized at lines 32-43 of `bin/m`:

```bash

# Use cache?

CACHE=${M_CACHE:-0}

# Location of cached source file

CACHE_SRC=${M_CACHE_SRC:-$M_DIR/cache-src.json}

# Cache source expiry (seconds)

CACHE_EXPIRY=${M_CACHE_EXPIRY:-3600}

```

### Reading the Cached Version List

When the **`get_all_versions`** function executes, it first checks if caching is enabled and whether the cached file is fresh. The script uses the **`file_age_in_seconds`** helper function to validate cache freshness against `CACHE_EXPIRY`.

At lines 14-22 in `bin/m`, the logic validates cache existence and age:

```bash
if [ $CACHE == 1 ]; then
  if [ -e $CACHE_SRC ]; then
    if [ $(file_age_in_seconds $CACHE_SRC) -lt $CACHE_EXPIRY ]; then
      all_versions=`cat $CACHE_SRC`
    fi
  fi
fi

```

If the cache exists and its age is less than the expiry threshold, `m` reads the JSON directly from disk instead of fetching from the remote endpoint.

### Writing and Refreshing the Cache

When the cache is disabled, missing, or expired, `m` fetches fresh data from `$src_url` (pointing to `https://downloads.mongodb.org/full.json`). If caching is enabled, the newly fetched data persists to `$CACHE_SRC` for subsequent calls.

Lines 24-31 of `bin/m` implement the write logic:

```bash
if [ -z "$all_versions" ]; then
  all_versions=`$GET $src_url`
  if [ $CACHE ]; then
    echo "$all_versions" > $CACHE_SRC
  fi
fi

```

All version-querying commands—including `list_versions`, `display_latest_version`, and `display_latest_stable_version`—invoke `get_all_versions`, ensuring consistent cache utilization across the tool.

## Practical Cache Configuration Examples

Configure `m` caching behavior using environment variables before executing commands.

Enable caching for a single session with the default 1-hour expiry:

```bash
export M_CACHE=1
m ls 4.4

```

Extend cache validity to 12 hours:

```bash
export M_CACHE=1
export M_CACHE_EXPIRY=$((12*60*60))
m ls 5.0

```

Force a fresh fetch by disabling the cache:

```bash
M_CACHE=0 m ls 6.0

```

Manually clear the cache file:

```bash
rm "$HOME/.local/m/cache-src.json"

```

## Summary

- **`m` caches only metadata**, not MongoDB binaries, storing the JSON from [`downloads.mongodb.org/full.json`](https://github.com/aheckmann/m/blob/main/downloads.mongodb.org/full.json) locally.
- **Caching is opt-in** via the `M_CACHE` environment variable, which defaults to `0` (disabled).
- **Cache location** defaults to `$M_DIR/cache-src.json` but is configurable via `M_CACHE_SRC`.
- **Default expiry** is 3600 seconds (1 hour), controlled by `M_CACHE_EXPIRY`.
- **Implementation** resides in `bin/m`, specifically within the `get_all_versions` function and supporting cache validation logic.

## Frequently Asked Questions

### Does m cache MongoDB binaries or just version metadata?

`m` caches only the version metadata JSON file, not the actual MongoDB binaries. The cache stores the response from `https://downloads.mongodb.org/full.json`, which contains version numbers and download URLs, while the binaries themselves are downloaded on-demand during installation.

### How do I enable caching in m?

Set the `M_CACHE` environment variable to `1` before running `m` commands. By default, `M_CACHE` is `0`, meaning the tool fetches fresh version data from the MongoDB downloads API on every invocation unless caching is explicitly enabled.

### Where does m store its cache file?

By default, `m` stores the cache at `$M_DIR/cache-src.json`, typically resolving to `$HOME/.local/m/cache-src.json`. You can override this location by setting the `M_CACHE_SRC` environment variable to a custom file path.

### How long does m keep the version cache?

The default cache expiry is 3600 seconds (1 hour), defined by the `M_CACHE_EXPIRY` environment variable. After this period, `m` considers the cache stale and fetches fresh metadata from the remote endpoint upon the next version query.