Jump to content

Caching fsockopen result


cirma

Recommended Posts

Hi,

 

I'm using fsockopen() to call a web service.  The service simply looks up a surfer's IP address and returns their city with whatever accuracy.  We get charged per query...so if a surfer refreshes the page 100 times, we get charged 100 times.

 

Don't ask me why we're not using another solution not involving a web service, I didn't make that decision :)

 

Anyway, I need to prevent that from happening but still allow the surfer to refresh the page 100 times.  So what I want to do is cache the surfer's IP address and city after the lookup so that we only ever get charged once per IP within a given day.  I was thinking of simply calling the webservice, storing the IP and city name in a mysql database, then every time a surfer visits the page, checking to see if we have the IP stored to begin with.  If not, call the web service, otherwise, retrieve it from the db.

 

Is there a more elegant solution than this?  We'll be doing about 9-10,000 lookups per day, and I'm not sure if there's a way to cache the responses with less performance hit, like not by using mysql?

 

Thanks.

Link to comment
Share on other sites

try something like this:

 

set a session variable, that once set flags the system to not re-check the IP:

<?php
session_start();
if (!isset($_SESSION['ip_checked']) || $_SESSION['ip_checked'] != true){
//run the check
}

Now, on the script that does the check with fsockopen()

<?php
session_start();
if (!isset($_SESSION['ip_check']) || $_SESSION['ip_check'] != true){
// check them
$_SESSION['ip_check'] = true;
$_SESSION['location'] = $LOCATION_FOUND_FROM_WEB_SERVICE;
}

Complete pseudocode, but it should point you in the right direction.

Link to comment
Share on other sites

While sessions will work, and possibly suffice for your application, they will only be good for as long as the user's browser session is open.  So, if that user visits the site 5 times in a day, closing their browser window/tab in between each visit, you get charged 5 times for that user.

 

Consider actual caching using Cache Lite.  It's very, very simple to setup and you can set cache limits for however long you want per user.  I use it all the time for any API/cURL calls I make to avoid unnecessary external calls being made.  Call once, display 100 times.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.