{"id":148,"date":"2013-10-10T09:21:39","date_gmt":"2013-10-10T09:21:39","guid":{"rendered":"http:\/\/nikola-breznjak.com\/blog\/?p=148"},"modified":"2015-08-20T11:49:41","modified_gmt":"2015-08-20T11:49:41","slug":"simple-caching-with-cache_lite-withouth-having-to-install-it-through-pear","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/php\/simple-caching-with-cache_lite-withouth-having-to-install-it-through-pear\/","title":{"rendered":"Simple caching with Cache_Lite without having to install it through PEAR"},"content":{"rendered":"<p>Sooner or later everyone has to make use of some kind of caching of their content. Below is the simplest possible code demonstration on how to implement caching with Cache_Lite (<a title=\"Cache_Lite standalone PHP class file\" href=\"http:\/\/nikola-breznjak.com\/files\/php\/Cache_Lite.zip\">download Cache_Lite standalone PHP<\/a> class file which you can just include to your project without having to install it through PEAR):<\/p>\n<pre class=\"lang:php decode:true\">require_once('Cache_Lite.php');\r\n$cache = new Cache_Lite(\t\r\n  array(\r\n    'cacheDir' =&gt; \"cache\/\",\r\n    'lifeTime' =&gt; 20 \/\/seconds\r\n  )\r\n);\r\n\r\n$req = \"file.txt\";\r\nif($data = $cache-&gt;get($req)) {\r\n  echo \"Result from cache: \" . $data;\r\n}\r\nelse {\r\n  $data = file_get_contents($req); \/\/ this is usually a DB call!\r\n  $cache-&gt;save($data);\r\n  echo \"Result not from cache: \" . $data;\r\n}<\/pre>\n<p>So, first you require the class file, make a new instance by setting a caching directory and cache lifetime. For demonstration purposes I&#8217;m outputing the contents of file.txt but of course in a real use case scenario that would be some result of a database query or sth like that.<\/p>\n<p>Simply, to get the data from cache (if it exists) you have to call the get() function on the Cache_Lite object, and to save it, well, use save() function and you&#8217;re all done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sooner or later everyone has to make use of some kind of caching of their content. Below is the simplest possible code demonstration on how to implement caching&hellip;<\/p>\n","protected":false},"author":1,"featured_media":150,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,3],"tags":[],"class_list":["post-148","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-codeproject","category-php"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/148","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=148"}],"version-history":[{"count":7,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":157,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/148\/revisions\/157"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/150"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}