Skip to content

Tools: Using AppCache

Torbjorn Zetterlund edited this page Apr 10, 2017 · 1 revision

Yggdrasil use the library AppCache. The benefit of using AppCache over the built in Cache class is that it had simple model and library call modes. No idea what that means? Have a look at this example. Here is an uncached module call:

 $this->media_m->get_article($media_id, 'live');

Here is a cached module call:

 $this->appcache->model('media_m', 'get_article', array($media_id, 'live'), 120); // keep for 2 minutes 

 // Keep forever
 $this->appcache->library('some_library', 'calculate_something', array($foo, $bar, $bla)); // keep for default time (0 = unlimited)

// Clear / Delete cache for a library or model
$this->appcache->delete_all('some_library');

This is basically a really simple way to wrap your model and library calls with cache logic to save you having to manually check expiry dates and the like. If you feel like doing it manually or are dealing with caching that is not inside a model or library method then you can do things the old fashioned way too:

if ( ! $data = $this->appcache->get('cached-name'))
{ 
   $data = $this->some_library->massive_query();
   $this->appcache->write($data, 'cached-name');
}

$this->appcache->delete('cached-name');

Where is the cache saved?

This cache data is saved in app->cache and will have a name that matches the model or library that is provided. The actual cache file will be an encryption of the method name, the parameters passed to the method and a few other things. The file will not be web readable so no data could be read by unauthorised people. It can only be added and edited by PHP, or by you as a root user.

How can I clear the cache?

If you are using a *nix system then you will want to use the following command:

 sudo rm -rf app/cache/*_m

This will remove any cached models. To remove a library, mention the name instead of *_m.

Can I make AppCache use Memcache, XCache, etc?

Nope, for that you need to work with the Cache class in CodeIgniter Reactor. It does not have the model/library call features, but will let you work with multiple caching mechanisms very easily.

Clone this wiki locally