title: Zend Framework 2 - Using Memcache/Memcached to store DB results
body: <p>I tried to implement memcache to store Db results (within tablegateway), but I have a problem. Without "setItem" and "getItem" methods, the query works fine, but if I use them, it displays this error:</p>
  
  <pre><code>An error occurred
  
  An error occurred during execution; please try again later.
  Additional information:
  PDOException
  File:
  C:\Program Files (x86)\xampp\htdocs\ZF-Tutorial\vendor\zendframework\zendframework\library\Zend\Serializer\Adapter\PhpSerialize.php:48
  Message:
  You cannot serialize or unserialize PDOStatement instances
  </code></pre>
  
  <p>Inside my tablegateway there is a </p>
  
  <pre><code>namespace Application\Model;
  
  use Zend\Cache\Storage\StorageInterface;
  use Zend\Db\TableGateway\TableGateway;
  use Zend\Db\Sql\Sql;
  use Application\Model\Defaultlanguage;
  
  class AccessoriesTable 
  {
      protected $tableGateway;
      protected $cache;
  
  public function __construct(TableGateway $tableGateway)
  {
      $this-&gt;tableGateway = $tableGateway;
  }
  
  
  public function setCache(StorageInterface $cache)
  {
      $this-&gt;cache = $cache;
  } 
  
  
  public function fetchAll($lang = null)
  {
      if(!isset($lang))  {  
          $lang = DefaultLanguage::LANG;
      }
  
      if( ($result = $this-&gt;cache-&gt;getItem('testcache')) == FALSE) {
          $adapter = $this-&gt;tableGateway-&gt;getAdapter();
          $sql = new Sql($adapter);
          $select = $sql-&gt;select();
          $select-&gt;columns(array(
              'id',
              'accessories' =&gt; $lang
          ));
          $select-&gt;from('cat_accessories'); 
          $select-&gt;order('accessories ASC');
          $statement = $sql-&gt;prepareStatementForSqlObject($select);
          $result = $statement-&gt;execute();
  
          $this-&gt;cache-&gt;setItem('testcache',  $result);
      }
      return $result; 
  }
  </code></pre>
  
  <p>inside my module.php</p>
  
  <pre><code>'Application\Model\AccessoriesTable' =&gt;  function($sm) {
      $tableGateway = $sm-&gt;get('AccessoriesTableGateway');
      $cacheAdapter = $sm-&gt;get('Zend\Cache\Storage\Memcache');
      $table = new AccessoriesTable($tableGateway);
      $table-&gt;setCache($cacheAdapter);
      return $table;
  },
  'AccessoriesTableGateway' =&gt; function ($sm) {
      $dbAdapter = $sm-&gt;get('Zend\Db\Adapter\Adapter');
      $resultSetPrototype = new ResultSet();
      $resultSetPrototype-&gt;setArrayObjectPrototype(new Accessories());
      return new TableGateway('cat_accessories', $dbAdapter, null, $resultSetPrototype);
  },
  </code></pre>
  
  <p>I tried to use memcache inside my controller and view and works fine. I don't understood where is(are) the error(s). Thanks you all for the help.</p>
  
  <p><strong>EDIT 28-06-2015</strong></p>
  
  <p>I found a solution here, works fine but I don't like so much:
  <a href="https://samsonasik.wordpress.com/2012/09/27/zend-framework-2-using-zendcache-and-hydratingresultset-to-save-database-resultset/" rel="nofollow">https://samsonasik.wordpress.com/2012/09/27/zend-framework-2-using-zendcache-and-hydratingresultset-to-save-database-resultset/</a></p>
  
  <p>inside module.php</p>
  
  <pre><code>'Application\Model\AccessoriesTable' =&gt;  function($sm) {
      $dbAdapter    = $sm-&gt;get('Zend\Db\Adapter\Adapter');
      $cacheAdapter = $sm-&gt;get('Zend\Cache\Storage\Memcache');
      $table = new AccessoriesTable($dbAdapter);
      $table-&gt;setCache($cacheAdapter);
      return $table;
  },
  </code></pre>
  
  <p>Inside Accessories Class:</p>
  
  <pre><code>namespace Application\Model;
  
  class Accessories 
  {
      public $id;
      public $accessories;
  
      public function exchangeArray($data)
      {
          $this-&gt;id     = (!empty($data['id'])) ? $data['id'] : null;
          $this-&gt;accessories     = (!empty($data['accessories'])) ? $data['accessories'] : null;
      }   
  
      public function getArrayCopy()
      {
          return get_object_vars($this);
      }
  }
  </code></pre>
  
  <p>Inside AccessoriesTable:</p>
  
  <pre><code>namespace Application\Model;
  
  use Zend\Db\Adapter\Adapter;
  use Zend\Db\ResultSet\HydratingResultSet;
  use Zend\Db\TableGateway\AbstractTableGateway;
  use Zend\Db\Sql\Select;
  use Zend\Cache\Storage\StorageInterface;
  use Application\Model\Accessories;
  use Application\Model\Defaultlanguage;
  
  class AccessoriesTable extends AbstractTableGateway
  {
      protected $table = 'cat_accessories';
      protected $cache;
      public $lang;
  
      public function __construct(Adapter $adapter)
      {
          $this-&gt;adapter = $adapter;
          $this-&gt;resultSetPrototype = new HydratingResultSet();
          $this-&gt;resultSetPrototype-&gt;setObjectPrototype(new Accessories());
          $this-&gt;initialize();
      }
  
      public function fetchAll($lang = null)
      {
          $this-&gt;setLang($lang);
          $cacheName = md5('accessories-'.$this-&gt;lang);
          if( ($resultSet = $this-&gt;cache-&gt;getItem($cacheName)) == FALSE) {
              $resultSet = $this-&gt;select(function (Select $select){
                  $select-&gt;columns(array('id', 'accessories'=&gt; $this-&gt;lang ));
                  $select-&gt;order(array($this-&gt;lang. ' asc'));  
              });
              $this-&gt;cache-&gt;setItem($cacheName ,  $resultSet );
          }
          return $resultSet; 
      }
  }
  </code></pre>
  
  <p><strong>NEW QUESTION</strong> : how can I implement it with "traditional" tablegateway construct????</p>
  
  <p>I tried to use traditional tablegateway but the error is the same above.</p>
  
  <p>inside tablegateway:</p>
  
  <pre><code>public function fetchAll($lang = null)
  {
      $this-&gt;setLang($lang);
      $cacheName = md5('accessories-'.$this-&gt;lang);
      if( ($resultSet = $this-&gt;cache-&gt;getItem($cacheName)) == FALSE) {
          $resultSet = $this-&gt;tableGateway-&gt;select(function (Select $select){
              $select-&gt;columns(array('id', 'accessories'=&gt; $this-&gt;lang ));
              $select-&gt;order(array($this-&gt;lang. ' asc'));  
          });
          $this-&gt;cache-&gt;setItem($cacheName ,  $resultSet );
      }
      return $resultSet; 
  }
  </code></pre>