1

嗨,我有PHP Zend框架的應用程序,我建立一個AWS ElastiCache作爲跟隨我的Zend應用的PHP 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 
    } 
} 

而且我控制器上:

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緩存?有沒有一種方法可以比較使用緩存和不使用緩存多久?謝謝

回答

0

最簡單的事情是將else子句放在if (!$result = $cache->load())上並使用Zend_Log來記錄緩存命中。

此外,我想AWS給你一些管理控制檯來檢查/管理緩存的內容/活動。但只是在那個特定點上大聲思考。