Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Docs Other Cache

Frank Kleine edited this page Apr 7, 2012 · 1 revision

Table of Contents

Using the cache component

Dynamic pages often create a huge amount of load and require more time to render the result that will be displayed to the user. However there is not the need to create the same data again and again - you can cache it and therefore save time and resources. Stubbles contains a cache component that can be used to cache data in various ways.

Simple example

#php
<?php
class MyAppClass
{
    protected $cache;

    /**
***sets cache instance
     *
***@param  stubCacheContainer  $cache
***@Inject
***/
    public function setCache(stubCacheContainer $cache)
    {
        $this->cache = $cache;
    }

    public function doSomething()
    {
        $key = 'someKey_to_identify_concrete_content';
        if ($this->cache->has($key) === true) {
            $result = $this->cache->get($key);
        } else {
            // create result
            $this->cache->put($key, $result);
        }

        // do something with $result
    }
}

?>

As you can see it is really simple to use the cache: just get a cache container injected, and in the application process check if the cache knows what we want to have. If yes retrieve the data from cache, else generate it and put it into the cache.

Configuring the cache

The cache is configured via dependency injection. Stubbles provides a default configuration with the net::stubbles::util::cache::ioc::stubCacheBindingModule. This means by default the cache data will be stored in the projects/$PROJECT/cache directory.

To use another cache configuration you may want to extend the net::stubbles::util::cache::ioc::stubCacheBindingModule class or to create your own binding module.

Create your own caching strategy

A caching strategy makes the decisions for the cache container. While the cache container only knows how to store and retrieve cached data, the strategy knows when data should be cached or not or when data should be considered as expired.

To create your own caching strategy you just need to implement the net::stubbles::util::cache::stubCacheStrategy interface.

  • isCachable() should decide whether an item can be cached. This may be done by checking the size of the cache and of the item to cache and deciding whether the item fits into the cache or not.
  • isExpired() decides whether a cached item should be considered as expired or not.
  • shouldRunGc() decides whether a run of the garbage collection should be done or not. Possible things to consider may be the amount of items in the cache, the space used by the cache or any other condition you may consider useful.

See net::stubbles::util::cache::stubDefaultCacheStrategy for an example implementation.

Create your own cache container

A cache container does the real work of storing, retrieving and removing the cache data. Stubbles contains a net::stubbles::util::cache::stubFileCacheContainer which stores the cache data in files on the disc. (We may add more containers as we have the need for them and/or find them useful.)

To create your own cache container you need to implement the net::stubbles::util::cache::stubCacheContainer interface. To make this simple you may extend the net::stubbles::util::cache::stubAbstractCacheContainer which already contains all stuff that deals with the strategy and defines various abstract methods which ease the development of an own cache container because the own container just needs to deal with the storing, retrieving and removing operations and not with the strategy as well.

Clone this wiki locally