Prefixing cache | laravel-multitenancy | Spatie

 SPATIE

  Laravel Multitenancy
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-multitenancy](https://spatie.be/docs/laravel-multitenancy/v2)  Using-tasks-to-prepare-the-environment  Prefixing cache

 Version   v4   v3   v2   v1

 Other versions for crawler [v4](https://spatie.be/docs/laravel-multitenancy/v4) [v3](https://spatie.be/docs/laravel-multitenancy/v3) [v2](https://spatie.be/docs/laravel-multitenancy/v2) [v1](https://spatie.be/docs/laravel-multitenancy/v1)

- [ Introduction ](https://spatie.be/docs/laravel-multitenancy/v2/introduction)
- [ Support us ](https://spatie.be/docs/laravel-multitenancy/v2/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-multitenancy/v2/requirements)
- [ Questions and issues ](https://spatie.be/docs/laravel-multitenancy/v2/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-multitenancy/v2/changelog)
- [ ](https://spatie.be/docs/laravel-multitenancy/v2/installation-setup)

Installation
------------

- [ Base installation ](https://spatie.be/docs/laravel-multitenancy/v2/installation/base-installation)
- [ Using a single database ](https://spatie.be/docs/laravel-multitenancy/v2/installation/using-a-single-database)
- [ Using multiple databases ](https://spatie.be/docs/laravel-multitenancy/v2/installation/using-multiple-databases)
- [ Determining the current tenant ](https://spatie.be/docs/laravel-multitenancy/v2/installation/determining-current-tenant)

Basic usage
-----------

- [ Automatically determining the current tenant ](https://spatie.be/docs/laravel-multitenancy/v2/basic-usage/automatically-determining-the-current-tenant)
- [ Working with the current tenant ](https://spatie.be/docs/laravel-multitenancy/v2/basic-usage/working-with-the-current-tenant)
- [ Making queues tenant aware ](https://spatie.be/docs/laravel-multitenancy/v2/basic-usage/making-queues-tenant-aware)

Advanced usage
--------------

- [ Ensuring a current tenant has been set ](https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/ensuring-a-current-tenant-has-been-set)
- [ Looping over a collection of tenants ](https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/looping-over-a-collection-of-tenants)
- [ Making Artisan command tenant aware ](https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/making-artisan-commands-tenant-aware)
- [ Using a custom tenant model ](https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/using-a-custom-tenant-model)
- [ Listening for events ](https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/listening-for-events)
- [ Using tenant specific facades ](https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/using-tenant-specific-facades)
- [ Executing code for tenants and landlords ](https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/executing-code-for-tenants-and-landlords)

Using tasks to prepare the environment
--------------------------------------

- [ Overview ](https://spatie.be/docs/laravel-multitenancy/v2/using-tasks-to-prepare-the-environment/overview)
- [ Creating your own task ](https://spatie.be/docs/laravel-multitenancy/v2/using-tasks-to-prepare-the-environment/creating-your-own-task)
- [ Switching databases ](https://spatie.be/docs/laravel-multitenancy/v2/using-tasks-to-prepare-the-environment/switching-databases)
- [ Switching route cache paths ](https://spatie.be/docs/laravel-multitenancy/v2/using-tasks-to-prepare-the-environment/switching-route-cache-paths)
- [ Prefixing cache ](https://spatie.be/docs/laravel-multitenancy/v2/using-tasks-to-prepare-the-environment/prefixing-cache)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-multitenancy                                                                                                                                                                                                                                    `

Prefixing cache
===============

You might want to use separate caches for each different tenant. The `Spatie\Multitenancy\Tasks\PrefixCacheTask` allows you to do just that. This task only works for memory based caches, such as APC and Redis.

To use this task, you should add it to the `switch_tenant_tasks` key in the `multitenancy` config file.

```
// in config/multitenancy.php

'switch_tenant_tasks' => [
    \Spatie\Multitenancy\Tasks\PrefixCacheTask::class,
    // other tasks
],
```

When this task is installed, cache will behave like this

```
cache()->put('key', 'original-value');

$tenant->makeCurrent();
cache('key') // returns null;
cache()->put('key', 'value-for-tenant');

$anotherTenant->makeCurrent();
cache('key') // returns null;
cache()->put('key', 'value-for-another-tenant');

Tenant::forgetCurrent();
cache('key') // returns 'original-value';

$tenant->makeCurrent();
cache('key') // returns 'value-for-tenant'

$anotherTenant->makeCurrent();
cache('key') // returns 'value-for-another-tenant'
```

Behind the scenes, this works by dynamically changing the `cache.prefix` in the `cache` config file whenever another tenant is made current.

If you want to make the cache tenant aware in another way, you should [create your own task](/docs/laravel-multitenancy/v2/using-tasks-to-prepare-the-environment/creating-your-own-task/). You can take a look at the source code of `PrefixCacheTask` for inspiration.
