Torann / laravel-geoip

Determine the geographical location of website visitors based on their IP addresses.
http://lyften.com/projects/laravel-geoip
BSD 2-Clause "Simplified" License
2.11k stars 372 forks source link

Allow caching #31

Closed tremby closed 8 years ago

tremby commented 9 years ago

Allow caching via the built-in Laravel cache, with options for how long to store.

tremby commented 9 years ago

I overrode the GeoIP class for now (and had to override the ServiceProvider too to get my extension to load). This is the GeoIP override, in case it helps anyone:

<?php
namespace x\y\z;

use Cache;
use Carbon\Carbon;
use Torann\GeoIP\GeoIP as BaseClass;

class GeoIP extends BaseClass
{
    /**
     * @inheritDoc
     *
     * The result is cached for one week.
     */
    public function getLocation($ip = null)
    {
        if ($ip !== null) {
            $cacheKey = "geoip:$ip";
            if (Cache::has($cacheKey)) {
                return Cache::get($cacheKey);
            }
        }
        $result = parent::getLocation($ip);
        if ($ip !== null) {
            Cache::put($cacheKey, $result, Carbon::MINUTES_PER_HOUR * CARBON::HOURS_PER_DAY * Carbon::DAYS_PER_WEEK);
        }
        return $result;
    }
}
Torann commented 8 years ago

In v1.0 you can extend the GeoIP service to allow for your custom caching. I only see this as useful for if you were using a remote service to look up locations, otherwise it really isn't useful for anything I use this package for.

tremby commented 8 years ago

Yes, it's for when using a remote service. Is that not one of the services this package supports?

It seems to me that caching responses (and it's the Laravel cache being used by a Laravel package, so I don't really see that it's "custom") would be a feature useful to almost everybody who uses the remote service.