Gå til innhold

Sammen skal vi lage: Gjestebok


Anbefalte innlegg

La til innskriving av ucachet resultat i MC.

 

<?php

class Guestbook {
private $numEntriesPerPage = 10;
private $connection;
private $entries;
private $memcacheConnection;
private $mcConnected;

public function __construct(PDO $connection, $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}

	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
		$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
	}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO entries (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
			$stmt = $this->connection->prepare($sql);
			$stmt->execute(array(
			   $this->entries[$i]->author,
			   $this->entries[$i]->email,
			   $this->entries[$i]->website,
			   $this->entries[$i]->message,
			   $_SERVER['REMOTE_ADDR']
			));
		}
	}
}

}

class GuestbookEntry {
public $id;
public $author;
public $mail;
public $website;
public $message;
public $date;
public $ip;

public function __set($key, $value) {
	if (isset($this->$key)) {
		$this->$key = $value;
	}
}
}

?>

 

Lenke til kommentar
Videoannonse
Annonse

La til cache i addEntry

 

 

<?php

class Guestbook {
private $numEntriesPerPage = 10;
private $connection;
private $entries;
private $memcacheConnection;
private $mcConnected;

public function __construct(PDO $connection, $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
	if ( $this->mcConnected ) { }
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}

	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
		$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
	}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO entries (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
			$stmt = $this->connection->prepare($sql);
			$stmt->execute(array(
			   $this->entries[$i]->author,
			   $this->entries[$i]->email,
			   $this->entries[$i]->website,
			   $this->entries[$i]->message,
			   $_SERVER['REMOTE_ADDR']
			));
		}
	}
}

}

class GuestbookEntry {
public $id;
public $author;
public $mail;
public $website;
public $message;
public $date;
public $ip;

public function __set($key, $value) {
	if (isset($this->$key)) {
		$this->$key = $value;
	}
}
}

?>

 

Lenke til kommentar

Spørsmålet er om cache skal slettes eller oppfriskes når en ny oppføring kommer inn. Jeg stemmer sterkt for sletting.

 

Jeg horet opp en SQL-spørring.

 

<?php

class Guestbook {
private $numEntriesPerPage = 10;
private $connection;
private $entries;
private $memcacheConnection;
private $mcConnected;

public function __construct(PDO $connection, $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
	if ( $this->mcConnected ) { }
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}
	$sql = "SELECT /*SQL_CACHE*/ id, author, email, website, message, created FROM guestbook WHERE !deleted ORDER BY created DESC;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
		$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
	}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO entries (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
			$stmt = $this->connection->prepare($sql);
			$stmt->execute(array(
			   $this->entries[$i]->author,
			   $this->entries[$i]->email,
			   $this->entries[$i]->website,
			   $this->entries[$i]->message,
			   $_SERVER['REMOTE_ADDR']
			));
		}
	}
}

}

class GuestbookEntry {
public $id;
public $author;
public $mail;
public $website;
public $message;
public $date;
public $ip;

public function __set($key, $value) {
	if (isset($this->$key)) {
		$this->$key = $value;
	}
}
}

?>

 

Lenke til kommentar

La til sletting av memcache i addEntry

 

<?php

class Guestbook {
private $numEntriesPerPage = 10;
private $connection;
private $entries;
private $memcacheConnection;
private $mcConnected;

public function __construct(PDO $connection, $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
	if ( $this->mcConnected ) {
		$this->memcacheConnection->delete('gb-entries');
	}
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}
	$sql = "SELECT /*SQL_CACHE*/ id, author, email, website, message, created FROM guestbook WHERE !deleted ORDER BY created DESC;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
		$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
	}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO entries (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
			$stmt = $this->connection->prepare($sql);
			$stmt->execute(array(
			   $this->entries[$i]->author,
			   $this->entries[$i]->email,
			   $this->entries[$i]->website,
			   $this->entries[$i]->message,
			   $_SERVER['REMOTE_ADDR']
			));
		}
	}
}

}

class GuestbookEntry {
public $id;
public $author;
public $mail;
public $website;
public $message;
public $date;
public $ip;

public function __set($key, $value) {
	if (isset($this->$key)) {
		$this->$key = $value;
	}
}
}

?>

 

Endret av Eirikkkkkk
Lenke til kommentar

La til metode deleteEntry().

 

<?php

class Guestbook {
private $numEntriesPerPage = 10;
private $connection;
private $entries;
private $memcacheConnection;
private $mcConnected;

public function __construct(PDO $connection, $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
	if ( $this->mcConnected ) {
		$this->memcacheConnection->delete('gb-entries');
	}
}

public function deleteEntry( $id ) {}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}
	$sql = "SELECT /*SQL_CACHE*/ id, author, email, website, message, created FROM guestbook WHERE !deleted ORDER BY created DESC;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
		$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
	}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO entries (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
			$stmt = $this->connection->prepare($sql);
			$stmt->execute(array(
			   $this->entries[$i]->author,
			   $this->entries[$i]->email,
			   $this->entries[$i]->website,
			   $this->entries[$i]->message,
			   $_SERVER['REMOTE_ADDR']
			));
		}
	}
}

}

class GuestbookEntry {
public $id;
public $author;
public $mail;
public $website;
public $message;
public $date;
public $ip;

public function __set($key, $value) {
	if (isset($this->$key)) {
		$this->$key = $value;
	}
}
}

?>

 

Lenke til kommentar

Fiksa noen små feil

 

<?php

class Guestbook {
private $numEntriesPerPage = 10;
private $connection;
private $entries;
private $memcacheConnection;
private $mcConnected;

public function __construct(PDO $connection, Memcache $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
	if ( $this->mcConnected ) {
		$this->memcacheConnection->delete('gb-entries');
	}
}

public function deleteEntry( $id ) {}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}
	$sql = "SELECT /*SQL_CACHE*/ id, author, email, website, message, created FROM guestbook WHERE !deleted ORDER BY created DESC;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
		$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
	}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO guestbook (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
			$stmt = $this->connection->prepare($sql);
			$stmt->execute(array(
			   $this->entries[$i]->author,
			   $this->entries[$i]->email,
			   $this->entries[$i]->website,
			   $this->entries[$i]->message,
			   $_SERVER['REMOTE_ADDR']
			));
		}
	}
}

}

class GuestbookEntry {
public $id;
public $author;
public $email;
public $website;
public $message;
public $date;
public $ip;

public function __set($key, $value) {
	if (isset($this->$key)) {
		$this->$key = $value;
	}
}
}

?>

 

Lenke til kommentar

Dersom du mener //-kommentarer er jeg helt uenig. Inlinekommentarer er ikke nødvendig for noe så banalt som.. hva er det igjen vi lager?

 

La til en if i deleteEntry();

 

<?php

class Guestbook {
private $numEntriesPerPage = 10;
private $connection;
private $entries;
private $memcacheConnection;
private $mcConnected;

public function __construct(PDO $connection, Memcache $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
	if ( $this->mcConnected ) {
		$this->memcacheConnection->delete('gb-entries');
	}
}

public function deleteEntry( $id ) {
	if ( is_int( $id ) && $id > 0 ) {}
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}
	$sql = "SELECT /*SQL_CACHE*/ id, author, email, website, message, created FROM guestbook WHERE !deleted ORDER BY created DESC;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
		$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
	}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO guestbook (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
			$stmt = $this->connection->prepare($sql);
			$stmt->execute(array(
			   $this->entries[$i]->author,
			   $this->entries[$i]->email,
			   $this->entries[$i]->website,
			   $this->entries[$i]->message,
			   $_SERVER['REMOTE_ADDR']
			));
		}
	}
}

}

class GuestbookEntry {
public $id;
public $author;
public $email;
public $website;
public $message;
public $date;
public $ip;

public function __set($key, $value) {
	if (isset($this->$key)) {
		$this->$key = $value;
	}
}
}

?>

 

Endret av JohndoeMAKT
Lenke til kommentar

Prøvde meg på en sql i deleteEntry

 

<?php

class Guestbook {
private $numEntriesPerPage = 10;
private $connection;
private $entries;
private $memcacheConnection;
private $mcConnected;

public function __construct(PDO $connection, Memcache $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
	if ( $this->mcConnected ) {
		$this->memcacheConnection->delete('gb-entries');
	}
}

public function deleteEntry( $id ) {
	if ( is_int( $id ) && $id > 0 ) {
		$sql = "DELETE FROM guestbook WHERE id='?'";
	}
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}
	$sql = "SELECT /*SQL_CACHE*/ id, author, email, website, message, created FROM guestbook WHERE !deleted ORDER BY created DESC;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
		$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
	}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO guestbook (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
			$stmt = $this->connection->prepare($sql);
			$stmt->execute(array(
			   $this->entries[$i]->author,
			   $this->entries[$i]->email,
			   $this->entries[$i]->website,
			   $this->entries[$i]->message,
			   $_SERVER['REMOTE_ADDR']
			));
		}
	}
}

}

class GuestbookEntry {
public $id;
public $author;
public $email;
public $website;
public $message;
public $date;
public $ip;

public function __set($key, $value) {
	if (isset($this->$key)) {
		$this->$key = $value;
	}
}
}

?>

 

Lenke til kommentar

Ryddet litt i koden :grin:

 

 

<?php

class Guestbook {


private $numEntriesPerPage = 10;

private $connection;

private $entries;

private $memcacheConnection;

private $mcConnected;


	public function __construct(PDO $connection, Memcache $memcacheConnection) {

		$this->connection = $connection;
		$this->memcacheConnection = $memcacheConnection;

	}


	public function addEntry(GuestbookEntry $entry) {

		$this->entries[] = $entry;

			if ( $this->mcConnected ) {
				$this->memcacheConnection->delete('gb-entries');
			}

	}


	public function deleteEntry( $id ) {

		if ( is_int( $id ) && $id > 0 ) {
			$sql = "DELETE FROM guestbook WHERE id='?'";
		}

	}


	public function fetchEntries(array $options = array()) {

		if ( $this->mcConnected ) {

			$entries = $this->memcacheConnection->get( 'gb-entries' );

				if ( $entries !== false ) {
					return unserialize( $entries );
				}

		}

		$sql = "SELECT /*SQL_CACHE*/ id, author, email, website, message, created FROM guestbook WHERE !deleted ORDER BY created DESC;";
		$stmt = $this->connection->prepare($sql);
		$stmt->execute();
		$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');

			if ( $this->mcConnected ) {
				$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
			}

	}


	public function __destruct () {

		for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {

			if ( empty ( $this -> entries[$i] -> id ) ) {

				$sql = "INSERT INTO guestbook (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
				$stmt = $this->connection->prepare($sql);
				$stmt->execute(array(
					$this->entries[$i]->author,
					$this->entries[$i]->email,
					$this->entries[$i]->website,
					$this->entries[$i]->message,
					$_SERVER['REMOTE_ADDR']
				));

			}

		}

	}


}


class GuestbookEntry {


public $id;

public $author;

public $email;

public $website;

public $message;

public $date;

public $ip;


	public function __set($key, $value) {

		if (isset($this->$key)) {
			$this->$key = $value;
		}

	}


}

?>

 

Lenke til kommentar

Jeg er vanligvis en som liker mye luft i koden, men ærlig talt - linjebreak mellom klassevariablene? Hvorfor i all verden indeterer du alle funksjonene i klassen et hakk mer enn vanlig? Og hvorfor to linjer mellom starten på en klasse og første linje i den klassen?

Endret av Jonas
Lenke til kommentar

Fortsatte litt med deleteEntry, jeg :)

 

<?php

class Guestbook {
private $numEntriesPerPage = 10;
private $connection;
private $entries;
private $memcacheConnection;
private $mcConnected;

public function __construct(PDO $connection, Memcache $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
	if ( $this->mcConnected ) {
		$this->memcacheConnection->delete('gb-entries');
	}
}

public function deleteEntry( $id ) {
	if ( is_int( $id ) && $id > 0 ) {
		$sql = "DELETE FROM guestbook WHERE id='?'";
		$stmt = $this->connection->prepare($sql);
	}
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}
	$sql = "SELECT /*SQL_CACHE*/ id, author, email, website, message, created FROM guestbook WHERE !deleted ORDER BY created DESC;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
		$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
	}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO guestbook (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
			$stmt = $this->connection->prepare($sql);
			$stmt->execute(array(
			   $this->entries[$i]->author,
			   $this->entries[$i]->email,
			   $this->entries[$i]->website,
			   $this->entries[$i]->message,
			   $_SERVER['REMOTE_ADDR']
			));
		}
	}
}

}

class GuestbookEntry {
public $id;
public $author;
public $email;
public $website;
public $message;
public $date;
public $ip;

public function __set($key, $value) {
	if (isset($this->$key)) {
		$this->$key = $value;
	}
}
}

?>

 

Lenke til kommentar

Ok, tenkte ikke over det..

 

Sånn da:

 

<?php

class Guestbook {
private $numEntriesPerPage = 10;
private $connection;
private $entries;
private $memcacheConnection;
private $mcConnected;

public function __construct(PDO $connection, Memcache $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
	if ( $this->mcConnected ) {
		$this->memcacheConnection->delete('gb-entries');
	}
}

public function deleteEntry( $id ) {
	if ( is_int( $id ) && $id > 0 ) {
		$sql = "UPDATE guestbook SET deleted='1' WHERE id='?'";
		$stmt = $this->connection->prepare($sql);
	}
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}
	$sql = "SELECT /*SQL_CACHE*/ id, author, email, website, message, created FROM guestbook WHERE !deleted ORDER BY created DESC;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
		$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
	}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO guestbook (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
			$stmt = $this->connection->prepare($sql);
			$stmt->execute(array(
			   $this->entries[$i]->author,
			   $this->entries[$i]->email,
			   $this->entries[$i]->website,
			   $this->entries[$i]->message,
			   $_SERVER['REMOTE_ADDR']
			));
		}
	}
}

}

class GuestbookEntry {
public $id;
public $author;
public $email;
public $website;
public $message;
public $date;
public $ip;

public function __set($key, $value) {
	if (isset($this->$key)) {
		$this->$key = $value;
	}
}
}

?>

 

Lenke til kommentar

deleted er et date_time felt med vilje.

 

<?php

class Guestbook {
private $numEntriesPerPage = 10;
private $connection;
private $entries;
private $memcacheConnection;
private $mcConnected;

public function __construct(PDO $connection, Memcache $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
	if ( $this->mcConnected ) {
		$this->memcacheConnection->delete('gb-entries');
	}
}

public function deleteEntry( $id ) {
	if ( is_int( $id ) && $id > 0 ) {
		$sql = "UPDATE guestbook SET deleted = NOW() WHERE id=?";
		$stmt = $this->connection->prepare($sql);
	}
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}
	$sql = "SELECT /*SQL_CACHE*/ id, author, email, website, message, created FROM guestbook WHERE !deleted ORDER BY created DESC;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
		$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
	}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO guestbook (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
			$stmt = $this->connection->prepare($sql);
			$stmt->execute(array(
			   $this->entries[$i]->author,
			   $this->entries[$i]->email,
			   $this->entries[$i]->website,
			   $this->entries[$i]->message,
			   $_SERVER['REMOTE_ADDR']
			));
		}
	}
}

}

class GuestbookEntry {
public $id;
public $author;
public $email;
public $website;
public $message;
public $date;
public $ip;

public function __set($key, $value) {
	if (isset($this->$key)) {
		$this->$key = $value;
	}
}
}

?>

 

Lenke til kommentar

:blush:

 

 

<?php

class Guestbook {
private $numEntriesPerPage = 10;
private $connection;
private $entries;
private $memcacheConnection;
private $mcConnected;

public function __construct(PDO $connection, Memcache $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
	if ( $this->mcConnected ) {
		$this->memcacheConnection->delete('gb-entries');
	}
}

public function deleteEntry( $id ) {
	if ( is_int( $id ) && $id > 0 ) {
		$sql = "UPDATE guestbook SET deleted = NOW() WHERE id=?";
		$stmt = $this->connection->prepare($sql);
		$stmt->execute(array($id));
	}
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}
	$sql = "SELECT /*SQL_CACHE*/ id, author, email, website, message, created FROM guestbook WHERE !deleted ORDER BY created DESC;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
		$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
	}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO guestbook (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
			$stmt = $this->connection->prepare($sql);
			$stmt->execute(array(
			   $this->entries[$i]->author,
			   $this->entries[$i]->email,
			   $this->entries[$i]->website,
			   $this->entries[$i]->message,
			   $_SERVER['REMOTE_ADDR']
			));
		}
	}
}

}

class GuestbookEntry {
public $id;
public $author;
public $email;
public $website;
public $message;
public $date;
public $ip;

public function __set($key, $value) {
	if (isset($this->$key)) {
		$this->$key = $value;
	}
}
}

?>

 

Lenke til kommentar

La til en blokk for flushing av cache i deleteEntry.

 

 

<?php

class Guestbook {
private $numEntriesPerPage = 10;
private $connection;
private $entries;
private $memcacheConnection;
private $mcConnected;

public function __construct(PDO $connection, Memcache $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
	if ( $this->mcConnected ) {
		$this->memcacheConnection->delete('gb-entries');
	}
}

public function deleteEntry( $id ) {
	if ( is_int( $id ) && $id > 0 ) {
		$sql = "UPDATE guestbook SET deleted = NOW() WHERE id=?";
		$stmt = $this->connection->prepare($sql);
		$stmt->execute(array($id));

		if ( $this->mcConnected ) {}
	}
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}
	$sql = "SELECT /*SQL_CACHE*/ id, author, email, website, message, created FROM guestbook WHERE !deleted ORDER BY created DESC;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
		$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
	}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO guestbook (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
			$stmt = $this->connection->prepare($sql);
			$stmt->execute(array(
			   $this->entries[$i]->author,
			   $this->entries[$i]->email,
			   $this->entries[$i]->website,
			   $this->entries[$i]->message,
			   $_SERVER['REMOTE_ADDR']
			));
		}
	}
}

}

class GuestbookEntry {
public $id;
public $author;
public $email;
public $website;
public $message;
public $date;
public $ip;

public function __set($key, $value) {
	if (isset($this->$key)) {
		$this->$key = $value;
	}
}
}

?>

 

Lenke til kommentar

La til flushing av memcache i deleteEntry.

 

 

<?php

class Guestbook {
private $numEntriesPerPage = 10;
private $connection;
private $entries;
private $memcacheConnection;
private $mcConnected;

public function __construct(PDO $connection, Memcache $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
	if ( $this->mcConnected ) {
		$this->memcacheConnection->delete('gb-entries');
	}
}

public function deleteEntry( $id ) {
	if ( is_int( $id ) && $id > 0 ) {
		$sql = "UPDATE guestbook SET deleted = NOW() WHERE id=?";
		$stmt = $this->connection->prepare($sql);
		$stmt->execute(array($id));

		if ( $this->mcConnected ) {
			$this->memcacheConnection->flush();
		}
	}
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}
	$sql = "SELECT /*SQL_CACHE*/ id, author, email, website, message, created FROM guestbook WHERE !deleted ORDER BY created DESC;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
		$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
	}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO guestbook (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
			$stmt = $this->connection->prepare($sql);
			$stmt->execute(array(
			   $this->entries[$i]->author,
			   $this->entries[$i]->email,
			   $this->entries[$i]->website,
			   $this->entries[$i]->message,
			   $_SERVER['REMOTE_ADDR']
			));
		}
	}
}

}

class GuestbookEntry {
public $id;
public $author;
public $email;
public $website;
public $message;
public $date;
public $ip;

public function __set($key, $value) {
	if (isset($this->$key)) {
		$this->$key = $value;
	}
}
}

?>

 

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...