Problème avec la mise en cache des résultats SQL

  • Bogey
  • Bogey
  • Genius
  • Avatar de l’utilisateur
  • Inscription: Juil 14, 2005
  • Messages: 8211
  • Loc: USA
  • Status: Offline

Message Novembre 5th, 2010, 10:10 pm

Ok, j'ai la fonction suivante:
PHP Code: [ 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.     }

Vous voyez où il vérifie si elle est mise en cache? Au début pour voir si elle a besoin de retourner près de la fin où il vérifie si elle était nécessaire pour la mise en cache...

Quoi qu'il en soit, la fonction qui fait des caches est la suivante:
PHP Code: [ 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.     }

Le problème Im avoir, c'est quand j'essaie d'exécuter l'instruction SQL et être mise en cache eux, il met en cache les en entier. Je ne var_dump ($ results) et l'affiche int (1).

Je vérifie les fichiers du cache et ils disent i: 1;

Toute aide?
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Anonymous
  • Bot
  • No Avatar
  • Inscription: 25 Feb 2008
  • Messages: ?
  • Loc: Ozzuland
  • Status: Online

Message Novembre 5th, 2010, 10:10 pm

  • Bogey
  • Bogey
  • Genius
  • Avatar de l’utilisateur
  • Inscription: Juil 14, 2005
  • Messages: 8211
  • Loc: USA
  • Status: Offline

Message Novembre 6th, 2010, 11:22 am

Je n'ai pas encore pouvoir trouver une solution à ce problème pour le moment. Y at-il quelqu'un ici qui est capable de m'aider à résoudre ce problème.

La fonction a changé à ce qui suit:
PHP Code: [ 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 l’utilisateur
  • Inscription: Juil 14, 2005
  • Messages: 8211
  • Loc: USA
  • Status: Offline

Message Novembre 6th, 2010, 8:03 pm

Ok, jamais l'esprit. Le problème n'était pas avec la mise en cache...J'ai finalement réussi à le corriger. Tu parles d'une charge sur mon dos :lol: Je peux enfin continuer mon projet.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Inscription: Fév 10, 2004
  • Messages: 13455
  • Loc: Florida
  • Status: Offline

Message Novembre 7th, 2010, 9:53 am

Heureux que vous ça a marché. Ive a regardé à ce fil d'un couple de fois se demander pourquoi le cache de requêtes MySQL n'a pas été assez cache. :)
Strong with this one, the sudo is.
  • Bogey
  • Bogey
  • Genius
  • Avatar de l’utilisateur
  • Inscription: Juil 14, 2005
  • Messages: 8211
  • Loc: USA
  • Status: Offline

Message Novembre 7th, 2010, 7:40 pm

joebert a écrit:
Heureux que vous ça a marché. Ive a regardé à ce fil d'un couple de fois se demander pourquoi le cache de requêtes MySQL n'a pas été assez cache. :)

:D

Le problème a été la manière dont j'ai été la mise en œuvre de la cache.

J'étais avec mon _get_ () qui appelle mysql_num_rows (), qui a ensuite, il met en cache. Puis il demande à mysql_fetch_row et il met en cache ainsi...le problème, c'est que le SQL est le même pour les deux requêtes.

Eh bien, ce n'était pas le problème, mais ils ont tous deux essayé d'écrire les résultats dans le fichier cache même.

Avec Ubuntu, dès qu'il crée le fichier écrit, ils sont automatiquement chmodded en lecture seule, donc mysql_fetch_row revenait int (1) depuis thats ce qui est dans le cache après mysql_num_rows.


J'ai ajouté _num le SQL qui met en cache mysql_num_row comme nom de fichier md5 ($ sql. "_num")
"Bring forth therefore fruits meet for repentance:" Matthew 3:8

Afficher de l'information

  • Total des messages de ce sujet: 5 messages
  • Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 95 invités
  • Vous ne pouvez pas poster de nouveaux sujets
  • Vous ne pouvez pas répondre aux sujets
  • Vous ne pouvez pas éditer vos messages
  • Vous ne pouvez pas supprimer vos messages
  • Vous ne pouvez pas joindre des fichiers
 
 

© 2011 Unmelted, LLC. Ozzu® est une marque déposée de Unmelted, LLC