Welcome Guest, Not a member yet? Register   Sign In
Custom Cache Library
#1

[eluser]Braden Schaeffer[/eluser]
For those of you who ever wanted to cache more than just queries or views, I made a custom cache library for myself that I decided to share with you guys.

You can find the git repo here: http://bitbucket.org/bschaeffer/ci-cache

There's a sample controller in the repo for a usage example.
#2

[eluser]ralf57[/eluser]
Sounds great, but how it's different from Phil Sturgeon's Cache library?

http://github.com/philsturgeon/codeigniter-cache

Anyway, nice contribution
#3

[eluser]Braden Schaeffer[/eluser]
@ralf57

Um.... didn't know about that library.

It looks like mine is a little bit simpler. Phil's library has apparently implemented data retrieval methods within the class, while mine is more straight forward:

Here's an example:
Code:
<?php

class Cache_example extends Controller {

    function Cache_example()
    {
        parent::Controller();

        $this->load->library("cache");
    }

    // Simple Example

    function example()
    {
        // Did the cache library return FALSE, meaning there was no cache file

        if(($data = $this->cache->get("my_folder", "my_custom_cache_name") === FALSE)
        {
            // Use your own methods to return some data

            $this->load->model("my_model");

            $data = $this->my_model->get_some_data();

            // Attempt to write a new cache file for next time

            $this->cache->write("my_folder", "my_custom_cache_name", $data);

            // You can optionally send a custom expiration in minutes (in lieu of the
            // default set in your config file) as the 4th parameter... like so:
            //
            // $this->cache->write("my_folder", "my_custom_cache_name", $data, 30);
        }

        $this->load->view("some_view", $data);
    }

    // Semi-Advanced Example

    function posts($post_id)
    {
        if(($data = $this->cache->get("blog_posts", $post_id) === FALSE)
        {
            // Exaggerated Data Retrieval

            $this->load->model(array("blog_model", "pictures_model"));

            $data['posts'] = $this->blog_model->get_posts();
            $data['recent_comments'] = $this->blog_model->get_recent_comments();
            $data['pictures'] = $this->pictures_model->get_pictures();

            $this->cache->write("blog_posts", $post_id, $data);
        }

        $this->load->view("blog_posts", $data);
    }

}

I guess you can imagine caching any function that returns data in a fairly simple way. Cache whatever you want... you know? Also.. I haven't added a delete method yet. Smile

His library looks pretty nifty though... might have to take a look at it.




Theme © iAndrew 2016 - Forum software by © MyBB