Php Flow » Php

PHP Cache: How to Cache XML file in php

Cache is important terminology for website performance. The cache is playing very important role to improve performance of website.
For example when we are accessing web service and This method is accessing very frequently. The web method response is not change frequently.
At that point if we are accessing each time web service ,it’s very costly in terms of website performance.
At that time we will keep a xml copy of response in cache folder and use it again and again until response will not change. Now our mind have question how to identify the response or file has been change.


There are two method to control cache expiration:
1- Set expiry date time of file from cache folder.
2- File dependent caching(compare file created time of cache file as well as source file).

Below is code to create cache file with fixed expiry datetime.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function checkCache()
  {
      $path = 'cache/phpflow.xml';
	  $oriFile = ' phpflow.xml';
      if ((!file_exists($path) || time() - filemtime($path) > 60) && $cache = fopen($path, 'w+'))
      {
        fwrite($cache, file_get_contents($oriFile));
        fclose($cache);
      }
      else
      {
        $cache = fopen($path, 'r');
        return file_get_contents($path);
        fclose($cache);
      }
  }

HTML call:

1
checkCache();
Did you enjoy this article? Share it!

About the Author:

Hi, This is Parvez Alam from India. I am software developer with 4 years’ experience in web development. I have submitted articles on PHP, Mysql, Magento,CSS, HTML, jQuery, web designing and social API. You can subscribe to my blog via RSS/Twitter/Google plus and Facebook.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>