{"id":1236,"date":"2015-02-17T06:16:29","date_gmt":"2015-02-17T06:16:29","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1236"},"modified":"2015-08-10T06:50:50","modified_gmt":"2015-08-10T06:50:50","slug":"writing-small-data-to-file-and-reading-from-it-or-query-the-database","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/writing-small-data-to-file-and-reading-from-it-or-query-the-database\/","title":{"rendered":"Writing small data to file and reading from it or query the database"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[1236]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>My <a href=\"http:\/\/stackoverflow.com\/questions\/14851653\/writing-small-data-to-file-and-reading-from-it-or-query-the-database\">question<\/a> was:<\/p>\n<p style=\"color: #222222;\">I have a situation where I would have to query the database for some data which is the same for all users and changes daily, so I figured I could make a file in which I would save this data (once per day) and then load it from that file each time a user visits my site.<\/p>\n<p style=\"color: #222222;\">Now, I know that this is a common practice (caching) when the requests from database are big but the data I&#8217;m about to write to the file is a simple 3 digit number, so my question is should this still be faster or is it just an overkill and I should stick with the database query?<\/p>\n<p style=\"color: #222222;\">\u00a0The answer, by <a href=\"http:\/\/stackoverflow.com\/users\/1711796\/dukeling\">Dukeling<\/a>,\u00a0was:<\/p>\n<blockquote>\n<p style=\"color: #222222;\">Caching, when done right, is always faster.<\/p>\n<p style=\"color: #222222;\">It depends how long storing and retrieving data from the file takes and how long requests to the database takes.<\/p>\n<p style=\"color: #222222;\">If the database query to get the number takes long, then caching may be a good idea, since the data is small.<\/p>\n<p style=\"color: #222222;\">If you were to do a search (e.g. sequential) in a file with lots of cached data (which doesn&#8217;t seem to be the case), it would take long.<\/p>\n<p style=\"color: #222222;\">Disk I\/O could be slower than database I\/O (which is unlikely, unless it&#8217;s a local DB).<\/p>\n<p style=\"color: #222222;\">Bottom line &#8211; benchmark.<\/p>\n<p style=\"color: #222222;\">For your scenario, caching is probably a good idea, but if it&#8217;s only a single 3-digit number for all users then I&#8217;d just try to stick it in RAM rather than in a file.<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts I will will be posting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":609,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1236","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1236"}],"version-history":[{"count":2,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1236\/revisions"}],"predecessor-version":[{"id":1691,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1236\/revisions\/1691"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/609"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}