I installed the memcache (without the D) extension for PHP. This is the function i'm using to create the memcache object just once, and then reuse it.
# Memcache handler
class mem {
var $id;
var $expire;
public static $mem;
# Constructor
function __construct($unique) {
# ID
$this->id = md5($unique);
# Item expiration
$this->expire = 86400;
# Create or reuse object
if(!isset(self::$mem)) {
# Create new connection
self::$mem = new Memcache;
self::$mem->addServer('localhost', 11211);
}
}
# Get
public static function get() {
# Check if object is cached
if($data = self::$mem->get($this->id)) {
# ID is cached
return $data;
} else {
# ID is not cached
return false;
}
}
# Set
public static function set($id, $data, $replace=false) {
if(self::$mem->add($this->id, $data, MEMCACHE_COMPRESSED, $this->expire)) {
# Write to memory
return true;
} elseif($replace) {
# Replace information
self::$mem->replace($this->id, $merge, MEMCACHE_COMPRESSED, $this->expire);
return true;
} else {
# Return nothing
return false;
}
}
# Delete key
public static function delete() {
if(self::$mem->delete($this->id)) {
return true;
} else {
return false;
}
}
}
Usage examples
User writes data to memory:
mem::set('some_clever_ID', array('this','and','that'));
User grabs the data from memory:
$data = mem::get('some_clever_ID');
User prints the grabbed data:
print_r($data); //should print an array with ['this', 'and', 'that']
User deletes the key from memory:
mem::delete('some_clever_ID');
I have 2 questions:
Why am I getting this error? Fatal error: Call to a member function add() on a non-object in /inc/memcache.php
I wonder if there would be any way to improve the performance of this code, or if I should implement a different approach. The idea is to have a quick to manage memcache values and to reuse the memcache connection when possible.
Any help and comments will be highly appreciated.
Update: Solution
I ended up placing the memcache function outside of the class.
# Memcache settings
$config['memcache']['servers'][] = array('localhost',11211);
$config['memcache']['debug'] = true;
# Create memcached object
function cache() {
global $config;
static $cache;
if(!isset($cache)) {
$cache = new Memcache;
foreach($config['memcache']['servers'] as $server) {
$cache->addServer($server[0], $server[1]);
}
}
return $cache;
}
# Memcache handler
class mem {
var $id;
var $expire;
# Constructor
function __construct($unique) {
global $config;
# ID
$this->id = md5(PRO_NAME.'_'.$unique);
# Item expiration
if($config['memcache']['debug']) {
$this->expire = 5;
} else {
$this->expire = 86400;
}
}
# Get
function get() {
# Check if object is cached
if($data = cache()->get($this->id)) {
# ID is cached
return $data;
} else {
# ID is not cached
return false;
}
}
# Set
function set($data, $replace=false) {
if(cache()->add($this->id, $data, MEMCACHE_COMPRESSED, $this->expire)) {
# Write to memory
return true;
} elseif($replace) {
# Replace information
if(cache()->replace($this->id, $data, MEMCACHE_COMPRESSED, $this->expire)) {
return true;
} else {
return false;
}
} else {
# Return nothing
return false;
}
}
# Delete key
function delete() {
if(cache()->delete($this->id)) {
return true;
} else {
return false;
}
}
}
Write example
$obj = new mem('clever_ID_here');
$obj->set('some data');
//or use $obj->set('some data', true); if you want to replace previous value
Grab example
$obj = new mem('clever_ID_here');
echo $obj->get();
Delete example
$obj = new mem('clever_ID_here');
$obj->delete(); //if you wish to delete the key