Memcached Help

Started on the old forum in May 2012 · view the original in the archive · old links redirect here

7 replies · 593 views · started 14 years ago · 1 min read

3 participants
tutorial Junior · 14 posts #1

Hey friends, Please help me. Tell me how can i optimize my sql script through memcached?

$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

$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

GumSlone Administrator Principal · 1,690 posts #2
<?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, $arr, true, 3600);//(key,array,compression,cache time in seconds)
		}
		
	print_r($arr);
?>

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

shushant Developer · 207 posts #3
<?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);

?>
tutorial Junior · 14 posts #4

GumSlone wrote:

<?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, $arr, true, 3600);//(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

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?

GumSlone Administrator Principal · 1,690 posts #5

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

<?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, $arr, true, 3600);//(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

External image (may be gone)

tutorial Junior · 14 posts #6

GumSlone wrote: its not complicated, you can use for, while or foreach loop.

<?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, $arr, true, 3600);//(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?

GumSlone Administrator Principal · 1,690 posts #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

External image (may be gone)

tutorial Junior · 14 posts #8

GumSlone wrote: 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.

Fatal error: Call to a member function get() on a non-object in /home/............

In Following $memcache_vars = $m

🙂 fixed now. i forgot to connect memcached server

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close