# Migrate From Flask-Cache to Flask-Caching

Yes. An agent swaps the abandoned Flask-Cache for Flask-Caching, renames the backend setting, and proves with requests that responses are really cached.

[Run this on Rush](https://cloud.getrush.ai/run/migrate-flask-cache-to-flask-caching/flask-cache/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

Flask-Cache has had no release since 0.13.1, and it cannot be imported next to a current Flask. Flask-Caching is the maintained fork with the same `Cache` class and the same `cached` and `memoize` decorators, so the migration looks like a one-line import change. The agent treats it as a behaviour to prove instead. It reads the pins first. In a small JSON API like christophersu/quora-api, `requirements.txt` holds `Flask==0.10.1` and `Flask-Cache==0.13.1`, and `server.py` opens with `from flask.ext.cache import Cache`, builds `Cache(config={'CACHE_TYPE': 'simple'})`, calls `cache.init_app(app)`, and puts `@cache.cached(timeout=300)` on eleven routes.

It reproduces what a present-day install does. The `flask.ext` namespace was removed in Flask 1.0, so the first line a current environment trips on is `ModuleNotFoundError: No module named 'flask.ext'`. Rewriting it to `from flask_cache import Cache` does not help: Flask-Cache's own `__init__.py` fails with `ImportError: cannot import name 'import_string' from 'werkzeug'`. That is the evidence that the package has to go. The agent replaces the requirement with `Flask-Caching`, changes the import to `from flask_caching import Cache`, and starts the app again.

The next failure is in configuration. Current Flask-Caching no longer accepts the short backend name, and `init_app` raises an `ImportError` saying `flask_caching.backends` has no attribute `simple`. The setting becomes `'CACHE_TYPE': 'SimpleCache'`. The same rename applies to the other short names a project might use, such as `redis` and `filesystem`, and the agent checks the whole config for them, along with any import of Flask-Cache's backends module for a custom backend.

Then it tests the one thing the library is for. It sends the same request twice through Flask's test client with a counter on the function behind the view, and the counter reads two. Nothing is cached, before or after the migration, and the cause is in the application: every cached route is written with `@cache.cached(timeout=300)` above `@app.route(...)`. Decorators apply from the bottom up, so Flask registers the plain function and the cached wrapper is never called. With the two lines swapped on all eleven routes, a repeated request for `/users/bob` reaches the function once and a request for `/users/amy` is cached separately, since `cached` keys on the request path. One limit is stated up front. This fixture's data comes from a `quora` scraping package that fails on Python 3 with `No module named 'user'`, so the agent stands in a stub for that module while it measures caching and does not claim that the service's real endpoints were exercised.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Migrate this Flask application from Flask-Cache to Flask-Caching. Read the dependency files and report the pinned Flask, Werkzeug and Flask-Cache versions. Find every import of flask.ext.cache or flask_cache, every Cache() construction and init_app call, every CACHE_ config key, and every use of cached, memoize, delete_memoized and the template fragment cache tag. In a clean virtualenv with current Flask, show the import error the old package produces. Replace the dependency and the imports, and rename any short CACHE_TYPE value (simple, null, redis, filesystem, memcached) to the backend class name Flask-Caching expects, keeping the same backend. Do not change timeouts, key prefixes or which views are cached. Then prove caching works: for each cached view or function, call it twice through the test client with a counter or mock on the work behind it and report how many times the work ran. If a view is decorated but not actually cached, find out why, check whether that was already true on the old package, and fix it only if the cause is decorator order or a renamed setting; report anything else. If the app cannot start for reasons unrelated to caching, stub only what is needed to exercise the cache layer and say exactly what was stubbed. Do not upgrade unrelated pins beyond what the new package requires. Open a pull request with each error and the request counts before and after.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that changes `requirements.txt` and `server.py`: the dependency line, the import, the `CACHE_TYPE` value, and the order of two decorators on each of eleven routes. The description walks through the three errors in sequence, the missing `flask.ext` module, Flask-Cache's failed Werkzeug import, and the rejected short backend name, with the line that resolved each.

The decorator finding is presented as its own section with numbers: calls to the underlying function for two identical requests, before the swap and after. It is marked as a defect that predates the migration, because merging it changes production behaviour. Responses that were always computed fresh are now served from memory for five minutes. That is what the code says it wanted, and a reviewer should still decide it knowingly, and may want it as a separate commit.

The description closes with what the run cannot show. `SimpleCache` lives in one process, so under gunicorn with several workers each worker keeps its own copy, exactly as the old `simple` backend did. And the scraping dependency is stubbed, so the pull request proves the cache layer and says nothing about whether the upstream site still answers.

## Run it on every Monday morning

Abandoned Flask extensions are usually found in batches, when a team finally moves a group of old services to a current Python. Put this prompt in an automation on each service's Project with a `schedule` trigger, a `cron` of `0 9 * * 1` for example, and let it report weekly until the migration is merged. A run that finds no Flask-Cache import left has nothing to do and opens no pull request.

For a one-time sweep, search your organisation for `flask.ext.cache` and `flask_cache`, then start one run for each repository through `POST /o/{org}/sessions` with the body above.

## Why run it on Rush?

A cache that is not working fails silently. The pages load, the tests pass, and the only symptom is a slower service and more calls to whatever sits behind it. A migration done by search and replace carries that state forward and adds a commit message saying caching was migrated. The agent measures instead: two requests, one counter, in a sandbox where a clean install of the new package is the only version present.

Old Flask applications are also awkward to run on a developer machine, since their pins date from a Python that is no longer installed. The sandbox is disposable, so the agent can build the environment the migration targets, hit each error in order, and keep every traceback in the run's record for the reviewer.

## Frequently asked questions

### Is Flask-Caching a drop-in replacement for Flask-Cache?

Nearly. The class and decorator names are the same, and the import becomes `from flask_caching import Cache`. The differences are in configuration: short backend names like `simple` are replaced by class names like `SimpleCache`, and a custom backend has to import its base class from the new package. The agent checks each of those.

### Why do I get "No module named 'flask.ext'"?

Flask 1.0 removed the `flask.ext` import redirect that old tutorials used. Every extension is now imported by its own package name, such as `flask_caching` or `flask_sqlalchemy`. If other `flask.ext` imports remain in your app, the agent lists them; this job changes only the cache one.

### Does the order of @app.route and @cache.cached matter?

Yes. `@app.route` must be the outer decorator, written above `@cache.cached`. In the other order Flask registers the undecorated function and the cache is never consulted, with no error. The agent checks every cached view for this and measures it with repeated requests.

### Will my cached data survive the migration?

With an in-process backend there is nothing to keep, since the cache empties on every restart. With Redis or Memcached, do not count on the new package reading entries the old one wrote; they expire on their own timeouts and are rebuilt on the next request. The agent keeps your key prefix and timeouts unchanged and says so in the description.

## More jobs like this: Upgrade dependencies weekly

- [Migrate From Flask-JWT to Flask-JWT-Extended](https://getrush.ai/run/migrate-flask-jwt-to-flask-jwt-extended/flask-jwt/)
- [Migrate From Flask-RESTPlus to Flask-RESTX](https://getrush.ai/run/migrate-flask-restplus-to-flask-restx/flask-restplus/)
- [Migrate From Flask-Script to the Flask CLI](https://getrush.ai/run/migrate-flask-script-to-flask-cli/flask-script/)
- [Migrate From garyburd/redigo to gomodule/redigo](https://getrush.ai/run/migrate-garyburd-redigo-to-gomodule-redigo/redigo/)
- [Migrate a Go Test Suite From Ginkgo v1 to Ginkgo v2](https://getrush.ai/run/migrate-ginkgo-v1-to-v2/ginkgo/)
- [Replace go-bindata With go:embed in a Go Web Server](https://getrush.ai/run/migrate-go-bindata-to-embed/go-bindata/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
