POT
POT
POT
Main menu

Download

Forum

SVN

Prev Next

Cache drivers

About caching binary file contents.

Table of Contents

Cache mechanism

Loading binary formats in PHP is extramly slow. If you have big files it can take even few minutes! Of course such situation could be impossible to accept in case of for example standard website which should load many times for many users at once. To speed it up you should create cache drivers for data loaded from binary files.

Binary files are organised in nodes - each node in PHP is represented by node class. Nodes have their contents, can also have children and siblings. Cache handlers can compose such nodes faster and filter only those nodes which are essential for running application.

When object attempts to load file, it first tries to load it's content from cache. If it success then it's done. If no then it loads file normal way and after that it saves cache for newly loaded file for next loads.

Driver interface

Cache handler class must implement IOTS_FileCache interface. It defines two methods - for reading and writing cache. readCache() method takes one parameter - MD5 checksum of file to identify cache signature. Returns root node of cached content (entire file content should be bound with children and siblings) or null if cache doesn't exist. writeCache() method takes MD5 sum as first parameter and root node as second parameter and saves it with given sum signature.

Items cache

Another problem with file-based resources are items - items definitions files (items.xml) is also usualy very big and contains many item elements. For items there are additional methods declared in extended cache driver interface - readItems() and writeItems(). They handle only items.xml cache saving. It means that your cache driver must separately implement caching mechanism for binary formats.

Note that you can assign basic IOTS_FileCache driver to items list - then simply only binary content will be handled by cache, XML part will not be affected by cache.


Example

Cache can be stored in many ways - temporarily in session, or in some more constant ways - in SQL database, in text files, in some exported format... it's your way how to handle those mechanisms and how to code cache loading/saving methods. Here is very simple driver that stores everything in PHP notation in text file - it is very uneffective, but it's just an example to show how this all works:

  1. <?php
  2.  
  3. // to not repeat all that stuff
  4. include('quickstart.php');
  5.  
  6. /*
  7.     POT binary formats cache driver.
  8. */
  9.  
  10. class CacheDriver implements IOTS_FileCache
  11. {
  12.     // cache content
  13.     private $cache;
  14.  
  15.     // loads cache from file
  16.     public function __construct()
  17.     {
  18.         $this->cache unserializefile_get_contents('.cache') );
  19.     }
  20.  
  21.     // saves cache to file
  22.     public function __destruct()
  23.     {
  24.         file_put_contents('.cache'serialize($this->cache) );
  25.     }
  26.  
  27.     // returns cached content
  28.     public function readCache($md5)
  29.     {
  30.         ifisset($this->cache[$md5]) )
  31.         {
  32.             return $this->cache[$md5];
  33.         }
  34.     }
  35.  
  36.     // saves cache for new file
  37.     public function writeCache($md5OTS_FileNode $root)
  38.     {
  39.         $this->cache[$md5$root;
  40.     }
  41. }
  42.  
  43. // creates instance of our driver
  44. $cache new CacheDriver();
  45.  
  46. // creates OTBM loaded
  47. $otbm new OTS_OTBMFile();
  48.  
  49. // sets cache for OTBM loader
  50. $otbm->setCacheDriver($cache);
  51.  
  52. // if cache for this file will exist - will read it from cache
  53. // if not - will read file directly and save new cache
  54. $otbm->loadFile('/home/wrzasq/.otserv/data/world/map.otbm');
  55.  
  56. ?>

Prev Up Next
Global resources data/ directory resources OTAdmin client