Guest User

Webservice Smartping DatabaseCache

a guest
Aug 26th, 2015
165
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. /**
  10.  * @author VincentBab vincentbab@gmail.com
  11.  */
  12. class DatabaseCache implements Cache
  13. {
  14.     private $connection;
  15.     private $tableName;
  16.  
  17.     public function __construct(Connection $connection, $tableName)
  18.     {
  19.         $this->connection = $connection;
  20.         $this->tableName = $tableName;
  21.  
  22.         if (!$this->connection->getSchemaManager()->tablesExist($this->tableName)) {
  23.             $table = new Table($tableName);
  24.             $table->addColumn('id', 'string');
  25.             $table->addColumn('data', 'text');
  26.             $table->addColumn('expire', 'integer');
  27.             $table->setPrimaryKey(array('id'));
  28.             $table->addIndex(array('expire'));
  29.  
  30.             $this->connection->getSchemaManager()->createTable($table);
  31.         }
  32.     }
  33.  
  34.     public function fetch($id)
  35.     {
  36.         $result = $this->doFetch($id);
  37.  
  38.         if ($result) {
  39.             return unserialize($result['data']);
  40.         }
  41.  
  42.         return false;
  43.     }
  44.  
  45.     public function contains($id)
  46.     {
  47.         $result = $this->doFetch($id);
  48.  
  49.         return (bool)$result;
  50.     }
  51.  
  52.     public function save($id, $data, $lifeTime = 0)
  53.     {
  54.         $expire = time() + $lifeTime;
  55.         $data = serialize($data);
  56.  
  57.         $this->connection->beginTransaction();
  58.         $this->connection->setTransactionIsolation(Connection::TRANSACTION_SERIALIZABLE);
  59.         try {
  60.             $this->connection->delete($this->tableName, array('id' => $id));
  61.  
  62.             $this->connection->insert($this->tableName, array(
  63.                 'id' => $id,
  64.                 'data' => $data,
  65.                 'expire' => $expire,
  66.             ));
  67.  
  68.             $this->connection->commit();
  69.         } catch(\Exception $e) {
  70.             $this->connection->rollback();
  71.  
  72.             return false;
  73.         }
  74.  
  75.         return true;
  76.     }
  77.  
  78.     public function delete($id)
  79.     {
  80.         $this->connection->delete($this->tableName, array('id' => $id));
  81.  
  82.         return true;
  83.     }
  84.  
  85.     public function getStats()
  86.     {
  87.         return array(
  88.             Cache::STATS_HITS               => null,
  89.             Cache::STATS_MISSES             => null,
  90.             Cache::STATS_UPTIME             => null,
  91.             Cache::STATS_MEMORY_USAGE       => null,
  92.             Cache::STATS_MEMORY_AVAILABLE   => null,
  93.         );
  94.     }
  95.  
  96.     private function doFetch($id)
  97.     {
  98.         $result = $this->connection->fetchAll("SELECT * FROM {$this->tableName} WHERE id = :id AND expire > :expire", array(
  99.             'id' => $id,
  100.             'expire' => time(),
  101.         ));
  102.  
  103.         return isset($result[0]) ? $result[0] : false;
  104.     }
  105. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×