satishgaudo.com

Understanding technology

memcached: high-performance, distributed memory object caching system

memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.memcached drop the database load to almost nothing, yielding faster page load times for users, better resource utilization, and faster access to the databases on a memcache miss.For more details log on to : http://in.php.net/manual/en/book.memcache.php”

Here is the memcache class:

class Cache
{
/**
* Resources of the opend memcached connections
* @var array [memcache objects]
*/
var $mc_servers = array();
/**
* Quantity of servers used
* @var int
*/
var $mc_servers_count;
 
var $servers = array(
//array of memcache servers
array('192.168.0.217'=>'11211')
 
);
/**
* Accepts the 2-d array with details of memcached servers
*
* @param array $servers
*/
var $memObj;
function Cache(){
$this->memObj = new Memcache;
if($this->memObj === false)
{
return false;
}
foreach($this->servers as $key=>$val)
{
$this->memObj->addServer(key($val),current($val),false,1,1,-1,TRUE);
}
}
 
/**
* Clear the cache
*
* @return void
*/
function flush() {
if($this->memObj)
{
$this->memObj->flush();
}
}
 
/**
* Returns the value stored in the memory by it's key
*
* @param string $key
* @return mix
*/
function get($key) {
if (is_array($key)) {
$dest = array();
foreach ($key as $subkey) {
$val = $this->memObj->get($subkey);
if (!($val === false)) $dest[$subkey] = $val;
}
return $dest;
} else {
 
return $this->memObj->get($key);
}
}
 
/**
* Store the value in the memcache memory (overwrite if key exists)
*
* @param string $key
* @param mix $var
* @param bool $compress
* @param int $expire (seconds before item expires)
* @return bool
*/
function set($key, $var, $compress=0, $expire=0) {
if($this->memObj)
{
return $this->memObj->set($key, $var, $compress?MEMCACHE_COMPRESSED:null, $expire);
}
}
/**
* Set the value in memcache if the value does not exist; returns FALSE if value exists
*
* @param sting $key
* @param mix $var
* @param bool $compress
* @param int $expire
* @return bool
*/
function add($key, $var, $compress=0, $expire=0) {
if($this->memObj)
{
return $this->memObj->add($key, $var, $compress?MEMCACHE_COMPRESSED:null, $expire);
}
}
 
/**
* Replace an existing value
*
* @param string $key
* @param mix $var
* @param bool $compress
* @param int $expire
* @return bool
*/
function replace($key, $var, $compress=0, $expire=0) {
if($this->memObj)
{
return $this->memObj->replace($key, $var, $compress?MEMCACHE_COMPRESSED:null, $expire);
}
}
/**
* Delete a record or set a timeout
*
* @param string $key
* @param int $timeout
* @return bool
*/
function delete($key, $timeout=0) {
if($this->memObj)
{
return $this->memObj->delete($key, $timeout);
}
}
/**
* Increment an existing integer value
*
* @param string $key
* @param mix $value
* @return bool
*/
function increment($key, $value=1) {
if($this->memObj)
{
return $this->memObj->increment($key, $value);
}
}
/**
* Decrement an existing value
*
* @param string $key
* @param mix $value
* @return bool
*/
function decrement($key, $value=1) {
if($this->memObj)
{
return $this->memObj->decrement($key, $value);
}
}
//class end
}
Bookmark and Share
Categories: PHP
satish_gaude at 20:33 on 25 June 2009

< ?
/**
* Example php script to implement the memcache class
*/
/**
* Class instance: memcache
*/
$cache = new Cache;

$key = $_REQUEST['key'];
$keys = explode("_", $key);
$id = $keys[0]."_".$keys[1];
//get value for specific key from memcache
$array = $cache->get($id);

// check if there already exists a record in the mem cache
// if no
if(!$array)
{
if($keys[2]==1)
$array = Array(”thumbsup”=>1,”thumbsdown”=>0);
else if($keys[2]==2)
$array = Array(”thumbsup”=>0,”thumbsdown”=>1);
$cache->set($id,$array);
}
else
{
// increment the count
if($keys[2]==1)
$type = “thumbsup”;
else if($keys[2]==2)
$type = “thumbsdown”;

$array[$type] = $array[$type]+1;
$cache->set($id,$array);
}

td2n8pf at 11:05 on 11 July 2009

Thanks . thats a great post

Comment form currently closed..