You are viewing the PKP Support Forum | PKP Home Wiki

Output caching

OJS development discussion, enhancement requests, third-party patches and plug-ins.

Moderators: jmacgreg, btbell, michael, bdgregg, barbarah, asmecher

Forum rules
The Public Knowledge Project Support Forum is moving to http://forum.pkp.sfu.ca

This forum will be maintained permanently as an archived historical resource, but all new questions should be added to the new forum. Questions will no longer be monitored on this old forum after March 30, 2015.

Output caching

Postby christo » Tue Dec 04, 2007 5:32 am


What is the best way to handle simple output caching with OJS? I am looking to achieve something similar to what can be done with PEAR::CACHE_LITE. I've had a look at the FileCache object, but am not 100% sure that i have got it correctly. Could anyone give me a short example of how to go about output caching with the OJS 2 framework?

Thanks in advance.
Posts: 22
Joined: Mon Jan 09, 2006 12:04 am
Location: Grahamstown, South Africa

Re: Output caching

Postby asmecher » Tue Dec 04, 2007 1:38 pm

Hi christo,

I'd suggest having a look at the current CVS version (which will be released as OJS 2.2) -- there is simple output caching implemented in it. Have a look at the web_cache and web_cache_hours settings in config.inc.php. Only certain pages are cached, e.g. the home page and a few others that are most frequently accessed.

Keep in mind, of course, that this will only work for sites that aren't using dynamic content such as banner ads on those pages.

Alec Smecher
Public Knowledge Project Team
Posts: 10015
Joined: Wed Aug 10, 2005 12:56 pm

Re: Output caching

Postby christo » Thu Dec 27, 2007 3:06 am

Here is some code if there is anyone else wanting to do this. It is based on the slightly OJS 2.1 codebase.

Code: Select all
$cacheManager =& CacheManager::getManager();

$cache = $cacheManager->getFileCache('page', /* this is the name of the group of items you want to cache - existing examples are 'journalSettings' and 'country'*/
                                                           $cacheId . "-" . Locale::getLocale() , /* this is specific to this particular instance which you are caching */
                                                           array('this','_cacheMiss') ); /* this is a function which is called to populate your cache. I cheated because i wasnt quite sure how to pass arguments to this function, so i just handled the caching procedurally below (this is probably not the best way to do it! */

if(is_null($cache->cache)){   //we don't have an existing cache, we need to renew it.
   //handle caching
   $contents = time(); //for the case of this example lets just use the timestamp...
echo $cache->getContent(); //spit out the result.

You can flush/clear the cache with $cache->flush() (this will only delete the cache for the file with the specific cacheId)

- have a look at Classes/User/CountryDAO.inc.php for a more in-depth example.
- As you will see in that example, the CacheManager can cache objects as well as variables. (which is useful).

..hope that helps someone....
Posts: 22
Joined: Mon Jan 09, 2006 12:04 am
Location: Grahamstown, South Africa

Return to OJS Development

Who is online

Users browsing this forum: Baidu [Spider] and 0 guests