mercredi 18 août 2021

Laravel Cache - Attempt to read from a closed MaxMind DB

I'm using Laravel 5.8 and MaxMind GeoLite2-City.mmdb together with Reader (GeoIp2\Database\Reader).

This is how it look like:

public function checkWithoutCache(Request $request) {
    
    $reader = new Reader(storage_path().'/app/ipToCountry/GeoLite2-City-v2.mmdb');      
    $r      = $reader->city($request->ip());
    $reader->close();
    return $r;
}

Everything working ok. Now because our server deals with tons of requests that for each request we are loading the Reader with the GeoLite2-City-v2.mmdb file (70MB), we wonder if it's smart to leave it like that or adding the Reader object to laravel Cache in order to load it once and then use the cache so we will improve performance.

So we were trying to use Laravel Cache like the next example:

public function checkWithCache(Request $request) {
    
    $reader = \Cache::remember('geo_city_instance', now()->addDays(30), function(){
        return new Reader(storage_path().'/app/ipToCountry/GeoLite2-City-v2.mmdb'); 
    });
    $r      = $reader->city($request->ip());
    $reader->close();
    return $r;
}

But we got the next Exception:

BadMethodCallException Attempt to read from a closed MaxMind DB ( …/vendor/maxmind-db/reader/src/MaxMind/Db/Reader.php158)

Does anyone have a solution on how to add it to cache without exception?

Or we don't need actually need to add it to cache?

Please help, thanks!



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3D0W4aa
via IFTTT

Aucun commentaire:

Enregistrer un commentaire