Memcached Help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Memcached Help

    Hey friends,
    Please help me. Tell me how can i optimize my sql script through memcached?
    PHP Code:
    $sql "SELECT * FROM table";
    $query mysql_query($sql);
    while(
    $row mysql_fetch_array($query))
    {
    echo 
    "$row[1] - $row[2]"."<br/>";

    How can i integrate memcached in this type of coding? I am new to memcached and it's making me crazy. Unable to sorting it out. Please guys help me out.

    Is it like
    PHP Code:
    $m = new Memcache
    $m->connect("localhost");
    $sql "SELECT * FROM table";
    $query mysql_query($sql);
    while(
    $row mysql_fetch_array($query))
    {
    $m->set("data","$row[1]);
    $a = $m->get("data");
    echo "
    $row[1] - $row[2]"."<br/>";


    ???????????

    Please help me.
    Thanks
    Last edited by arnage; 09.05.12, 15:34.

    #2
    PHP Code:
    <?php
        $m 
    = new Memcache;
        
    $m->pconnect('localhost'11211);

        
    $sql "SELECT * FROM table";
        
        
    $m_key md5($sql);
        
        
    $memcache_vars $m->get(array($m_key));
        
    $arr $memcache_vars[$m_key];
        
            if(empty(
    $arr))
            {
                
    $query mysql_query($sql);
                while(
    $row mysql_fetch_array($query))
                {
                    
    $arr[]=$row;
                }
                if(!empty(
    $arr))$m->add($m_key$arrtrue3600);//(key,array,compression,cache time in seconds)
            
    }
            
        
    print_r($arr);
    ?>
    Advertise your mobile site for FREE with AdTwirl

    Comment


      #3
      Code:
      <?php
      $memcache = new Memcache; $memcache->connect('localhost',11211) or die ("Could not connect");
      $key = uniqid();
      $cache = array(); $cache = $memcache->get($key); 
      if
      ($cache){
      $r=$cache;
      }
      else 
      {
      
      $t = mysql_query("SELECT id, name FROM table"); while($row=mysql_fetch_array($t))
      $r[]=$row; $memcache->set($key, $r, MEMCACHE_COMPRESSED, 2000);
      // 2000 sec
      }
      flush;
      print_r ($r);
      
      ?>
      Last edited by shushant; 08.05.12, 11:29.

      Comment


        #4
        Originally posted by GumSlone View Post
        PHP Code:
        <?php
            $m 
        = new Memcache;
            
        $m->pconnect('localhost'11211);

            
        $sql "SELECT * FROM table";
            
            
        $m_key md5($sql);
            
            
        $memcache_vars $m->get(array($m_key));
            
        $arr $memcache_vars[$m_key];
            
                if(empty(
        $arr))
                {
                    
        $query mysql_query($sql);
                    while(
        $row mysql_fetch_array($query))
                    {
                        
        $arr[]=$row;
                    }
                    if(!empty(
        $arr))$m->add($m_key$arrtrue3600);//(key,array,compression,cache time in seconds)
                
        }
                
            
        print_r($arr);
        ?>
        Thanks bro. I am getting array. But i don't know how can i manage them to display one by one. In only mysql i would use
        Code:
        while($row = mysql_fetch_array($query))
        {
        echo''.$row[column].'<br/>';
        }
        This would list me only wanted colum names.

        But how can i get column names only in while loop with memcache?
        Thanks

        I am not getting arrays in associative format. It's returning in multidimensional format.
        to get expected data i have to use $arr[number][column] which is complicated.
        is it possible to get the array in associative format?

        And where i should use this array in while loop section or at the end of above codes?
        Last edited by tutorial; 15.05.12, 02:16.

        Comment


          #5
          its not complicated,
          you can use for, while or foreach loop.

          PHP Code:
          <?php 
              $m 
          = new Memcache
              
          $m->pconnect('localhost'11211); 
              
              
          $sql "SELECT * FROM table"
              
              
          $m_key md5($sql); 
              
              
          $memcache_vars $m->get(array($m_key)); 
              
          $arr $memcache_vars[$m_key]; 
              
              if(empty(
          $arr)) 
              { 
                  
          $query mysql_query($sql); 
                  while(
          $row mysql_fetch_array($query)) 
                  { 
                      
          $arr[]=$row
                  } 
                  if(!empty(
          $arr))$m->add($m_key$arrtrue3600);//(key,array,compression,cache time in seconds) 
              

              if(!empty(
          $arr))
              {
                  foreach(
          $arr AS $row)
                  {
                      echo 
          $row['column'];
                  }
              }
          ?>
          Advertise your mobile site for FREE with AdTwirl

          Comment


            #6
            Originally posted by GumSlone View Post
            its not complicated,
            you can use for, while or foreach loop.

            PHP Code:
            <?php 
                $m 
            = new Memcache
                
            $m->pconnect('localhost'11211); 
                
                
            $sql "SELECT * FROM table"
                
                
            $m_key md5($sql); 
                
                
            $memcache_vars $m->get(array($m_key)); 
                
            $arr $memcache_vars[$m_key]; 
                
                if(empty(
            $arr)) 
                { 
                    
            $query mysql_query($sql); 
                    while(
            $row mysql_fetch_array($query)) 
                    { 
                        
            $arr[]=$row
                    } 
                    if(!empty(
            $arr))$m->add($m_key$arrtrue3600);//(key,array,compression,cache time in seconds) 
                

                if(!empty(
            $arr))
                {
                    foreach(
            $arr AS $row)
                    {
                        echo 
            $row['column'];
                    }
                }
            ?>
            Thanks bro. Can you recommend me how much memory i should use for memcache? each of my table size will be more than 300MB. Any other scale tricks?

            Comment


              #7
              depends on the array size and the type of data stored in the array, i think it should be between 64 and 128 mb, you dont need more because you store your data temporarily in the memcache.
              Advertise your mobile site for FREE with AdTwirl

              Comment


                #8
                Originally posted by GumSlone View Post
                depends on the array size and the type of data stored in the array, i think it should be between 64 and 128 mb, you dont need more because you store your data temporarily in the memcache.
                I am having an error in my 2nd page where i am having more than 2 sql query.
                1 is to count total rows for page navigation and other is for content listing.

                PHP Code:
                Fatal errorCall to a member function get() on a non-object in /home/............ 
                In Following
                [PHP]$memcache_vars = $m

                fixed now. i forgot to connect memcached server
                Last edited by tutorial; 16.05.12, 07:35.

                Comment

                Working...
                X