Gå til innhold

Sammen skal vi lage: Gjestebok


Anbefalte innlegg

Skrevet (endret)
La til flushing av memcache i deleteEntry.

 

Hva skal det være godt for ? :omg:

En smule enig med PS_CS4 her. Flush invaliderer all cache lagret i serverinstansen av MC. Med andre ord, har du et pool på et par 2-3 GB - alt borte. Med andre ord ikke akkurat ønsket oppførsel. Skal du slette én nøkkel sletter du selvsagt én nøkkel i stedet for å potensielt sende hele serveren inn i swap heaven fordi all cache plutselig må gjenoppbygges.

 

 

<?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->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 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
Videoannonse
Annonse
Skrevet

Oops, det gjorde jeg, men jeg mente ikke flushing av all cache med ->flush(), men bare cachen brukt av dette systemet. Jeg tar den på min kappe, jeg burde forklart bedre når det gjelder MC som ikke er så mye brukt.

Skrevet

La til editEntry()

 

 

<?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->delete( 'gb-entries' );
		}
	}
}

public function editEntry($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;
	}
}
}

?>

 

Skrevet

Jeg endret litt i konstruktøren angående MC.

 

<?php

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

public function __construct(PDO $connection, Memcache $memcacheConnection) {
	if ( get_class( $memcacheConnection ) === 'Memcache' ) {}

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

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->delete( 'gb-entries' );
		}
	}
}

public function editEntry($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;
	}
}
}

?>

 

Skrevet (endret)

Er en del penere å skrive if ($memcacheConnection instanceof Memcache) :)

Hva var tanken bak?

 

<?php

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

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

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

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->delete( 'gb-entries' );
		}
	}
}

public function editEntry($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;
	}
}
}

 

Endret av Josh Homme
Skrevet

Serriøst PS_CS4, det er da absolutt mer vesentlig og lage backend før man i hele tatt tenker på input feltene og frontend generelt, det er det man tar HEELT til slutt...

Skrevet
Er en del penere å skrive if ($memcacheConnection instanceof Memcache) :)

Hva var tanken bak?

Ingen tanke i det hele tatt, jeg bruker til vanlig et rammeverk som tar seg av cache, enten til MC om tilgjengelig eller fallback til flatfil.

 

Men det trengs vel ikke siden det står

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

Jeg er usikker på hvor streng PHP er på signaturer, men dersom klassen initialiseres med NULL som andre parameter er den ikke instanceof Memcache.

 

Skal dere snart begynne med det vesentlige med en gjestebok? Altså lage formen, poste innlegget - og hente det ut igjen.

Som sagt er det det minst vesentlige av alt, og for å være helt ærlig syntes jeg det er utenfor denne oppgavens scope. Det er M i MVC-strukturen som kan holdes ren og gjenbrukbar, HTML-koden vil nok de fleste omskrive.

Skrevet

Sånn da:

 

<?php

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

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

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

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->delete( 'gb-entries' );
		}
	}
}

public function editEntry($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;
	}
}
}

 

Skrevet

Flyttet $this->memcacheConnection = $memcacheConnection; inn i if-blokken.

 

<?php

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

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

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->delete( 'gb-entries' );
		}
	}
}

public function editEntry($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;
	}
}
}

 

Skrevet

Vi burde vel sjekke om vi faktisk er koblet til en memcache server

 

<?php

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

public function __construct(PDO $connection, $memcacheConnection) {
	if (($memcacheConnection instanceof Memcache) and $memcacheConnection->getStats() !== false) {
		$this->memcacheConnection = $memcacheConnection;
		$this->mcConnected = true;
	}
	$this->connection = $connection;
}

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->delete( 'gb-entries' );
		}
	}
}

public function editEntry($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;
	}
}
}

 

Skrevet
Vi burde vel sjekke om vi faktisk er koblet til en memcache server

Akkurat det er jeg uenig i. Det er desverre ingen god måte jeg vet om for å finne ut om en MC-tilkoblet er tilkoblet og klar, og det å kalle getStats og bare kaste resultatet er IMO feil bruk av metoden og bortkastede ressurser. Jeg vet ikke hvor tung getStats er, men potensielt er den mye tyngre enn get/set, og jeg syntes derfor det er et dårlig valg.

Skrevet

Hva bruker vi da?

 

set('gb-entries', '')

 

 

<?php

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

public function __construct(PDO $connection, $memcacheConnection) {
	if (($memcacheConnection instanceof Memcache) and $memcacheConnection->set('gb-entries', '') !== false) {
		$this->memcacheConnection = $memcacheConnection;
		$this->mcConnected = true;
	}
	$this->connection = $connection;
}

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->delete( 'gb-entries' );
		}
	}
}

public function editEntry($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;
	}
}
}

 

Skrevet
Hva bruker vi da?

Ingenting. Anta at dersom du får et objekt av type Memcache er den korrekt tilkoblet, og at dersom det var en feil i oppkoblingen ville ikke programmeren ha sendt med det objektet.

Skrevet

Ok.

 

La til en if i editEntry

 

<?php

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

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

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->delete( 'gb-entries' );
		}
	}
}

public function editEntry($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;
	}
}
}

 

Skrevet

La til den obligatoriske if mcConnected i editEntry.

 

<?php

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

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

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->delete( 'gb-entries' );
		}
	}
}

public function editEntry($id) {
	if(is_int($id) && $id > 0) {
		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;
	}
}
}

 

Skrevet

La til sql i editEntry

 

<?php

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

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

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->delete( 'gb-entries' );
		}
	}
}

public function editEntry($id) {
	if(is_int($id) && $id > 0) {
		$sql = "UPDATE guestbook SET message=? WHERE 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;
	}
}
}

 

Skrevet

La til sletting av MC i editEntry.

 

<?php

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

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

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->delete( 'gb-entries' );
		}
	}
}

public function editEntry($id) {
	if(is_int($id) && $id > 0) {
		$sql = "UPDATE guestbook SET message=? WHERE id=?";
		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 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;
	}
}
}

 

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...