Skip to content

Commit

Permalink
[OPTIMIZATION] Caching of parsed format_comment() output in details.p…
Browse files Browse the repository at this point in the history
…hp and commenttable()

Version change
  • Loading branch information
yunasc committed Mar 20, 2014
1 parent 2e15dff commit ac1d2b3
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 7 deletions.
5 changes: 5 additions & 0 deletions comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@

$newid = mysql_insert_id();

sql_query('INSERT INTO comments_parsed (cid, text_hash, text_parsed) VALUES ('.implode(', ', array_map('sqlesc', array($newid, md5($text), format_comment($text)))).')') or sqlerr(__FILE__,__LINE__);

sql_query("UPDATE torrents SET comments = comments + 1 WHERE id = $torrentid");

/////////////////ÑËÅÆÅÍÈÅ ÇÀ ÊÎÌÌÅÍÒÀÌÈ/////////////////
Expand Down Expand Up @@ -188,12 +190,15 @@
if ($text == "")
stderr($tracker_lang['error'], $tracker_lang['comment_cant_be_empty']);

$orig_text = $text;
$text = sqlesc($text);

$editedat = sqlesc(get_date_time());

sql_query("UPDATE comments SET text=$text, editedat=$editedat, editedby=$CURUSER[id] WHERE id=$commentid") or sqlerr(__FILE__, __LINE__);

sql_query('REPLACE INTO comments_parsed (cid, text_hash, text_parsed) VALUES ('.implode(', ', array_map('sqlesc', array($commentid, md5($orig_text), format_comment($orig_text)))).')') or sqlerr(__FILE__,__LINE__);

if ($returnto)
header("Location: $returnto");
else
Expand Down
17 changes: 12 additions & 5 deletions details.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function dltable($name, $arr, $torrent) {
if (!isset($id) || !$id)
die();

$res = sql_query("SELECT t.multitracker, t.last_mt_update, t.keywords, t.description, t.free, t.seeders, t.banned, t.leechers, t.info_hash, t.filename, UNIX_TIMESTAMP() - UNIX_TIMESTAMP(t.last_action) AS lastseed, t.numratings, t.name, IF(t.numratings < $minvotes, NULL, ROUND(t.ratingsum / t.numratings, 1)) AS rating, t.owner, t.save_as, t.descr, t.visible, t.size, t.added, t.views, t.hits, t.times_completed, t.id, t.type, t.numfiles, t.image1, t.image2, t.image3, t.image4, t.image5, c.name AS cat_name, u.username FROM torrents AS t LEFT JOIN categories AS c ON t.category = c.id LEFT JOIN users AS u ON t.owner = u.id WHERE t.id = $id") or sqlerr(__FILE__, __LINE__);
$res = sql_query("SELECT td.descr_hash, td.descr_parsed, t.multitracker, t.last_mt_update, t.keywords, t.description, t.free, t.seeders, t.banned, t.leechers, t.info_hash, t.filename, UNIX_TIMESTAMP() - UNIX_TIMESTAMP(t.last_action) AS lastseed, t.numratings, t.name, IF(t.numratings < $minvotes, NULL, ROUND(t.ratingsum / t.numratings, 1)) AS rating, t.owner, t.save_as, t.descr, t.visible, t.size, t.added, t.views, t.hits, t.times_completed, t.id, t.type, t.numfiles, t.image1, t.image2, t.image3, t.image4, t.image5, c.name AS cat_name, u.username FROM torrents AS t LEFT JOIN categories AS c ON t.category = c.id LEFT JOIN users AS u ON t.owner = u.id LEFT JOIN torrents_descr AS td ON td.tid = $id WHERE t.id = $id") or sqlerr(__FILE__, __LINE__);
$row = mysql_fetch_array($res);

$keywords = $row['keywords'];
Expand Down Expand Up @@ -291,8 +291,15 @@ function hex_esc($matches) {
tr($tracker_lang['details_poster'], $img1, 1);
}

if (!empty($row["descr"]))
tr($tracker_lang['description'], format_comment($row["descr"]), 1, 1);
if (!empty($row["descr"])) {
if (md5($row['descr']) == $row['descr_hash'])
$descr = $row['descr_parsed'];
else {
$descr = format_comment($row['descr']);
sql_query('INSERT INTO torrents_descr (tid, descr_hash, descr_parsed) VALUES ('.implode(', ', array_map('sqlesc', array($id, md5($row['descr']), $descr))).')') or sqlerr(__FILE__,__LINE__);
}
tr($tracker_lang['description'], $descr, 1, 1);
}

$images = array();

Expand Down Expand Up @@ -600,8 +607,8 @@ function send() {
} else {
list($pagertop, $pagerbottom, $limit) = pager($limited, $count, "details.php?id=$id&", array('lastpagedefault' => 1));

$subres = sql_query("SELECT c.id, c.ip, c.text, c.user, c.added, c.editedby, c.editedat, u.avatar, u.warned, ".
"u.username, u.title, u.class, u.donor, u.downloaded, u.uploaded, u.gender, u.last_access, e.username AS editedbyname FROM comments AS c LEFT JOIN users AS u ON c.user = u.id LEFT JOIN users AS e ON c.editedby = e.id WHERE torrent = " .
$subres = sql_query("SELECT cp.text_hash, cp.text_parsed, c.id, c.ip, c.text, c.user, c.added, c.editedby, c.editedat, u.avatar, u.warned, ".
"u.username, u.title, u.class, u.donor, u.downloaded, u.uploaded, u.gender, u.last_access, e.username AS editedbyname FROM comments AS c LEFT JOIN users AS u ON c.user = u.id LEFT JOIN users AS e ON c.editedby = e.id LEFT JOIN comments_parsed AS cp ON cp.cid = c.id WHERE torrent = " .
"$id ORDER BY c.id $limit") or sqlerr(__FILE__, __LINE__);
$allrows = array();
while ($subrow = mysql_fetch_array($subres))
Expand Down
2 changes: 1 addition & 1 deletion include/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ function magnet($html = true, $info_hash, $name, $size, $announces = array()) {

//  ýòîé ñòðîêå çàáèò êîïèðàéò. Ïðè åãî óáèðàíèè ìîæåøü ïîïëàòèòüñÿ ðàáî÷èì òðåêåðîì ;)  äàííîì ñëó÷àå - óáèðàÿ ñòðî÷êè íèæå òû íå ñìîæåøü èñïîëüçîâàòü òðåêåð.
define ('VERSION', '');
define ('NUM_VERSION', '2.1.16');
define ('NUM_VERSION', '2.1.17');
define ('TBVERSION', 'Powered by <a href="http://www.tbdev.net" target="_blank" style="cursor: help;" title="Áåñïëàòíàÿ OpenSource áàçà" class="copyright">TBDev</a> v'.NUM_VERSION.' <a href="http://bit-torrent.kiev.ua" target="_blank" style="cursor: help;" title="Ñàéò ðàçðàáîò÷èêà äâèæêà" class="copyright">Yuna Scatari Edition</a> '.VERSION.' Copyright &copy; 2001-'.date('Y'));

function mysql_modified_rows () {
Expand Down
8 changes: 7 additions & 1 deletion include/functions_commenttable.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ function commenttable($rows, $redaktor = "comment") {

$avatar = ($CURUSER["avatars"] == "yes" ? htmlspecialchars_uni($row["avatar"]) : "");
if (!$avatar){$avatar = "pic/default_avatar.gif"; }
$text = format_comment($row["text"]);

if (md5($row['text']) == $row['text_hash'])
$text = $row['text_parsed'];
else {
$text = format_comment($row['text']);
sql_query('INSERT INTO comments_parsed (cid, text_hash, text_parsed) VALUES ('.implode(', ', array_map('sqlesc', array($row['id'], md5($row['text']), $text))).')') or sqlerr(__FILE__,__LINE__);
}

if ($row["editedby"]) {
//$res = mysql_fetch_assoc(sql_query("SELECT * FROM users WHERE id = $row[editedby]")) or sqlerr(__FILE__,__LINE__);
Expand Down
26 changes: 26 additions & 0 deletions install/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ CREATE TABLE `comments` (
KEY `torrent` (`torrent`)
) ENGINE=MyISAM;

#
# Structure for the `comments_parsed` table :
#

DROP TABLE IF EXISTS `comments_parsed`;

CREATE TABLE `comments_parsed` (
`cid` int(10) unsigned NOT NULL DEFAULT '0',
`text_hash` varchar(32) NOT NULL DEFAULT '',
`text_parsed` text NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=MyISAM;

#
# Structure for the `countries` table :
#
Expand Down Expand Up @@ -577,6 +590,19 @@ CREATE TABLE `torrents` (
KEY `vnsi` (`visible`, `not_sticky`, `id`)
) ENGINE=MyISAM;

#
# Structure for the `torrents_descr` table :
#

DROP TABLE IF EXISTS `torrents_descr`;

CREATE TABLE `torrents_descr` (
`tid` int(10) unsigned NOT NULL DEFAULT '0',
`descr_hash` varchar(32) NOT NULL DEFAULT '',
`descr_parsed` text NOT NULL,
PRIMARY KEY (`tid`)
) ENGINE=MyISAM

#
# Structure for the `torrents_scrape` table :
#
Expand Down
3 changes: 3 additions & 0 deletions takeedit.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,11 @@ function dict_get($d, $k, $t) {
bark("Âû äîëæíû ââåñòè îïèñàíèå!");

$updateset[] = "name = " . sqlesc($name);

$updateset[] = "descr = " . sqlesc($descr);
$updateset[] = "ori_descr = " . sqlesc($descr);
sql_query('REPLACE INTO torrents_descr (tid, descr_hash, descr_parsed) VALUES ('.implode(', ', array_map('sqlesc', array($id, md5($descr), format_comment($descr)))).')') or sqlerr(__FILE__,__LINE__);

$updateset[] = "category = " . (0 + $type);
if (get_user_class() >= UC_ADMINISTRATOR) {
if ($_POST["banned"]) {
Expand Down
3 changes: 3 additions & 0 deletions takeupload.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ function bark($msg) {
bark("mysql puked: ".mysql_error());
}
$id = mysql_insert_id();

sql_query('INSERT INTO torrents_descr (tid, descr_hash, descr_parsed) VALUES ('.implode(', ', array_map('sqlesc', array($id, md5($descr), format_comment($descr)))).')') or sqlerr(__FILE__,__LINE__);

sql_query("INSERT INTO checkcomm (checkid, userid, torrent) VALUES ($id, $CURUSER[id], 1)") or sqlerr(__FILE__,__LINE__);
sql_query("DELETE FROM files WHERE torrent = $id");
foreach ($filelist as $file) {
Expand Down
17 changes: 17 additions & 0 deletions upgrade/from_2.1.16_2.1.17.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# SQL script to update from 2.1.16 version to 2.1.17
#

CREATE TABLE `comments_parsed` (
`cid` int(10) unsigned NOT NULL DEFAULT '0',
`text_hash` varchar(32) NOT NULL DEFAULT '',
`text_parsed` text NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=MyISAM;

CREATE TABLE `torrents_descr` (
`tid` int(10) unsigned NOT NULL DEFAULT '0',
`descr_hash` varchar(32) NOT NULL DEFAULT '',
`descr_parsed` text NOT NULL,
PRIMARY KEY (`tid`)
) ENGINE=MyISAM;

0 comments on commit ac1d2b3

Please sign in to comment.