• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Caching Functions

#1
[eluser]Flesha[/eluser]
Hi,

I need to create a cache of some functions, but the Class Cache from the "Code Igniter" creates the whole page Cache.

I'm creating a Control Panel to Linux Servers, and some funcions execute slowly.

Can anybody help me?

Thanks!

PS: Sorry for my bad english, rsrs

#2
[eluser]Flesha[/eluser]
anybody?

Thanks

#3
[eluser]Sumon[/eluser]
Function consists of database actions and some logic. You mostly intend to cache database (more specifically select statement). So as a result site will be faster. Now you might not need to cache every parts. So you can turn it off. For example

Code:
// Turn caching on
$this->db->cache_on();
$query = $this->db->query("SELECT * FROM mytable");

// Turn caching off for this one query
$this->db->cache_off();
$query = $this->db->query("SELECT * FROM members WHERE member_id = '$current_user'");

// Turn caching back on
$this->db->cache_on();
$query = $this->db->query("SELECT * FROM another_table");
You can find more detail in CI User Guide.
By the way, i am not sure my try helps you or not.

#4
[eluser]Flesha[/eluser]
Hi, thanks!

but I wanted to know about any function and not only the database Wink

Wink

#5
[eluser]Mirage[/eluser]
You can't cache a function. Are you trying to send back a page while the function executes in the background?

Or are you calling a slow running function multiple times in your script/view and want to speed up the result of subsequent calls within the same request?

#6
[eluser]Flesha[/eluser]
Example:

I have a function to get hostname from the server and every pageview he performs the function.

This processe are slowly, then I wanted to cache this function and every 10, 20 minutes create a cache again.

Wink

#7
[eluser]Mirage[/eluser]
I see.

Many options. You could use one of the generic caching libraries out there or something like APC cache extension to php to quickly store temporary values like that. Or you could roll your own. This would share the value across all requests.

If you need to cache it user specific, you can store it in the session along with a timestamp and check that on every request.

Another really 'cheap' option is to set a cookie with with a 10 minute expiration that contains the value. If the cookie is sent you pull the value from there, if not - you run your external request. Again that caches it per user, not for the entire site.

HTH,
-m

#8
[eluser]Sumon[/eluser]
[quote author="Flesha" date="1220997427"]Example:

I have a function to get hostname from the server and every pageview he performs the function.

This processe are slowly, then I wanted to cache this function and every 10, 20 minutes create a cache again.

Wink[/quote]

From CI User Guide

Since cache files do not expire, you'll need to build deletion routines into your application. For example, let's say you have a blog that allows user commenting. Whenever a new comment is submitted you'll want to delete the cache files associated with the controller function that serves up your comments. You'll find two delete functions described below that help you clear data.

Code:
$this->db->cache_delete()
Deletes the cache files associated with a particular page. This is useful if you need to clear caching after you update your database.
Code:
$this->db->cache_delete_all()
Clears all existing cache files.

Mirage,

You can’t cache a function. - Thanks for the information. I was sure but not confident enough as now i am.

So Finally, why not let the cache remain for a long period of time? You create a function of delete cache whenever new hostname added into the system. No need to create it every 10 or 20 minutes.

#9
[eluser]Flesha[/eluser]
Hi,
Sumon, the function hostname is a simple example. My problem are in the function that measures the use of disk, that can vary from hour to hour, minute to minute, because users can add new files on the server, using the proxy cache...


Wink

#10
[eluser]thinkigniter[/eluser]
You can cache the results of a function with khcache.

Check this out http://ellislab.com/forums/viewthread/69843/


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.