Skip to content

Instantly share code, notes, and snippets.

@gherkins
Created May 14, 2015 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gherkins/2491b7db2cd2d206d29b to your computer and use it in GitHub Desktop.
Save gherkins/2491b7db2cd2d206d29b to your computer and use it in GitHub Desktop.
cache identical queries
<?php
/**
* Class CachedMySQLDatabase
* @inheritdoc
*/
class CachedMySQLDatabase extends MySQLDatabase
{
protected $cache = array();
public function query($sql, $errorLevel = E_USER_ERROR)
{
$cacheKey = crc32($sql);
if (isset($this->cache[$cacheKey])) {
return $this->cache[$cacheKey];
}
$res = parent::query($sql, $errorLevel = E_USER_ERROR);
$this->cache[$cacheKey] = $res;
return $res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment