Welcome, Guest. Please login or register.
Did you miss your activation email?
Jul. 04, 2009, 11:33:03 AM
50802 Posts in 11235 Topics by 6001 Members
Latest Member: CocoChanels
News: New to PRADO? The PRADO blog tutorial is a good start point.
 
The PRADO Community » Prado v3.x » Feature Requests » TDatabasePageStatePersister « previous next »
Pages: [1] Print
Author Topic: TDatabasePageStatePersister  (Read 4344 times)
intol
Senior Member
***

Karma: 6
Offline Offline

Posts: 155


View Profile WWW
« on: Sep. 17, 2007, 12:23:27 PM »

Hi! I'm already using my own class for PageStatePersister, and a performance of all request is now much higher (especially AJAX - about 1000% incrise)!

I must say, that TSessionPageStatePersister has many disadvantages (e.g. is unable to store pagestates for multiple pages by user).

I have no time for writing "ready for use" component, but maybe my code will help someone to write it.

Database shema:

Code:
CREATE TABLE `page_states` (
  `id` int(11) NOT NULL auto_increment,
  `timestamp` int(11) NOT NULL,
  `data` longtext collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `timestamp` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=867 ;

Class:

Code:
<?php

class UPageStatePersister extends TComponent implements IPageStatePersister
{
private $_page;

/**
 * @return TPage the page that this persister works for
 */
public function getPage()
{
return $this->_page;
}

/**
 * @param TPage the page that this persister works for
 */
public function setPage(TPage $page)
{
$this->_page=$page;
}

/**
 * Saves state in hidden fields.
 * @param mixed state to be stored
 */
public function save($state)
{
$db PRADO::getApplication()->getDB();

$dataAsString serialize($state);

$db->execute("INSERT INTO page_states(timestamp,data) VALUES(:timestamp,:data)",
array('timestamp' => time(), 'data' => $dataAsString));
$pageStateNumber $db->getLastInsertID();

$this->_page->setClientState($pageStateNumber);
}

/**
 * Loads page state from hidden fields.
 * @return mixed the restored state
 * @throws THttpException if page state is corrupted
 */
public function load()
{
$db PRADO::getApplication()->getDB();
$db->execute("DELETE FROM page_states WHERE timestamp < :timestamp",
array('timestamp' => (time()-60*60*2)));

$data null;

$pageStateNumber $this->_page->getRequestClientState();
if(strlen($pageStateNumber) > 0)
{
$dbResult $db->execute("SELECT data FROM page_states WHERE id=:id LIMIT 0,1",
array('id' =>$pageStateNumber));
if($dbResult->getRowCount() > 0)
{
$dbArray $dbResult->read();
$data unserialize($dbArray['data']);
}
}

if($data!==null)
return $data;
else
throw new THttpException(400,'pagestatepersister_pagestate_corrupted');
}
}

?>

Logged

rojaro
Global Moderator
Platinum Member
****

Karma: 27
Offline Offline

Posts: 514


PRADO aint no voodoo ...


View Profile WWW
« Reply #1 on: Sep. 17, 2007, 02:18:29 PM »

I don't think storing cache data in a database is such a good idea. I think it would be much faster and better if you would use a file system based implementation. But anyway, your implementation looks nice. Karma++ (btw ... what did you do to get a negative karma?)
Logged

A mathematician is a machine for turning coffee into theorems. ~ Alfred Renyi (*1921 - †1970)
intol
Senior Member
***

Karma: 6
Offline Offline

Posts: 155


View Profile WWW
« Reply #2 on: Sep. 17, 2007, 03:50:44 PM »

Any idea that reduce traffic between browser and server is good.

Take notice that I'm only sending page state ID between them.

Karma? I'm not interested with that.
Logged

Qiang
PRADO Team Leader
Administrator
Diamond Member
*****

Karma: 100
Offline Offline

Posts: 3231



View Profile
« Reply #3 on: Sep. 17, 2007, 06:21:26 PM »

Thanks for sharing your component.

Inspired by your work, I just wrote TCachePageStatePersister, which allows you to use cache as persistent medium for storing page state. It can be used together with any available cache module. Since we have TDbCache, this means the persister can also store data using (any) database.

Enjoy...
Logged
intol
Senior Member
***

Karma: 6
Offline Offline

Posts: 155


View Profile WWW
« Reply #4 on: Sep. 17, 2007, 06:34:20 PM »

In http://www.eioba.com we have done few things (like this above) that incrise PRADO applications performance very much.
Maybe, if there'll we enough time, I try to write some article about it, or present it on some conference.
Logged

Qiang
PRADO Team Leader
Administrator
Diamond Member
*****

Karma: 100
Offline Offline

Posts: 3231



View Profile
« Reply #5 on: Sep. 17, 2007, 06:43:03 PM »

That will be great! Could you share a little bit about the traffic information of your site?
Logged
intol
Senior Member
***

Karma: 6
Offline Offline

Posts: 155


View Profile WWW
« Reply #6 on: Sep. 17, 2007, 07:10:22 PM »

It's about 500.000 page views by month, but server can handle much higher traffic.
« Last Edit: Sep. 17, 2007, 08:47:04 PM by intol » Logged

FragMaster B
PRADO Supporter
Senior Member
*

Karma: 6
Offline Offline

Posts: 220



View Profile WWW
« Reply #7 on: Nov. 29, 2007, 07:32:46 PM »

I just attempted adding TCachePageStatePersister along with TDbCache via...

Code:
<modules>
...
<module id="MySqlCache" class="System.Caching.TDbCache"
AutoCreateCacheTable="false" CacheTableName="_pradocache"
ConnectionString="mysql:host=localhost;dbname=my_db_name_here"
Username="my_username_here"" Password="my_password_here"" />
</modules>

<services>
<service id="page" class="TPageService" DefaultPage="Home">
<pages
StatePersisterClass="System.Web.UI.TCachePageStatePersister"
StatePersister.CacheModuleID="MySqlCache"
MasterClass="Application.pages.Master"
Theme="sky" />
</service>
</services>

...

Some page consistently give me the following error. Did I miss a step somewhere?

Code:
[Notice] unserialize() [<a href='function.unserialize'>function.unserialize</a>]: Error at offset 11 of 65535 bytes (@line 124 in file D:\localhost\php\includes\prado-3.1.1\framework\Caching\TCache.php).
Logged
teo
PRADO Supporter
Senior Member
*

Karma: 11
Offline Offline

Posts: 197


View Profile WWW
« Reply #8 on: Dec. 18, 2007, 11:03:35 AM »

@FragMaster B: I had the same error with BLOB as field type in mysql table.Set it to LONGBLOB fixed the issue.

BTW using TDatabasePageStatePersister I noticed great speed improvements!!
Logged
FragMaster B
PRADO Supporter
Senior Member
*

Karma: 6
Offline Offline

Posts: 220



View Profile WWW
« Reply #9 on: Dec. 18, 2007, 02:14:05 PM »

@FragMaster B: I had the same error with BLOB as field type in mysql table.Set it to LONGBLOB fixed the issue.

BTW using TDatabasePageStatePersister I noticed great speed improvements!!


That DOES seem to work. Thanks a ton!

Can we get a note added to the API docs here about this?

Edit: Submitted a ticket here.
« Last Edit: Dec. 18, 2007, 09:01:42 PM by FragMaster B » Logged
FragMaster B
PRADO Supporter
Senior Member
*

Karma: 6
Offline Offline

Posts: 220



View Profile WWW
« Reply #10 on: Dec. 19, 2007, 08:52:54 PM »

Great stuff here, I just created my own implementation here. I'm amazed at the performance boost this enables.
« Last Edit: Dec. 20, 2007, 04:29:09 PM by FragMaster B » Logged
Pages: [1] Print 
« previous next »
Jump to: