Documentation


SEARCH

TABLE OF CONTENT

    1. Getting started 2. Basic concepts 3. Request dispatching 4. Context container 5. Dual MVC 6. Component model: 7. Security 8. Configuration 9. Session handling 10. I18n 11. Cache 12. Logging 13. Error handling 14. Advanced Topics 15. API reference

      Tutorials: Frequently Asqued Questions

      See also:


      The Cache facade

      Improving the performance by caching

      Table of Contents

      Introduction

      The Cache facade provided by the Lion Framework eases you to manage the cache usage within your application as well as define declarativelly or programatically your own caching policies.
      However, it's important to note that Lion hasn't his own cache implementation since there are really excelents solutions out there. Instead of it, Lion brings a facade as an integrated way to manage cache.

      By default, Lion framework has a facade to Cache Lite as well as MemCache, 2 of the most mature and completed PHP caching solutions.

      However, we are working in order to provide facades to others great caching frameworks such APC.
      In general, cache facade has been designed to work against whatever cache framework, so we are not binding our application to a concrete implementation at all.

      Configuring the Cache facade

      The app/lion.ini file has the following cache configuration by default:

      1. ;-------------------------------------
      2. CACHE DIRECTIVES:
      3. ;-------------------------------------
      4. ;GLOBAL CACHE DIRECTIVES:
      5. CACHE_ENABLED yes
      6. ;Cache handler class to use:
      7. CACHE_HANDLER_CLASS "__CacheLite"
      8. ;Where cache files will be placed (in case a file based cache)
      9. CACHE_FILE_DIR "var/code_c"
      10.  
      11. ;MEMCACHE SPECIFIC DIRECTIVES:
      12. ;Memcache server (defaultlocalhost)
      13. ;MEMCACHE_SERVER "localhost"
      14. ;Memcache port (default port11211)
      15. ;MEMCACHE_PORT   11211
      16.  
      17. ...

      Note that default facade points to __CacheLite. However, we can change it in order to use the MemCache facade by changing the CACHE_HANDLER_CLASS directive to use the __MemCache facade

      Cache facade API

      Cache facade works against specific cache handler implementations, so it exposes a defined interface working against whatever implementation like MemCache, APC, etc...

      To retrieve the cache handler associated to our application context, we just need to call the __ContextContainer::getCache() method:

      1. <?php
      2.  
      3.     //get the cache handler:
      4.     $cache __ApplicationContext::getInstance()->getCache();

      The cache handler exposes a really short and easy API allowing to store, retrieve and clear information from and to the cache.
      To highlight:

      i.e.

      1. <?php
      2.  
      3.     //get the cache handler:
      4.     $cache __ApplicationContext::getInstance()->getCache();
      5.     //get data from the cache:
      6.     $data $cache->load('data');
      7.     //if no data:
      8.     if($data === null{
      9.         //retrieve data from database:
      10.         $data eg_retrieve_data_from_database();
      11.         //store data to cache, so next time won't be needed to
      12.         //get it from the database but from the cache:
      13.         $cache->setData('data'$data);
      14.     }

      Cache TTL

      TTL is the expiration time of an item within the cache.
      If it's equal to zero, the item will never expire. You can use a number of seconds starting from current time, but in any case the number of seconds may not exceed 2592000 (30 days).

      We can set the TTL as the third parameter of __ICacheHandler::save() method. i.e.

      1. <?php
      2.  
      3.     //get the cache handler:
      4.     $cache __ApplicationContext::getInstance()->getCache();
      5.     //save a data to the cache in order to expire 
      6.     //after 5 minutes:
      7.     $cache->setData('data'$data300);