Welcome Guest, Not a member yet? Register   Sign In
Which Cache use and how?
#1

Hello everyone! I'm using CodeIgniter for a project and I would like to know which Cache I should use and how!

The main function of my website is that allow users to post some stuff about thing they want to buy or sell and everything is stored in a database.

At the moment I would like to cache the information of each "post" until the user doesn't modify/delete it.

My main URL are: website.com/ricerca/annuncio/423, where: "ricerca" is the controller, "annuncio" the function and "423" the "post" ID.

I've tried to use the Database caching but I find it kinda useless, maybe it's because I didn't understand how to use it. Because I've understood that CodeIgniter cache in folder like "controller+function" so i would have "ricerca+annuncio". But I would like to delete the cache of only 1 "post", and not ALL the cache..

What I have:
- User insert post -> when someone look the post becomes cached -> User modify/delete post -> ALL the cache get deleted..
What I want:
- User insert post -> when someone look the post becomes cached -> User modify/delete post -> The cache of THAT post get deleted..


Someone can tell me what I'm doing wrong and what I can do to get what I want?


Thanks Smile
Reply
#2

(This post was last modified: 05-19-2015, 11:55 AM by kilishan.)

According to the manual for both version 2 and 3, you would use
Code:
$this->cache->delete('cache_item_id');
to delete an item.

So, the only trick is to ensure that you've assigned a name that will be unique when you create the cache, and then use that same name to delete it later. Typically this would contain the item's ID, or page name, etc, and the object type. Any combination that won't conflict with something else within your application. If that page can vary based on the user's role, or whether they are logged in, you can include the role name/id as part of it, also.

Just re-read your post and it looks like you're naming your cache item 'controller/method'. In that case, there's nothing unique about each item, so a better name might be 'controller/method/post-{id}', or simply 'post-{id}'. If the contents of your post varies based on the role of the person viewing it, you could include that as part of the cache name also so you don't accidentally show the wrong thing to people who don't need it.

As for which cache driver to use? Depends on your needs. The file-based one is a great one to start with since it doesn't require anything specific on your servers. And it's simple to change out later.
Reply
#3

Thank you for your answer.

I was using the Database Caching Class which seemed the one that I needed.

My problem is that I would like to delete only the cache for 1 specific ID. From the manual I read:

Quote:The caching system saves your cache files to folders that correspond to the URI of the page you are viewing. For example, if you are viewing a page at example.com/index.php/blog/comments, the caching system will put all cache files associated with it in a folder called blog+comments. To delete those particular cache files you will use:

Code:
$this->db->cache_delete('blog', 'comments');

In my case I would need something like
Code:
$this->db->cache_delete('ricerca', 'annuncio', $ID_post);
Maybe I'm not seeing something obvious?
A solution I can think of is to use this
Code:
$this->load->driver('cache');
$this->cache->file->save($ID_post, $Data_post, $CachingTime);

//And then this to get the data that I need
$this->cache->get($ID_post);

Something like this could work? What do you think?
Thank you again for your time Smile
Reply
#4

(This post was last modified: 05-20-2015, 06:40 AM by kilishan.)

Yes, your solution would work fine. The first thing you mentioned is Database Caching - which is different than the Cache Drivers. I think that's where the confusion comes in.

The Database Cache only caches the results of the database query itself. This should only be used in production since there isn't a way to selectively delete queries and your data in development will be frequently changing. The purpose of this is for when you have queries that, having already been optimized, are still taking too long or putting too much strain on the server. This will cache the results of that, frequently being much faster than hitting the database for the query. I've found that one's best to use selectively, but only after I've tried to optimize the query, or even break the query down into multiple queries that can have more efficient joins.

The Cache Library is used for caching anything else that you want to cache, compiled views, settings, whatever.

Hope that clears it up.
Reply
#5

Thank you for the explanation, I think that I've understand now.

I will try the solution I posted earlier, will see what I can do.


Thank you again.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB