php - 提高 php get_browser() 性能

标签 php browscap

我们在 PHP 中使用 php_browscap.ini 使用 get_browser(),但性能很糟糕。我们将每个页面 100 个左右的用户代理传递到 get_browser() 中,渲染页面需要超过 30 秒的时间。我们需要一个高性能的解决方案,而不需要持久存储实际的 get_browser() 结果(我们只想存储用户代理)。

我们已经使用了 memcached,有没有办法可以改变 get_browser() 来缓存结果,或者将整个 php_browscap.ini 加载到 memcached 中。

最佳答案

最终推出了我们自己的解决方案:

    ////
    // 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;
    }

关于php - 提高 php get_browser() 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23024185/

相关文章:

php - 为 get_browser() 设置 $_SERVER ['HTTP_USER_AGENT' ]

cron - Browscap.ini 在加载 PHP 时抛出错误(命令行 - PHP_CLI)

php - fatal error : require_once(): Failed opening required

php - 如何用 PHP 编写 DICOM 应用程序

php - 如何在 PHP 中检查字符串是否以 "_"开头?

php - 在 web 目录外访问 PHP Browscap FastCGI

javascript - 在 jQuery 中访问 PHP $_SESSION 变量并执行错误处理和页面重定向

php - 保存 Canvas 图像(将数据字符串发布到 PHP)

php - 我应该如何设置 browscap.ini 文件