---
title: "Caching — Geolocation for Laravel | Roundly"
description: "Cache successful locate() and distance() results on any cache store to cut API costs and latency — failures are never cached."
url: https://roundly-consulting.com/open-source/docs/geolocation-for-laravel/caching
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Geolocation for Laravel](https://roundly-consulting.com/open-source/docs/geolocation-for-laravel.md)

# Caching

Turn on caching to make repeated lookups cheap and rate-limit friendly:

```bash
GEOLOCATION_CACHE=true
GEOLOCATION_CACHE_STORE=redis      # null = the default cache store
GEOLOCATION_CACHE_TTL=86400        # seconds
GEOLOCATION_CACHE_PREFIX=geolocation
```

- Successful locate() and distance() results are cached for cache.ttl seconds; null results are never cached.
- batch() items go through locate(), so each IP is cached individually. Distance matrices are not cached.
- Calls scoped with using() / provider() or carrying withToken() / withTimeout() / withConfig() overrides bypass the cache entirely.
- A cache hit returns immediately and dispatches no event.
- The default fallback is never cached: it only answered because the real providers did not, so the next call asks them again.

## Safe with any store

Results are stored as plain arrays and rebuilt on read, so the cache works even when your store refuses to unserialize classes (Laravel’s cache.serializable\_classes set to false). A payload the package doesn’t recognise — say, one written by an older version — is treated as a miss, never an exception.

## Forgetting and flushing

Drop one entry with forget() — it takes the same GeolocationQuery or DistanceQuery you looked up — or every entry at once with flushCache():

```php
use RoundlyConsulting\Geolocation\DataTransferObjects\DistanceQuery;
use RoundlyConsulting\Geolocation\DataTransferObjects\GeolocationQuery;
use RoundlyConsulting\Geolocation\Facades\Geolocation;

Geolocation::forget(GeolocationQuery::forIp('8.8.8.8'));   // true when an entry was removed
Geolocation::forget(DistanceQuery::between($from, $to));  // distances too

Geolocation::flushCache();                                 // every lookup and distance
```

flushCache() works on any cache store without touching other keys: cache keys carry a generation number ({prefix}:v{generation}:locate:{hash}) and a flush moves it forward, so older entries are never read again and expire on their own TTL. A lookup or distance already in flight during a flush stores its result under the old generation, so that result is never read either.

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/geolocation-for-laravel.md)
