Best Approach To Keep Records In Memory

Hi,

I Have News Site Which Loads News From News Table ,

I have 5,6 widgets which loads records from same table but with some change in query Criteria

I guss if i read all records in db for current day and keep theme in global scope so for each widgets just filter Array or go for Loop in Array

Is There Better Approach ? Any Ideas ?

if i separate query for each widget i should query 5 time in pagerequest and for 1000 user its ==> 5*1000=5000 but with array its just 1000 query .

Another Problem May Happen is Memory Overload for high quantity requests

After 24 hours No Answer Recevied?

please tell your solution about question

Another solution i have though is Cache Those Queries Which are repetetive

Have you tried APC cache?

Hi Mohammad,

Yii supports various range of caching … from the data caching or the query caching at the bottom layer to the page caching on the top.

http://www.yiiframework.com/doc/guide/1.1/en/caching.overview

You can cache the content of each widget by query caching or fragment caching. And also the page caching will do a great job, if the news are not so frequently updated compared to the page views.

Thank You , For Replies

I Guess Caching is The Best Method , But is It possible to cache a query but some where change one params? or i should cache again for new changes?

The query caching uses SQL string (or precisely saying the hash of it) as the key to the entry. For example, the results of the following 2 SQLs will be cached into the separated entries.




1. SELECT * from table where some_field like '%yii%'

2. SELECT * from table where some_field like '%php%'



When 2 of your widgets uses exactly the same SQL, then they will share the same cache entry. But if there are any difference between the 2 SQLs, no matter how small the difference might be, they will be cached as the different entries.

Well, I don’t get precisely what you mean by “Array” in your first post, but I don’t think it’s a good idea to cache some range of db records for generic purpose and loop through it to get each result sets for individual widgets.

thank you for reply

i ask you make post in wiki for best caching techniques to make performance higher and suggest where to fragment caching , query caching and all type of caching

I also advise you to carefully check your ‘cache dependency’ conditions, and/or set ones. Not thinking in advance on those issues can lead to hard to find bugs. It all depends on your exact requirements, BTW.

Information on ‘cache dependency’ and expiration setting can be found in the link given above by softark to the official documentation.

Another question To Ask Is

If i cache query and forexample query is "select * from news order by date DESC";

if data was added to table is caching work or read from db or it doesn’t negative effect on cache ???

If you haven’t defined any dependency, then cache will remain the same … the outdated content will continue to be served from the cache. So we usually define some dependency to invalidate the cache entry if something has changed in the relevant tables.

Just as Boaz said, the settings of expiration and dependency are the critical path. Please refer to the official documentation for details.