Thread: JSON Cache

Results 1 to 5 of 5
  1. #1 JSON Cache 
    Valar Morghulis

    Laxika's Avatar
    Join Date
    Sep 2006
    Age
    32
    Posts
    2,813
    Thanks given
    1,804
    Thanks received
    274
    Rep Power
    2128
    Hy guys!

    I wanted to speed up my site and my host don't have any memcaching avaidable for objects so I wroted my own Cache class. It's cache to disk not to memory but it's still much faster. Here it is:

    Edit: Replaced JSON with serialize because unserialize is faster than json_decode most of the time. JSON_encode is much faster but in a cache we need the fastest reading speed moostly and not the fastest writing speed.

    Code:
    <?php
    /**
     * Cache the item/monster etc data to disc.
     */
    Class Cache {
    
        private static $instance;
    
        /**
         * Return the singleton instance.
         * 
         * @return object the cache singleton 
         */
        public static function getInstance() {
            if (!isset(Cache::$instance)) {
                Cache::$instance = new Cache();
            }
            return Cache::$instance;
        }
    
        /**
         * Add a new object to the cache. The type and id define the place in the cache,
         * the data is the data we want to cache. It use json_encode to save the data.
         * 
         * @param type $type the type of the data eg: item/monster etc...
         * @param type $id the id of the data
         * @param type $data the data we want to store
         */
        public function addObject($type, $id, $data) {
            $file = fopen('./cache/' . $type . '/' . $type . '_' . $id . '.cch', 'w');
            fwrite($file, serialize($data));
            fclose($file);
        }
    
        /**
         * Return and object from the cache. The type and id define the place in the cache
         * we are reading from. It use json_decode to get the array from file.
         * 
         * @param type $type the type of the data eg: item/monster etc...
         * @param type $id the id of the data
         * @return array the data array 
         */
        public function getObject($type, $id) {
            $fh = fopen('./cache/' . $type . '/' . $type . '_' . $id . '.cch', 'r');
            $data = fread($fh, filesize('./cache/' . $type . '/' . $type . '_' . $id . '.cch'));
            return unserialize($data);
        }
    
        /**
         * Check that data is stored with the given type and id.
         * 
         * @param type $type the type position
         * @param type $id the id position
         * @return boolean true if found false if not 
         */
        public function isCached($type, $id) {
            return file_exists('./cache/' . $type . '/' . $type . '_' . $id . '.cch');
        }
    
        /**
         * Cloning is blocked because of the singleton pattern.
         */
        public function __clone() {
            trigger_error('Clone is not allowed.', E_USER_ERROR);
        }
    
        /**
         * Wakeup is blocked because of the singleton pattern.
         */
        public function __wakeup() {
            trigger_error('Unserializing is not allowed.', E_USER_ERROR);
        }
    
        /**
         * Construct is private because of the singleton pattern.
         */
        private function __construct() {
            
        }
    
    }
    
    ?>
    It runs 2.5x faster than using plain SQL.
    Reply With Quote  
     

  2. #2  
    Registered Member
    Anthony`'s Avatar
    Join Date
    Sep 2008
    Age
    29
    Posts
    763
    Thanks given
    75
    Thanks received
    164
    Rep Power
    204
    JSON is really cool, but what's wrong with using a traditional array for storage? You have to do extra work with JSON.

    Quote Originally Posted by Laxika View Post
    It runs 2.5x faster than using plain SQL.
    This is because SQL database storage isn't optimal for caching.
    Reply With Quote  
     

  3. #3  
    Valar Morghulis

    Laxika's Avatar
    Join Date
    Sep 2006
    Age
    32
    Posts
    2,813
    Thanks given
    1,804
    Thanks received
    274
    Rep Power
    2128
    Quote Originally Posted by Anthony` View Post
    JSON is really cool, but what's wrong with using a traditional array for storage? You have to do extra work with JSON.



    This is because SQL database storage isn't optimal for caching.
    I'm using JSON to generate a string from the array and write that string to disc. It's "caching" on disc. Caching the array in memcache is much better, but my host doesn't support APC cache or memcache because they share the cached concent in a shared enviroment. "Caching" static content to disc is faster than getting it from SQL. Yeah SQL is for dynamic data.
    Reply With Quote  
     

  4. #4  
    Registered Member
    Anthony`'s Avatar
    Join Date
    Sep 2008
    Age
    29
    Posts
    763
    Thanks given
    75
    Thanks received
    164
    Rep Power
    204
    Quote Originally Posted by Laxika View Post
    I'm using JSON to generate a string from the array and write that string to disc. It's "caching" on disc. Caching the array in memcache is much better, but my host doesn't support APC cache or memcache because they share the cached concent in a shared enviroment. "Caching" static content to disc is faster than getting it from SQL. Yeah SQL is for dynamic data.
    Okay, but serialization is also a possibility.
    Reply With Quote  
     

  5. Thankful user:


  6. #5  
    Valar Morghulis

    Laxika's Avatar
    Join Date
    Sep 2006
    Age
    32
    Posts
    2,813
    Thanks given
    1,804
    Thanks received
    274
    Rep Power
    2128
    Quote Originally Posted by Anthony` View Post
    Okay, but serialization is also a possibility.
    Usualy Serialization is 1.5-10 times slower than json. Well sometimes unserlalization is 5% faster, if the array is small. Well if you use VPS to host add lgbinary extension, thats the fastest.

    EDIT: I replaced the json with serialize, because found that unserializing is actually faster than json's decode. Thanks for the info. repped...

    Spoiler for Test code:
    Code:
    <?php
    
    for($i = 0; $i<1000; $i++){
        $array[$i] = rand(1,10000);
    }
    
    $time_start = microtime(true);
    for($i = 0; $i<1000; $i++){
        $var = json_encode($array);
    }
    
    $time_end = microtime(true);
    echo '<br>JSON_ENCODE Time: '.(($time_end-$time_start)*1000).' ms';
    
    $time_start = microtime(true);
    for($i = 0; $i<1000; $i++){
        $var = serialize($array);
    }
    
    $time_end = microtime(true);
    echo '<br>SERIALISE Time: '.(($time_end-$time_start)*1000).' ms';
    
    $time_start = microtime(true);
    $JSON_DEC = json_encode($array);
    for($i = 0; $i<1000; $i++){
        $var = json_decode($JSON_DEC, true);
    }
    
    $time_end = microtime(true);
    echo '<br>JSON_DECODE Time: '.(($time_end-$time_start)*1000).' ms';
    
    $time_start = microtime(true);
    $UNSERIALIZE = serialize($array);
    for($i = 0; $i<1000; $i++){
        $var = unserialize($UNSERIALIZE);
    }
    
    $time_end = microtime(true);
    echo '<br>UNSERIALIZE Time: '.(($time_end-$time_start)*1000).' ms';
    ?>
    Reply With Quote  
     

  7. Thankful user:



Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2010, 02:00 AM
  2. Replies: 3
    Last Post: 05-23-2010, 09:02 PM
  3. Replies: 0
    Last Post: 10-17-2009, 03:42 PM
  4. 508 Loading 525 Cache [ Cache Needed]
    By Delifed in forum Requests
    Replies: 5
    Last Post: 08-27-2009, 06:01 PM
  5. Replies: 14
    Last Post: 01-08-2009, 12:34 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •