Problema con el almacenamiento en caché de resultados de SQL

  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Noviembre 5th, 2010, 10:10 pm

Ok, tengo la siguiente función:
PHP Código: [ Select ]
    public function fetch_rowset($sql, $type = 'assoc')
    {
        // Checking if the SQL query was cached
        if($this->cache_results === true)
        {
            // The name of the cached file
            $sql_file = $this->cache_dir . md5($sql) . '.cache';
       
            // Checking if the SQL file exists
            if(file_exists($sql_file))
            {
                return unserialize(file_get_contents($sql_file));
            }
        }
       
        // Creating a result resource
        $results = $this->set_result_resource($sql, true);
       
        // Initiating the result array
        $result = array();
       
        // Checking what type of result we want
        if($type == 'assoc')
        {
            while($row = mysql_fetch_assoc($results))
            {
                $result[] = $row;
            }
        }
        elseif($type == 'array')
        {
            while($row = mysql_fetch_array($results))
            {
                $result[] = $row;
            }
        }
        elseif($type == 'object')
        {
            while($row = mysql_fetch_object($results))
            {
                $result[] = $row;
            }
        }
        elseif($type == 'row')
        {
            while($row = mysql_fetch_row($results))
            {
                $result[] = $row;
            }
        }
        else
        {
            while($row = mysql_fetch_assoc($results))
            {
                $result[] = $row;
            }
        }
       
        // Checking if we need to free the result resource
        if($this->free_result == true)
        {
            $this->free_result();
        }
       
        // Checking if we need to cache the result
        if($this->cache_results === true)
        {
            return $this->cache_results($sql, $result);
        }
       
        // Returning the result (if not cached)
        return ($type == 'object') ? (object) $result : $result;
    }
  1.     public function fetch_rowset($sql, $type = 'assoc')
  2.     {
  3.         // Checking if the SQL query was cached
  4.         if($this->cache_results === true)
  5.         {
  6.             // The name of the cached file
  7.             $sql_file = $this->cache_dir . md5($sql) . '.cache';
  8.        
  9.             // Checking if the SQL file exists
  10.             if(file_exists($sql_file))
  11.             {
  12.                 return unserialize(file_get_contents($sql_file));
  13.             }
  14.         }
  15.        
  16.         // Creating a result resource
  17.         $results = $this->set_result_resource($sql, true);
  18.        
  19.         // Initiating the result array
  20.         $result = array();
  21.        
  22.         // Checking what type of result we want
  23.         if($type == 'assoc')
  24.         {
  25.             while($row = mysql_fetch_assoc($results))
  26.             {
  27.                 $result[] = $row;
  28.             }
  29.         }
  30.         elseif($type == 'array')
  31.         {
  32.             while($row = mysql_fetch_array($results))
  33.             {
  34.                 $result[] = $row;
  35.             }
  36.         }
  37.         elseif($type == 'object')
  38.         {
  39.             while($row = mysql_fetch_object($results))
  40.             {
  41.                 $result[] = $row;
  42.             }
  43.         }
  44.         elseif($type == 'row')
  45.         {
  46.             while($row = mysql_fetch_row($results))
  47.             {
  48.                 $result[] = $row;
  49.             }
  50.         }
  51.         else
  52.         {
  53.             while($row = mysql_fetch_assoc($results))
  54.             {
  55.                 $result[] = $row;
  56.             }
  57.         }
  58.        
  59.         // Checking if we need to free the result resource
  60.         if($this->free_result == true)
  61.         {
  62.             $this->free_result();
  63.         }
  64.        
  65.         // Checking if we need to cache the result
  66.         if($this->cache_results === true)
  67.         {
  68.             return $this->cache_results($sql, $result);
  69.         }
  70.        
  71.         // Returning the result (if not cached)
  72.         return ($type == 'object') ? (object) $result : $result;
  73.     }

A ver dónde se comprueba si se almacena en caché? Al principio para ver si se debe devolver cerca de la final donde se comprueba si era necesario para almacenar en caché...

De todos modos, la función que en realidad se almacena en caché:
PHP Código: [ Select ]
    private function cache_results($sql, $results)
    {
        // Generating the file name for the cache
        $sql_file = $this->cache_dir . md5($sql) . '.cache';
       
        // Creating the cache file
        if(touch($sql_file))
        {
            file_put_contents($sql_file, serialize($results));
        }
        return $results;
    }
  1.     private function cache_results($sql, $results)
  2.     {
  3.         // Generating the file name for the cache
  4.         $sql_file = $this->cache_dir . md5($sql) . '.cache';
  5.        
  6.         // Creating the cache file
  7.         if(touch($sql_file))
  8.         {
  9.             file_put_contents($sql_file, serialize($results));
  10.         }
  11.         return $results;
  12.     }

El problema estoy teniendo es cuando trato de ejecutar el SQL y el almacenamiento en caché se les almacena en caché como número entero. Yo var_dump ($ resultados) y se imprime int (1).

Puedo comprobar los archivos de caché y me dicen que: 1;

Cualquier ayuda?
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Anonymous
  • Bot
  • No Avatar
  • Registrado: 25 Feb 2008
  • Mensajes: ?
  • Loc: Ozzuland
  • Status: Online

Nota Noviembre 5th, 2010, 10:10 pm

  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Noviembre 6th, 2010, 11:22 am

Todavía no he de poder encontrar una solución a este problema todavía. ¿Hay alguien aquí que pueda ayudarme a resolver este problema.

La función ha cambiado a lo siguiente:
PHP Código: [ Select ]
    public function fetch_rowset($sql, $type = 'assoc')
 
    {
 
        // Checking if the SQL query was cached
 
        if($this->cache_results === true)
 
        {
 
            // The name of the cached file
 
            $sql_file = $this->cache_dir . md5($sql) . '.cache';
 
       
 
            // Checking if the SQL file exists
 
            if(file_exists($sql_file))
 
            {
 
                return unserialize(file_get_contents($sql_file));
 
            }
 
        }
 
       
 
        // Creating a result resource
 
        $results = $this->set_result_resource($sql, true);
 
       
 
        // Checking what type of result we want
 
        if($type == 'assoc')
 
        {
 
            while($row = mysql_fetch_assoc($results))
 
            {
 
                $result = $row;
 
            }
 
        }
 
 
 
        elseif($type == 'array')
 
        {
 
            while($row = mysql_fetch_array($results))
 
            {
 
                $result = $row;
 
            }
 
        }
 
        elseif($type == 'object')
 
        {
 
            while($row = mysql_fetch_object($results))
 
            {
 
                $result = $row;
 
            }
 
        }
 
        elseif($type == 'row')
 
        {
 
            while($row = mysql_fetch_row($results))
 
            {
 
                $result = $row;
 
            }
 
        }
 
        else
 
        {
 
            while($row = mysql_fetch_assoc($results))
 
            {
 
                $result = $row;
 
            }
 
        }
 
       
 
        // Checking if we need to free the result resource
 
        if($this->free_result == true)
 
        {
 
            $this->free_result();
 
        }
 
       
 
        // Checking if we need to cache the result
 
        if($this->cache_results === true)
 
        {
 
            $this->cache_result($sql, $result);
 
        }
 
       
 
        // Returning the result (if not cached)
 
        return ($type == 'object') ? (object) $result : $result;
 
    }
  1.     public function fetch_rowset($sql, $type = 'assoc')
  2.  
  3.     {
  4.  
  5.         // Checking if the SQL query was cached
  6.  
  7.         if($this->cache_results === true)
  8.  
  9.         {
  10.  
  11.             // The name of the cached file
  12.  
  13.             $sql_file = $this->cache_dir . md5($sql) . '.cache';
  14.  
  15.        
  16.  
  17.             // Checking if the SQL file exists
  18.  
  19.             if(file_exists($sql_file))
  20.  
  21.             {
  22.  
  23.                 return unserialize(file_get_contents($sql_file));
  24.  
  25.             }
  26.  
  27.         }
  28.  
  29.        
  30.  
  31.         // Creating a result resource
  32.  
  33.         $results = $this->set_result_resource($sql, true);
  34.  
  35.        
  36.  
  37.         // Checking what type of result we want
  38.  
  39.         if($type == 'assoc')
  40.  
  41.         {
  42.  
  43.             while($row = mysql_fetch_assoc($results))
  44.  
  45.             {
  46.  
  47.                 $result = $row;
  48.  
  49.             }
  50.  
  51.         }
  52.  
  53.  
  54.  
  55.         elseif($type == 'array')
  56.  
  57.         {
  58.  
  59.             while($row = mysql_fetch_array($results))
  60.  
  61.             {
  62.  
  63.                 $result = $row;
  64.  
  65.             }
  66.  
  67.         }
  68.  
  69.         elseif($type == 'object')
  70.  
  71.         {
  72.  
  73.             while($row = mysql_fetch_object($results))
  74.  
  75.             {
  76.  
  77.                 $result = $row;
  78.  
  79.             }
  80.  
  81.         }
  82.  
  83.         elseif($type == 'row')
  84.  
  85.         {
  86.  
  87.             while($row = mysql_fetch_row($results))
  88.  
  89.             {
  90.  
  91.                 $result = $row;
  92.  
  93.             }
  94.  
  95.         }
  96.  
  97.         else
  98.  
  99.         {
  100.  
  101.             while($row = mysql_fetch_assoc($results))
  102.  
  103.             {
  104.  
  105.                 $result = $row;
  106.  
  107.             }
  108.  
  109.         }
  110.  
  111.        
  112.  
  113.         // Checking if we need to free the result resource
  114.  
  115.         if($this->free_result == true)
  116.  
  117.         {
  118.  
  119.             $this->free_result();
  120.  
  121.         }
  122.  
  123.        
  124.  
  125.         // Checking if we need to cache the result
  126.  
  127.         if($this->cache_results === true)
  128.  
  129.         {
  130.  
  131.             $this->cache_result($sql, $result);
  132.  
  133.         }
  134.  
  135.        
  136.  
  137.         // Returning the result (if not cached)
  138.  
  139.         return ($type == 'object') ? (object) $result : $result;
  140.  
  141.     }
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Noviembre 6th, 2010, 8:03 pm

Ok, no importa. El problema no era con el almacenamiento en caché...Finalmente logró solucionarlo. Hablar de una carga de mi espalda :lol: Por fin puedo continuar con mi proyecto.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Registrado: Feb 10, 2004
  • Mensajes: 13458
  • Loc: Florida
  • Status: Offline

Nota Noviembre 7th, 2010, 9:53 am

Me alegro de que todo salió. He mirado en este hilo un par de veces preguntando por qué la caché de consultas MySQL no fue suficiente caché. :)
Strong with this one, the sudo is.
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Noviembre 7th, 2010, 7:40 pm

joebert escribió:
Me alegro de que todo salió. He mirado en este hilo un par de veces preguntando por qué la caché de consultas MySQL no fue suficiente caché. :)

:D

El problema estaba en cómo estaba la aplicación de la caché.

Yo estaba usando mi _get_ () que llama a mysql_num_rows (), que a su vez, se almacena en caché. Luego se pide a mysql_fetch_row y almacena en caché, así...el problema con esto es que el SQL es el mismo para ambas consultas.

Bueno, eso no era el problema, pero los dos trataron de escribir los resultados en el archivo de caché misma.

Con Ubuntu, tan pronto como se crea el archivo escrito, que son automáticamente chmodded como de sólo lectura, por lo que mysql_fetch_row regresaba int (1), ya que eso es lo que está en la caché después de mysql_num_rows.


He añadido _num para el SQL que almacena mysql_num_row como nombre de archivo md5 ($ sql ". _num")
"Bring forth therefore fruits meet for repentance:" Matthew 3:8

Publicar Información

  • Total de mensajes en este tema: 5 mensajes
  • Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 136 invitados
  • No puede abrir nuevos temas en este Foro
  • No puede responder a temas en este Foro
  • No puede editar sus mensajes en este Foro
  • No puede borrar sus mensajes en este Foro
  • No puede enviar adjuntos en este Foro
 
 

© 2011 Unmelted, LLC. Ozzu® es una marca registrada de Unmelted, LLC