2

We are using get_browser() in PHP using php_browscap.ini but performance is horrible. We pass 100 or so user-agents into get_browser() per page and it takes over 30 seconds to render the page. We need a performant solution, without storing the actual get_browser() results persistently (we only want to store user agents).

We already use memcached, is there a way we can alter get_browser() to cache results, or load the entire php_browscap.ini into memcached.

Justin
  • 42,716
  • 77
  • 201
  • 296

3 Answers3

2

Ended up rolling our own solution:

    ////
    // This function caches in memcached.
    ////
    public static function get_browser_memcached($user_agent) {
        if(empty(MemcacheConnection::$memcache_connection)) {
            MemcacheConnection::connect();
        }

        $memcache_key = preg_replace('/\s+/', '', sha1($user_agent)) . "_user_agent";
        $memcache_result = MemcacheConnection::get($memcache_key);

        if($memcache_result !== false) {
            return $memcache_result;
        }

        $browser = get_browser($user_agent);

        //Store in Memcached (cached for 7 days)
        MemcacheConnection::set($memcache_key, $browser, 604800);
        return $browser;
    }
Justin
  • 42,716
  • 77
  • 201
  • 296
0

I havn't used the browscap-php library, but the the usage is highly recommend by the Browser Capabilities Project. http://browscap.org/

The libary on GitHub should improve the performance.

AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
0

I know I'm chiming in a little late, but for what it's worth, I'm using the browscap-php library (as mentioned by @AbcAeffchen) in one of my projects and I'm happy so far.

A typical lookup (from my own simple measurements) takes about 20~30ms on a 1 core 512MB cloud instance (which is pretty much the minimum you can find anywhere). I've opted to cache with Redis and this brings the lookup time down to just a couple of ms... so it's possible to optimize if you really need to.

The convenience alone makes it worth a try.

Petar Zivkovic
  • 970
  • 10
  • 20