Blog Archive

Sunday 1 March 2015

Using the Fuel CMS cache library

I've only recently made use of the cache library in Fuel CMS, so I thought I'd post about it. In the past I've imported other cache libraries to do the task, but why bother when the native one is so useful?

If you want to save on model queries you can do this, in a controller for example:


        // try cache
        $cache_id = 'mycache';
        if(!$data = $this->fuel->cache->get($cache_id)) {
            $this->load->model('data_model');
            $data = $this->data_model->find_all_array(NULL, 'last_modified DESC');
            $this->fuel->cache->save($cache_id, $data, NULL, 3600);
        }
        // ....use the $data as intended


So all that happens here is a test to see if the cached item is valid, if not, run the query and create a new cache item. Note that $data is is populated in either case. The $cache_id  will be MD5'd in the application/cache directory, but if you identify the file by timestamp for example, you'll see that the data is serialized, like most other cache libraries.




No comments:

Post a Comment

My top artists