Translations
Info
All page names need to be in English.
en da  de  fr  it  ja  km  nl  ru  zh

Toi cache

From TYPO3Wiki
Jump to: navigation, search
Extension detail information
toi_cache
This extension provides an API to cache content in your own extensions.

This is the content of the README.txt

documentation state stable document state list
licence OCL
forgeproject
mailinglist
usergroups list of usergroups forAdmins, forDevelopers, forIntermediates
author(s) Juergen Kussmann
TER category fe
dependency

<< Back to Extension manuals page

[edit]


Description

This extension provides an API to cache content in your own extensions.

The features are:

  • if a content is cached, the cache attach an info at the content (so you can see, that the content comes from the cache).

You can stop this behavior, if you set the last-parameter of function 'tx_toicache::setCache' to '1' (look at the API for further information)

  • cached content can automaticaly deleted (from this extension), if
    • the cached content is expired because the cache is expired
    • the cached content is expired because the cache is older than the content, it caches
    • the cached content (e.g. a plugin-record) is modified by a be-user (that works only, if the defined table- and UID-params realy exists in database)
  • You can define, which tables are cachable
  • You can define, in which areas (frontend/backend) the cache should work (if cache works in BE, maybe the frontend-editing will not work; that depends on the content, you cache!)

FAQ

Question: How can i delete the cache manualy?

Answer: Execute this SQL-Statement: delete tx_toicache_cache where expired < UNIX_TIMESTAMP()


Question: When will this extension delete the cached content automaticaly?

Answer: Cached content can automaticaly deleted (from this extension), if

  • the cached content is expired because the cache is expired
  • the cached content is expired because the cache is older than the content, it caches
  • the cached content (e.g. a plugin-record) is modified by a be-user (that works only, if defined table- and UID-params realy exists in database)


Usage of the API

Take a look at the extensions toi_news or toi_produktbeschreibung; they use this extension.

include the cache-class

PHP script:
    if(t3lib_extMgm::isLoaded('toi_cache')) {
      require_once(t3lib_extMgm::extPath('toi_cache').'class.tx_toicache.php');
    }


use the cache-api

Example to cache a singleview in your plugin (function singleView)

PHP script:
    // define the table, to which the cached content belong
    $table = 'tx_toinews_news';
 
    // define the UID of the e.g. plugin-record
    $uid   = $this->internal['currentRow']['uid'];
 
    // define a key (the key should be unique, if you don't want cache wrong content
    $key   = 'singleView_'.$this->conf['templatePath'].'_'.$this->conf['template'].'_';
    if(isset($TSFE->fe_user->user['usergroup'])) $key .= $TSFE->fe_user->user['usergroup'];
    $key   = md5($key);
 
    // define, when the content (e.g. the plugin-record) was at last modified (this parameter is optional)
    $lastModification = $this->getFieldContent('tstamp');
 
    // get cached content or build content and cache them later
    $content = '';
    if(!t3lib_extMgm::isLoaded('toi_cache') || !$content = tx_toicache::getCache($table,$uid,$key,$lastModification)) {
      $content = 'test';
    }
 
 
    // save cached content
    if(t3lib_extMgm::isLoaded('toi_cache')) tx_toicache::setCache($table,$uid,$key,$content,$expired=86400);
 
    // now, we have the content
    echo $content;

Example of using the cache: We cache an array one day long

PHP script:
    // this is only a dummy-table (because our array didn't belong to any table)

    $table = 'plugin_tx_toisearch_t3_category_tree';

    // this is only a dummy-value (because our array didn't belong to any record-uid)

    $uid   = 9999;

    $key   = $table.$uid;

    if(isset($TSFE->fe_user->user['usergroup'])) $key .= $TSFE->fe_user->user['usergroup'];

    if(!t3lib_extMgm::isLoaded('toi_cache') || !$testArray = tx_toicache::getCache($table,$uid,$key)) {

      $testArray = array('test1' => 'test-content1', 'test2' => 'test-content2');

    } else {

      $testArray = unserialize($catArray);

    }



    // save cached array

    tx_toicache::setCache($table,$uid,$key,serialize($testArray),$expired=86400,$dontAttachInfoAtContent=1);



    // now, we have the array

    var_dump($testArray);

Installation

  1. Install the extension via Extension-Manager
  2. Define, which tables should be cachable (* means all)