Forum Moderators: coopster

Message Too Old, No Replies

Adodb for Php + Memcached?

         

asantos

11:52 pm on Jul 6, 2009 (gmt 0)

10+ Year Member



Has anyone used "Adodb for Php + Memcached"?

ADODB for PHP: 4.9.9.1
Memcached: v1.2.6

Im getting one of this errors (depending on the cache timeout):

9068b71dedc7d72a7e466f3369e4f9b8 cache failure: Timeout 0 (see sql below)

9068b71dedc7d72a7e466f3369e4f9b8 cache failure: Item with such key doesn't exists on the memcached server. (see sql below)

Any ideas?

ps: 9068b71dedc7d72a7e466f3369e4f9b8 is the md5() of the query

eelixduppy

2:02 am on Jul 31, 2009 (gmt 0)



Have you figured out what the problem was here yet?

asantos

4:55 pm on Jul 31, 2009 (gmt 0)

10+ Year Member



Hi... actually Adodb for PHP's memcached implementation seems to have a lot of bugs. That is way i ended coding my own implementation.

In case someone wants to check the code:


<?php
defined('FLG_CFG') or die();

# Memcached manager
# *****************
class cache {
var $id;
var $sql;
var $expire;
var $single;

# Constructor
# ***********
function __construct($sql) {
global $mem,$config;
# Create object
if(!is_object($mem)) {
$mem = new Memcache;
$mem->connect($config['cache'][0]['server'],$config['cache'][0]['port']);
}
# Id / Sql
$this->id = md5($sql);
$this->sql = $sql;
$this->expire = 43200; //12 hours
if($config['debug']) $this->expire = 15;
$this->single = false;
}

# Get key
# *******
function getRow($col=false) {
$this->single = true;
return $this->get();
}
function get() {
global $mem,$config;
# Check if object is cached
if($mem->get($this->id)) {
# Grab cache from memory
if($config['debug']) debugger('get: memory ('.$this->id.')');
return $mem->get($this->id,MEMCACHE_COMPRESSED);
} else {
# Connect to database
global $cnn;
include_once(DIR_INC.'db.php');
if($cnn->IsConnected()) {
# Create object
if($config['debug']) debugger('get: database ('.$this->id.')');
$rs = $cnn->Execute($this->sql);
if($this->single) {
$data = $rs->fields;
} else {
while(!$rs->EOF) {
$data[] = $rs->fields;
$rs->MoveNext();
}
}
# Close
if(is_object($rs)) $rs->Close();
# Write to memory
$mem->set($this->id,$data,MEMCACHE_COMPRESSED,$this->expire);
# Return recently grabbed records
return $data;
} else {
return false;
}
}
}

# Set key
# *******
function set($data,$replace=false) {
global $mem,$config;
# Write to memory
if($mem->add($this->id,$data,MEMCACHE_COMPRESSED,$this->expire)) {
if($config['debug']) debugger('set: memory ('.$this->id.')');
return true;
} elseif($replace) {
# Replace information
$mem->replace($this->id,$data,MEMCACHE_COMPRESSED,$this->expire);
if($config['debug']) debugger('set: replacing ('.$this->id.')');
# Return
return true;
} else {
# Return nothing
return false;
}
}

# Delete key
# **********
function delete() {
global $mem;
# Remove from memory
if($mem->delete($this->id)) {
return true;
} else {
return false;
}
}
}
?>

eelixduppy

6:49 am on Aug 2, 2009 (gmt 0)



Nice...thanks for sharing. :)