Guest User

DatabaseCache (Doctrine\Common\Cache\Cache)

a guest
May 6th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.69 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Mping\CoreBundle\Cache;
  4.  
  5. use Doctrine\DBAL\Schema\Table;
  6. use Doctrine\Common\Cache\Cache;
  7. use Doctrine\DBAL\Connection;
  8.  
  9. class DatabaseCache implements Cache
  10. {
  11.     private $connection;
  12.     private $tableName;
  13.  
  14.     public function __construct(Connection $connection, $tableName)
  15.     {
  16.         $this->connection = $connection;
  17.         $this->tableName = $tableName;
  18.  
  19.         if (!$this->connection->getSchemaManager()->tablesExist($this->tableName)) {
  20.             $table = new Table($tableName);
  21.             $table->addColumn('id', 'string');
  22.             $table->addColumn('data', 'text');
  23.             $table->addColumn('expire', 'integer');
  24.             $table->setPrimaryKey(array('id'));
  25.             $table->addIndex(array('expire'));
  26.  
  27.             $this->connection->getSchemaManager()->createTable($table);
  28.         }
  29.     }
  30.  
  31.     public function fetch($id)
  32.     {
  33.         $result = $this->doFetch($id);
  34.  
  35.         if ($result) {
  36.             return unserialize($result['data']);
  37.         }
  38.  
  39.         return false;
  40.     }
  41.  
  42.     public function contains($id)
  43.     {
  44.         $result = $this->doFetch($id);
  45.  
  46.         return (bool)$result;
  47.     }
  48.  
  49.     public function save($id, $data, $lifeTime = 0)
  50.     {
  51.         $expire = time() + $lifeTime;
  52.         $data = serialize($data);
  53.  
  54.         $this->connection->beginTransaction();
  55.         $this->connection->setTransactionIsolation(Connection::TRANSACTION_SERIALIZABLE);
  56.         try {
  57.             $this->connection->delete($this->tableName, array('id' => $id));
  58.  
  59.             $this->connection->insert($this->tableName, array(
  60.                 'id' => $id,
  61.                 'data' => $data,
  62.                 'expire' => $expire,
  63.             ));
  64.  
  65.             $this->connection->commit();
  66.         } catch(\Exception $e) {
  67.             $this->connection->rollback();
  68.  
  69.             return false;
  70.         }
  71.  
  72.         return true;
  73.     }
  74.  
  75.     public function delete($id)
  76.     {
  77.         $this->connection->delete($this->tableName, array('id' => $id));
  78.  
  79.         return true;
  80.     }
  81.  
  82.     public function getStats()
  83.     {
  84.         return array(
  85.             Cache::STATS_HITS               => null,
  86.             Cache::STATS_MISSES             => null,
  87.             Cache::STATS_UPTIME             => null,
  88.             Cache::STATS_MEMORY_USAGE       => null,
  89.             Cache::STATS_MEMORY_AVAILABLE   => null,
  90.         );
  91.     }
  92.  
  93.     private function doFetch($id)
  94.     {
  95.         $result = $this->connection->fetchAll("SELECT * FROM {$this->tableName} WHERE id = :id AND expire > :expire", array(
  96.             'id' => $id,
  97.             'expire' => time(),
  98.         ));
  99.  
  100.         return isset($result[0]) ? $result[0] : false;
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment