Source for file SqliteCache.php

Documentation is available at SqliteCache.php

  1. <?
  2.  
  3. /** A SQLite database cache.
  4.  *
  5.  * @package    php-lastfm-api
  6.  * @author  Felix Bruns <felixbruns@web.de>
  7.  * @version    1.0
  8.  */
  9. final class SqliteCache extends Cache {
  10.     /** The SQLite database handle.
  11.      *
  12.      * @var        string 
  13.      * @access    private
  14.      */
  15.     private $sqlite;
  16.  
  17.     /** Constructor that sets up the SqliteCache.
  18.      *
  19.      * @param    string    $database    The database file to use.
  20.      * @access    public
  21.      */
  22.     public function __construct($database){
  23.         parent::__construct();
  24.  
  25.         $this->sqlite sqlite_open($database);
  26.  
  27.         @sqlite_query($this->sqlite,
  28.             "CREATE TABLE cache (hash VARCAHR(40) PRIMARY KEY, xml TEXT, expiration INTEGER);"
  29.         );
  30.     }
  31.  
  32.     /** Destructor that closes the SQLite database.
  33.      *
  34.      * @access    public
  35.      */
  36.     public function __destruct(){
  37.         sqlite_close($this->sqlite);
  38.     }
  39.  
  40.     /** Checks if data associated with a hash exists in the cache.
  41.      *
  42.      * @param    string    $hash    The hash of the entry to be checked.
  43.      * @return    boolean            true if the entry exists, otherwise false.
  44.      * @access    public
  45.      */
  46.     public function contains($hash){
  47.         $result sqlite_query($this->sqlite,
  48.             "SELECT 1 FROM cache WHERE hash = '".sqlite_escape_string($hash)."'"
  49.         );
  50.  
  51.         return sqlite_num_rows($result0;
  52.     }
  53.  
  54.     /** Loads data from the cache.
  55.      *
  56.      * @param    string    $hash    The hash of the entry to be loaded.
  57.      * @return    string            The cached data.
  58.      * @access    public
  59.      */
  60.     public function load($hash){
  61.         $result sqlite_query($this->sqlite,
  62.             "SELECT xml FROM cache WHERE hash = '".sqlite_escape_string($hash)."'"
  63.         );
  64.  
  65.         return sqlite_fetch_string($result);
  66.     }
  67.  
  68.     /** Removes data from the cache.
  69.      *
  70.      * @param    string    $hash    The hash of the entry to be removed.
  71.      * @access    public
  72.      */
  73.     public function remove($hash){
  74.         sqlite_exec($this->sqlite,
  75.             "DELETE FROM cache WHERE hash = '".sqlite_escape_string($hash)."'"
  76.         );
  77.     }
  78.  
  79.     /** Stores data in the cache.
  80.      *
  81.      * @param    string    $hash        The hash of the data to be stored.
  82.      * @param    string    $data        The data to be stored.
  83.      * @param    string    $expiration    The expiration time of the data (unix timestamp).
  84.      * @access    public
  85.      */
  86.     public function store($hash$data$expiration){
  87.         sqlite_exec($this->sqlite,
  88.             "INSERT OR REPLACE INTO cache VALUES(
  89.             '".sqlite_escape_string($hash)."',
  90.             '".sqlite_escape_string($data)."',
  91.             '".intval($expiration)."'
  92.             )"
  93.         );
  94.     }
  95.  
  96.     /** Removes all data from the cache.
  97.      *
  98.      * @access    public
  99.      */
  100.     public function clear(){
  101.         sqlite_exec($this->sqlite"DELETE FROM cache");
  102.     }
  103.  
  104.     /** Checks if data associated with a hash is expired.
  105.      *
  106.      * @param    string    $hash    The hash of the entry to be checked.
  107.      * @return    boolean            true if the entry is expired, otherwise false.
  108.      * @access    public
  109.      */
  110.     public function isExpired($hash){
  111.         $result sqlite_query($this->sqlite,
  112.             "SELECT expiration FROM cache WHERE hash = '".sqlite_escape_string($hash)."'"
  113.         );
  114.  
  115.         $expiration sqlite_fetch_string($result);
  116.  
  117.         return (time(intval($expiration));
  118.     }
  119. }
  120.  
  121. ?>

Documentation generated on Mon, 22 Dec 2008 16:57:44 +0100 by phpDocumentor 1.4.1