how to use cache when fetching data from database?
Is is important to use cache when working with huge databases? What kind of benefits it can provide? Is it used to speedup the results?
Can you give any example for use of cache? For example, if I want to fetch some results from database and display on a webpage should i store it in cache and if the user request that page again, i should display data from cache instead of query from database again? Or there is some other reason? I dont know how to use cache, and how can i know if the data in database has been updated, etc. Can you please give any example of php/mysql where you display any demo data from database and use cache. thanks
3 Answers
Most of the large sites use some kind of caching. e.g.: Facebook uses memcached and APC. If you use caching you're likely to take off a lot of stress from your database (server), by saving query execution and even connection to the database. Query cache can be efficent, but doesn't scale well, and there are gotchas which prevents you from using it in certain situations/queries.
As for invalidation, i guess you need to handle that. You should know how long a data can live, and when to fetch the new one. All caching solutions allows you to set expiration dates for data. And you can clear the cache when a new version is deployed. About performance, see: Cache Performance Comparison (File, Query Cache, Memcached, APC) Posted: is_set 2 of 2 people found this answer helpful. Did you? Yes No
One way you can do it is to check the cache first, and if no data is found then query the DB and store the intermediate results in a cache. Here is some pseudocode:
if (! inCache ){ data = fetchDB( query ) setCache ( key, data ) } else { getCache ( key ) } return dataThe PHP docs have a memcache example as well as more information about using this type of cache. One way to store data in a cache would be to timestamp it, and to ignore data in the cache if it is too old. Obviously your caching strategy will depend on many factors, including the load on your site and the performance of your queries. You will likely want to try and profile your application to identify bottlenecks before implementing a caching strategy. Otherwise how will you know what (if any) performance improvements it has on your site? Posted: xtremex 1 of 1 people found this answer helpful. Did you? Yes No
the primary resaon for using the caching is make it more faster and decrease the load
you can look at this class for the mysql Posted: MacOS 1 of 1 people found this answer helpful. Did you? Yes No |
© Advanced Web Core. All rights reserved