PHP Zend 与 AWS ElastiCache

标签 php mysql zend-framework amazon-web-services amazon-elasticache

您好,我有一个带有 php zend 框架的应用程序,我在我的 zend 应用程序上设置了一个 AWS ElastiCache

Bootstrap.php:

protected function _initMemcache()
{
    if (extension_loaded('memcache'))
    {
        // Configure caching backend strategy
        $cacheBackend = new Zend_Cache_Backend_Memcached(
            array(
                'servers' => array(
                    array(
                        'host' => 'xxxx.yyyy.qqq.rrr.cache.amazonaws.com',
                        'port' => '11211'
                    )
                    // Other servers here
                ),
                'compression' => true,
                'compatibility' => true
            )
        );

        // Configure caching frontend strategy
        $cacheFrontend = new Zend_Cache_Core(
            array(
                'caching' => true,
                'cache_id_prefix' => 'MyApp_',
                'write_control' => true,
                'automatic_serialization' => true,
                'ignore_user_abort' => true
            )
        );

        // Build a caching object
        $memcache = Zend_Cache::factory($cacheFrontend, $cacheBackend);

        Zend_Registry::set('cache', $memcache);
    } else {
        // Handle a non-existent extension here
    }
}

在我的 Controller 上:

public function fetchfrontAction()
{
    $cache = Zend_Registry::get('cache');

    $catid = $this->_getParam('catId');

    $business = new Application_Model_Mapper_BusinessMapper();
    $businessReviewMapper = new Application_Model_Mapper_BusinessReviewsMapper();
    $count = 5;
    if (!$result = $cache->load('dataset')) {

        $result = $business->getFetchByCategory($catid,$count);

        for ($i = 0; $i < count($result); $i++) {

            $result[$i]['business_name_url'] =  $result[$i]['business_url'];
            $userBusinessReviews = $businessReviewMapper->getBusinessReview($result[$i]['business_id'],1);

            if(!is_null($userBusinessReviews)){
                $result[$i]['user_business_reviews'] = $userBusinessReviews;
            }
            $userImg = $this->view->getLoginUserImage(
                $result[$i]['user_business_reviews'][0]['social_id'],
                $result[$i]['user_business_reviews'][0]['login_type'],null,null,square);
            if (!is_null($userImg)) {
                $result[$i]['user_img'] = $userImg;
            }
        }
        $cache->save($result,'dataset');
    }

    $this->_helper->json($result);
}

现在的问题是它没有任何错误地工作,我如何测试它是否真的从 AWS 缓存?有没有一种方法可以比较使用和不使用缓存的时间?谢谢

最佳答案

最简单的方法是在 if (!$result = $cache->load()) 上放置一个 else 子句并使用 Zend_Log 记录缓存命中。

此外,我想 AWS 会为您提供一些管理控制台来检查/管理缓存的内容/事件。但只是在那个特定点上大声思考。

关于PHP Zend 与 AWS ElastiCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15399862/

相关文章:

php - 需要所有已知国家的州缩写

Docker 中的 MySQL 和卷

mysql - MySQL 中的更改数据捕获

zend-framework - Zend 搜索 Lucene 和波斯语!

php - Zend Framework Frontcontroller/dispatcher 背后的想法是什么

javascript - Zend_Dojo_Form 元素无法正常工作

php - site_url() 和 base_url() 有什么区别?

php - 将图像定位在页脚的特定位置

php - 验证在 php 中生成的 nodejs 中的密码哈希

php - 向 PDO/MySql 语句添加辅助 WHERE 语句