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

# Cache Expensive Resources in Rules

> Cache expensive resources across Auth0 Rule executions using a persistent global object to avoid recreating them each run.

> [!WARNING]
>
> **Rules will reach end of life (EOL) on January 31, 2028.**
>
> * As of October 16, 2023, Rules are no longer available in newly created tenants.
> * As of November 18, 2024, Rules are read-only. Active Rules still run, and you can enable or disable them and change their configuration values and secrets. You can't edit their code in the Dashboard, the Management API, or tools that use it, such as the Auth0 CLI, Deploy CLI, Terraform, and Agent Skills.
> * **After January 31, 2028, Rules stop running and are removed.**

**Use Actions to extend Auth0:**

* To learn more about Actions, read [Understand How Auth0 Actions Work](/docs/docs/customize/actions/actions-overview).
* To migrate, read [Migrate from Rules to Actions](/docs/docs/customize/actions/migrate/migrate-from-rules-to-actions).

In Rules you can store expensive resources using the `global` object. The `global` object survives individual execution, so you can reuse things stored there instead of creating them every time a Rule is run. However, the Rules environment can be recycled at any time so your code **must** always check that `global` contains what you expect.

This example shows how to use the `global` object to keep a MongoDB connection:

```javascript lines theme={null}
//If the db object is there, use it.
if (global.db){
  return query(global.db, callback);
}

//If not, get the db (mongodb in this case)
mongo('mongodb://user:pass@mymongoserver.com/my-db',  function (db){
  global.db = db;
  return query(db, callback);
});

//Do the actual work
function query(db, cb){
  // Do something with db
  ...
  return cb(null, user, context);
}
```

To learn more about using MongoDB, read [the MongoDB Manual](https://docs.mongodb.com/manual/).
