dpdp42233 2013-08-04 20:55
浏览 34

高效的PHP缓存策略? [重复]

This question already has an answer here:

I am kinda new in the application development. I am developing one application at the moment and I want to know your valueable inputs about a problem that I am facing as a newb.

When it comes to caching, which parts of the application I should add in cache? Let me give you an example of what I am doing:

I am developing a picture sharing application for my users and I have created a cache mechanism to cache every user's data, his albums & his pictures. I currently have 1 query per page (only in case that it is not already stored in Cache) for example:

if ( !$ItemNotInCache )
{
$MyData = $DB->Query('blah');
Cache::CacheIt($MyData);
}

This is about every result I am getting, I am trying not to call too many queries every time a user requests a page, is this efficient? The problem is that as you can understand I had to build this huge mechanism to manipulate all this data in arrays, etc..

  • When a user deletes an image from DB, i have to remove the index from the cached data of his album as well.

  • When a user adds an image to DB, I use array_merge to insert this item to his cached data and so on.

A friend of mine told me that I shouldn't abuse the cache since my data is dynamic and it will change very often and he also told me this is why MySQL is for. My question is I should use the cache in that way or no? As you can understand the data is dynamic and it should be refreshed often. Is this efficient or i should just stick with MySQL queries?

</div>
  • 写回答

1条回答 默认 最新

  • douxiu6835 2013-08-04 21:08
    关注

    I think what you are doing is perfectly fine. Retrieving from the cache uses less resources in almost all the cases as opposed to doing a query each time.

    However, I wouldn't try to "merge" the added image, just call the database again. Just increment the cache when the album modified. I don't think editing the cache when the album is modified is the right approach. Databases are good at storing and building the data so it's easy to be retrieved. Why do the building yourself?

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛