Skip to content

Add optional cache for IP locations.#409

Open
gonssal wants to merge 3 commits into
stevebauman:masterfrom
micasino:add-cache
Open

Add optional cache for IP locations.#409
gonssal wants to merge 3 commits into
stevebauman:masterfrom
micasino:add-cache

Conversation

@gonssal

@gonssal gonssal commented May 28, 2026

Copy link
Copy Markdown

This adds an optional cache for each IP's location, disabled by default.

New relevant config options are added for it, including LOCATION_CACHE to easily enable or disable it.

@stevebauman

Copy link
Copy Markdown
Owner

Thanks for the PR! Though I'm not sure if I want to add this into the package itself. Developers can cache this themselves in a few lines:

$ip = request()->ip();

$location = Cache::remember(
    "location:{$ip}", 
    now()->addHour(), 
    fn () => Location::get($ip)
);

@gonssal

gonssal commented May 28, 2026

Copy link
Copy Markdown
Author

Thanks for the PR! Though I'm not sure if I want to add this into the package itself. Developers can cache this themselves in a few lines:

$ip = request()->ip();

$location = Cache::remember(
    "location:{$ip}", 
    now()->addHour(), 
    fn () => Location::get($ip)
);

Hi there. Thanks for the reply.

We were using another library that had a built-in cache (using cache tags, which we learnt is very bad with the Redis backend), so I decided to implement it in yours too.

A couple of included nice things is that it ignores false or empty positions for you by default and crucially it adds the cached property to cached Positions, which you would have to extend or add dynamically without the property being defined, which won't be possible soon.

Of course it's up to you to add it, we will continue using our fork if you don't.

@gonssal

gonssal commented May 28, 2026

Copy link
Copy Markdown
Author

Sorry to ask it here, but I see you don't have discussions enabled and not sure if you'd like an issue open with feature proposals, so here it goes.

We also implemented a feature that lets you download the MaxMind database in any Laravel-defined storage disk, so for example you can download it to a cloud storage bucket with the location:update command. Because the geoip2 library requires the file to be local, each process then downloads the database from this storage and caches it locally.

This is basically necessary on highly distributed systems, because if you run the location:update on deploy for each server/instance, when you scale to hundreds or even thousands of them, you will quickly hit MaxMind's rate limits, specially if you deploy often.

This is all handled through config and env variables, and by default the current behaviour is untouched. I will say though, that I would strongly recommend you change the default path in the published config from using database_path() to using storage_path(), which is the appropriate place for those things, specially storage/app, which is git-ignored by default by Laravel.

If that's something you'd be interesed in merging, I can send a PR too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants