TCachePageStatePersister class
TCachePageStatePersister implements a page state persistent method based on cache. Page state are stored in cache (e.g. memcache, DB cache, etc.), and only a small token is passed to the client side to identify the state. This greatly reduces the size of the page state that needs to be transmitted between the server and the client. Of course, this is at the cost of using server side resource.
A cache module has to be loaded in order to use TCachePageStatePersister. By default, TCachePageStatePersister will use the primary cache module. A non-primary cache module can be used by setting CacheModuleID. Any cache module, as long as it implements the interface ICache, may be used. For example, one can use TDbCache, TMemCache, TAPCCache, etc.
TCachePageStatePersister uses CacheTimeout to limit the data that stores in cache.
Since server resource is often limited, be cautious if you plan to use TCachePageStatePersister for high-traffic Web pages. You may consider using a small CacheTimeout.
There are a couple of ways to use TCachePageStatePersister. One can override the page's TPage::getStatePersister() method and create a TCachePageStatePersister instance there. Or one can configure the pages to use TCachePageStatePersister in page configurations as follows,
- <pages StatePersisterClass="System.Web.UI.TCachePageStatePersister"
- StatePersister.CacheModuleID="mycache"
- StatePersister.CacheTimeout="3600" />
Note in the above, we use StatePersister.CacheModuleID to configure the cache module ID for the TCachePageStatePersister instance.
The above configuration will affect the pages under the directory containing this configuration and all its subdirectories. To configure individual pages to use TCachePageStatePersister, use
- <pages>
- <page id="PageID" StatePersisterClass="System.Web.UI.TCachePageStatePersister" />
- </pages>
Method Details |
calculateKey
protected string calculateKey |
(string $timestamp ) |
Input |
string | $timestamp | micro timestamp when saving state occurs |
Output |
string
| a key that is unique per user request |
Exception |
|
getCache
Output |
ICache
| the cache module being used for data storage |
Exception |
|
getCacheModuleID
public string getCacheModuleID |
() |
Output |
string
| the ID of the cache module. |
Exception |
|
getCacheTimeout
public integer getCacheTimeout |
() |
Output |
integer
| the number of seconds in which the cached state will expire. Defaults to 1800. |
Exception |
|
getKeyPrefix
public string getKeyPrefix |
() |
Output |
string
| prefix of cache variable name to avoid conflict with other cache data. Defaults to 'statepersister'. |
Exception |
|
getPage
public void getPage |
(TPage 0 ) |
Input |
TPage | 0 | the page that this persister works for |
Output |
Exception |
|
load
Loads page state from cache.
Output |
mixed
| the restored state |
Exception |
throws | THttpException if page state is corrupted |
|
save
public void save |
(mixed $state ) |
Saves state in cache.
Input |
mixed | $state | state to be stored |
Output |
Exception |
|
setCacheModuleID
public void setCacheModuleID |
(string $value ) |
Input |
string | $value | the ID of the cache module. If not set, the primary cache module will be used. |
Output |
Exception |
|
setCacheTimeout
public void setCacheTimeout |
(integer $value ) |
Input |
integer | $value | the number of seconds in which the cached state will expire. 0 means never expire. |
Output |
Exception |
throws | TInvalidDataValueException if the number is smaller than 0. |
|
setKeyPrefix
public void setKeyPrefix |
(string $value ) |
Input |
string | $value | prefix of cache variable name to avoid conflict with other cache data |
Output |
Exception |
|
setPage
public void setPage |
(TPage $page ) |
Input |
TPage | $page | the page that this persister works for. |
Output |
Exception |
|