PHP Classes

File: MySQLStorage.php

Recommend this page to a friend!
  Classes of Andrei Alexandru   Simple Cache System   MySQLStorage.php   Download  
File: MySQLStorage.php
Role: Example script
Content type: text/plain
Description: Example of an external storage
Class: Simple Cache System
Store and retrieve cached data in MySQL and files
Author: By
Last change:
Date: 7 years ago
Size: 2,516 bytes
 

Contents

Class file image Download
<?php

use Atto\Cache\Storage;
use
Atto\Cache\Item;

/**
 * Create this table in youre mysql / maria DB database
 *
 * CREATE TABLE `cache` (
 * `id` char(40) NOT NULL,
 * `item` text NOT NULL
 * ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 *
 *
 * Implementation of a MySQL storage for the cache system.
 *
 * @author Andrei
 *
 */
class MySQLStorage implements Storage {
   
   
/**
     * MySQLi Instance
     *
     * @var \mysqli
     */
   
protected $mysqli;
   
   
/**
     * Table name where all values are stored
     *
     * @var string
     */
   
protected $table;
   
   
/**
     * MySQLStorage constructor
     *
     * @param string $host
     * @param string $user
     * @param string $password
     * @param string $database
     * @param string $table
     */
   
public function __construct($host, $user, $password, $database, $table) {
       
       
// Connect to the database
       
$this->mysqli = new mysqli($host, $user, $password, $database);
       
       
// Check for any errors
       
if ($this->mysqli->connect_error) {
            die(
'Connection error (' . $this->mysqli->connect_errno . ') ' . $this->mysqli->connect_error);
        }
       
       
$this->table = $table;
    }

   
/**
     * Saves a new Item with the given key
     *
     * @param string $key
     * @param Item $item
     */
   
public function save($key, Item $item) {
       
$query = "INSERT INTO $this->table (id, item) VALUES (?, ?)";
       
       
$key = sha1($key);
       
$item = serialize($item);
       
       
$stmt = $this->mysqli->prepare($query);
       
$stmt->bind_param('ss', $key, $item);
       
$stmt->execute();
       
       
$stmt->close();
    }
   
   
/**
     * Removes the indicated Item from the storage
     *
     * @param string $key
     */
   
public function delete($key) {
       
$query = "DELETE FROM $this->table WHERE id = ?";
       
       
$key = sha1($key);
       
       
$stmt = $this->mysqli->prepare($query);
       
$stmt->bind_param('s', $key);
       
$stmt->execute();
       
       
$stmt->close();
    }
   
   
/**
     * Returns the Item associated with the given key
     *
     * @param string $key
     *
     * @return Item
     */
   
public function getItem($key) {
       
$query = "SELECT item FROM $this->table WHERE id = ?";
       
       
$key = sha1($key);
       
       
$stmt = $this->mysqli->prepare($query);
       
$stmt->bind_param('s', $key);
       
$stmt->execute();
       
$stmt->bind_result($item);
       
$stmt->fetch();
       
       
$stmt->close();
       
        return ! isset(
$item) ? null : unserialize($item);
    }
   
   
/**
     * Close the connection to the database
     */
   
public function __destruct() {
       
// Close mysqli connection
       
$this->mysqli->close();
    }
   
}