public function write_cache($cache_path, $content)
    {
        $cache_content = json_encode($content);
        $cache_size = strlen($cache_content);

        $temp = tempnam($this->params['path'], 'temp');
        if (!($f = @fopen($temp, 'wb'))) { 
            $temp = $this->params['path'].'/'.uniqid('temp'); 
            if (!($f = @fopen($temp, 'wb'))) { 
                throw new WriteException('Unable to write temporary file '.$temp);
            }
        }

        fwrite($f, $cache_content); 
        fclose($f); 

        if (!rename($temp, $cache_path)) { 
            unlink($cache_path); 
            rename($temp, $cache_path); 
        } 

        chmod($cache_path, $this->params['write_perms']); 

        return $cache_size;
    }