{"id":1253,"date":"2013-06-12T10:11:22","date_gmt":"2013-06-12T09:11:22","guid":{"rendered":"http:\/\/denbeke.be\/blog\/?page_id=1253"},"modified":"2013-06-12T10:29:40","modified_gmt":"2013-06-12T09:29:40","slug":"php-cache","status":"publish","type":"page","link":"https:\/\/denbeke.be\/blog\/php-cache\/","title":{"rendered":"PHP Cache"},"content":{"rendered":"<p>Voor een andere toepassing heb ik een eenvoudige klasse nodig om queries te cachen. Vandaar dat ik een simpele PHP Cache geschreven heb.<\/p>\n<p>&nbsp;<\/p>\n<p>Deze klasse is makkelijk te bedienen. Je maakt een nieuw Cache object aan, en je kan checken of de cache verlopen is of niet, een cache uitlezen, schrijven naar de cache.<\/p>\n<p>Intern worden de queries opgeslagen in de map &#8216;cache&#8217;. (Met .htaccess kan je ervoor zorgen dat gebruikers niet naar die map kunnen surfen)<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/github.com\/DenBeke\/phpcache\" target=\"_blank\">Download Code<\/a><\/p>\n<p>&nbsp;<\/p>\n<pre><code data-language=\"php\">\/**\r\n@brief Class for Caching data\r\n\r\nThis class caches data in a folder. You can check if a cache is still valid, read a cache and write to a cache.\r\n*\/\r\nclass Cache {\r\n\r\n    private $directory;\r\n    private $defaultTime = 500;\r\n\r\n    \/**\r\n    Constructor\r\n\r\n    @param cache directory\r\n    *\/\r\n    public function __construct($directory = '.\/cache\/') {\r\n        $this-&gt;directory = $directory;\r\n    }\r\n\r\n    \/**\r\n    Check if the cache of the given query is already expired\r\n\r\n    @param query\r\n    @param time after expired\r\n    @return bool expired\r\n    *\/\r\n    public function isExpired($query, $time = NULL) {\r\n        if ( is_null($time) ) {\r\n                $time = $this-&gt;defaultTime;\r\n        }\r\n        $fileName = $this-&gt;directory.$this-&gt;encrypt($query);\r\n        $modified = filemtime($fileName);\r\n        $now = time();\r\n        if(($now - $modified)  &lt;= $time) {             return true;         }         else {             return false;         }     }                 \/**     Check if there is a cached file for the given query          @param query     @return bool     *\/     public function cacheExists($query) {         $fileName = $this-&gt;directory.$this-&gt;encrypt($query);\r\n        return file_exists($fileName);\r\n    }\r\n\r\n    \/**\r\n    Read the cache file of the given query\r\n\r\n    @param query\r\n    @return string containing file content\r\n    *\/\r\n    public function readCache($query) {\r\n        $fileName = $this-&gt;directory.$this-&gt;encrypt($query);\r\n        if(!file_exists($fileName)) {\r\n            throw new exception(\"File does not exists\");\r\n        }\r\n        else {\r\n            return file_get_contents($fileName);\r\n        }\r\n    }\r\n\r\n    \/**\r\n    Write the result of the query to a cache file\r\n\r\n    @param query\r\n    @param result\r\n    *\/\r\n    public function writeCache($query, $result) {\r\n        $fileName = $this-&gt;directory.$this-&gt;encrypt($query);\r\n        file_put_contents($fileName, $result);\r\n    }\r\n\r\n    \/**\r\n    Encrypt string with md5\r\n\r\n    @param string\r\n    @return encrypted string\r\n    *\/\r\n    private function encrypt($string) {\r\n        return md5($string);\r\n    }\r\n\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Voor een andere toepassing heb ik een eenvoudige klasse nodig om queries te cachen. Vandaar dat ik een simpele PHP Cache geschreven heb. &nbsp; Deze klasse is makkelijk te bedienen. Je maakt een nieuw Cache object aan, en je kan checken of de cache verlopen is of niet, een cache uitlezen, schrijven naar de cache. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.6.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP Cache &ndash; DenBeke<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/denbeke.be\/blog\/php-cache\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP Cache &ndash; DenBeke\" \/>\n<meta property=\"og:description\" content=\"Voor een andere toepassing heb ik een eenvoudige klasse nodig om queries te cachen. Vandaar dat ik een simpele PHP Cache geschreven heb. &nbsp; Deze klasse is makkelijk te bedienen. Je maakt een nieuw Cache object aan, en je kan checken of de cache verlopen is of niet, een cache uitlezen, schrijven naar de cache. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/denbeke.be\/blog\/php-cache\/\" \/>\n<meta property=\"og:site_name\" content=\"DenBeke\" \/>\n<meta property=\"article:modified_time\" content=\"2013-06-12T09:29:40+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:site\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"2 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/denbeke.be\/blog\/#website\",\"url\":\"https:\/\/denbeke.be\/blog\/\",\"name\":\"DenBeke\",\"description\":\"Mathias Beke\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/denbeke.be\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/denbeke.be\/blog\/php-cache\/#webpage\",\"url\":\"https:\/\/denbeke.be\/blog\/php-cache\/\",\"name\":\"PHP Cache &ndash; DenBeke\",\"isPartOf\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#website\"},\"datePublished\":\"2013-06-12T09:11:22+00:00\",\"dateModified\":\"2013-06-12T09:29:40+00:00\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/denbeke.be\/blog\/php-cache\/\"]}]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/pages\/1253"}],"collection":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/comments?post=1253"}],"version-history":[{"count":10,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/pages\/1253\/revisions"}],"predecessor-version":[{"id":1263,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/pages\/1253\/revisions\/1263"}],"wp:attachment":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/media?parent=1253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}