Welcome Guest, Not a member yet? Register   Sign In
a lazy cache solution - no change on your current code
#1

[eluser]vizofan[/eluser]
I'm really a lazy developer and i did not realise the importance of cache until my blog slowed down when rendering a page with a lot of images.

At this stage i do not want too much change on my code to put the cache in. I did some analysis and noticed that, in most of the case, we only need cache for particular partition of the page. For example, I want to cache the content of my blog post, but no cache on the log in status (image I logged in and someone else visited the page, they could read the cached part as well).

So I started to search for some cache library, or at least solution which could meet my
criteria:

1. Must be easy to apply onto applications without cache
2. Capability to cache only part of a page (rather than what the default $CI->output is doing)
3. Ability to clear particular cache when needed
4. work in background, not change the way of codeigniter works

I did not find any solution doing exactly i wanted. Some need quite a lot of change to check if cached or not.

Then I did some experiment and comes to this particularly lazy solution!

I extended original Loader class, and overridden the method of view(). In this view() method, I check, save, retrieve the cache and organised the cache files same as $CI->output did. The only difference is, Output class works out the filename of cache based on your URI, my new Load class generated the file name based on URI, view path and parameters passed in.

Criteria 1 & 2 is done. Now I do not need to change my controllers to get my cache working. Great!

I also noticed new issue:
I have a list of recent blog posts which sits on everypage, now it is cached many times as the cache file differs in each URI

I need some way to indicate that this view is not related to URI. So I did a new method to pass an index name for the cache into view() method.

Here comes the code, as expected by programmers.


Messages In This Thread
a lazy cache solution - no change on your current code - by El Forum - 03-16-2011, 07:54 PM
a lazy cache solution - no change on your current code - by El Forum - 03-16-2011, 07:54 PM
a lazy cache solution - no change on your current code - by El Forum - 03-16-2011, 07:58 PM
a lazy cache solution - no change on your current code - by El Forum - 03-17-2011, 06:42 AM



Theme © iAndrew 2016 - Forum software by © MyBB