JohndoeMAKT Skrevet 29. desember 2008 Del Skrevet 29. desember 2008 (endret) La til flushing av memcache i deleteEntry. Hva skal det være godt for ? 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 29. desember 2008 av JohndoeMAKT Lenke til kommentar
Epower Skrevet 29. desember 2008 Del Skrevet 29. desember 2008 Det var jo du som sa at det skulle flushes. Lenke til kommentar
JohndoeMAKT Skrevet 29. desember 2008 Del Skrevet 29. desember 2008 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. Lenke til kommentar
Epower Skrevet 1. januar 2009 Del Skrevet 1. januar 2009 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; } } } ?> Lenke til kommentar
JohndoeMAKT Skrevet 1. januar 2009 Del Skrevet 1. januar 2009 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; } } } ?> Lenke til kommentar
Alex Moran Skrevet 2. januar 2009 Del Skrevet 2. januar 2009 (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 2. januar 2009 av Josh Homme Lenke til kommentar
Epower Skrevet 2. januar 2009 Del Skrevet 2. januar 2009 Men det trengs vel ikke siden det står public function __construct(PDO $connection, Memcache $memcacheConnection) Lenke til kommentar
PS_CS4 Skrevet 2. januar 2009 Del Skrevet 2. januar 2009 Skal dere snart begynne med det vesentlige med en gjestebok? Altså lage formen, poste innlegget - og hente det ut igjen. Lenke til kommentar
Wackamole Skrevet 2. januar 2009 Del Skrevet 2. januar 2009 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... Lenke til kommentar
JohndoeMAKT Skrevet 2. januar 2009 Del Skrevet 2. januar 2009 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årpublic 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. Lenke til kommentar
Epower Skrevet 7. januar 2009 Del Skrevet 7. januar 2009 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; } } } Lenke til kommentar
JohndoeMAKT Skrevet 7. januar 2009 Del Skrevet 7. januar 2009 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; } } } Lenke til kommentar
Epower Skrevet 7. januar 2009 Del Skrevet 7. januar 2009 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; } } } Lenke til kommentar
JohndoeMAKT Skrevet 8. januar 2009 Del Skrevet 8. januar 2009 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. Lenke til kommentar
Epower Skrevet 8. januar 2009 Del Skrevet 8. januar 2009 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; } } } Lenke til kommentar
JohndoeMAKT Skrevet 8. januar 2009 Del Skrevet 8. januar 2009 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. Lenke til kommentar
Epower Skrevet 8. januar 2009 Del Skrevet 8. januar 2009 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; } } } Lenke til kommentar
JohndoeMAKT Skrevet 9. januar 2009 Del Skrevet 9. januar 2009 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; } } } Lenke til kommentar
Epower Skrevet 9. januar 2009 Del Skrevet 9. januar 2009 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; } } } Lenke til kommentar
JohndoeMAKT Skrevet 9. januar 2009 Del Skrevet 9. januar 2009 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; } } } Lenke til kommentar
Anbefalte innlegg
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 kontoLogg inn
Har du allerede en konto? Logg inn her.
Logg inn nå