I am using PHP Memcached & when I delete a key, I can still retrieve the key. What could I be doing wrong?
function __construct() {
$this->_cache = array();
// if we have memcache support, load it from CACHE_POOL
//
if (class_exists('Memcached')) {
$this->_mc = new Memcached('CACHE_POOL');
$servers = $this->_mc->getServerList();
if (empty($servers)) {
//This code block will only execute if we are setting up a new EG(persistent_list) entry
$this->_mc->setOption(Memcached::OPT_RECV_TIMEOUT, 1000);
$this->_mc->setOption(Memcached::OPT_SEND_TIMEOUT, 3000);
$this->_mc->setOption(Memcached::OPT_TCP_NODELAY, true);
$this->_mc->setOption(Memcached::OPT_PREFIX_KEY, "md_");
$this->_mc->addServers(self::$_MEMCACHE_IPS);
}
$current_cache = $this->_mc->get(self::CACHE_KEY);
if ($current_cache) {
$this->_cache = array_merge($this->_cache, $current_cache);
}
}
}
function delete($key) {
self::instance()->_mc->delete($key);
}
function getSafe($key) {
return isset($this->_cache[$key]) ? $this->_cache[$key] : FALSE;
}
self::instance()->delete("test");
echo(self::instance()->getSafe("test"));
After running this, the get still returns a value. Not sure what is going on here.
You should also delete cache from _cache property in terms of the retrieving method:
function delete($key) {
self::instance()->_mc->delete($key);
unset(self::instance()->_cache[$key]);
}
But do not apply this code design in your production environment.
Related
I have this following code in the Address controller
public function index()
{
if($this->session->userdata('isLogin')) {
$this->load->driver('cache');
$this->load->model('MemberModel');
if(!$this->cache->get('province') == false) {
$this->load->model('ShippingModel');
$data['provinces'] = $this->ShippingModel->the_provinces(); // it will return json object
$this->cache->save('province', $data['provinces'], 300);
} else {
$data['provinces'] = $this->cache->get('province');
}
$userdata = $this->MemberModel->getProfile($this->session->userdata('userid'));
$data['user'] = $userdata;
$this->display_member_area('member/address', $data);
}
else {
redirect(base_url());
}
}
When I want to get the data using:
var_dump($this->cache->get('province'));
the result I get always shows
bool(false)
but when I tried to do this instead
var_dump($data['provinces']) // it's show me json object, that I want
Can anyone please show me where I'am doing wrong?
Thanks in advance
Remove false from the if condition and use this code.
if(!$this->cache->get('province')) {
$this->load->model('ShippingModel');
$data['provinces'] = $this->ShippingModel->the_provinces();
$this->cache->save('province', $data['provinces'], 300);
} else {
$data['provinces'] = $this->cache->get('province');
}
Make sure you have the correct credential on your config at:
https://github.com/bcit-ci/CodeIgniter/blob/develop/application/config/memcached.php
it will be at you application/config/memcached.php file. Hostname is the ip you setup your memcached instance. You could provide multiple server if you wish.
reference on installing memcached
I'm trying to make a user friendly way of testing via lime in symfony 1. I want to load a specific sql dump for each test I write (if required). The problem I face is that I don't know how to make dump loading independent of database type. Currently I'm using the shell exec() command. Here is the code :
public function loadSql()
{
$this->diag("Loading dump...");
if ($this->_sql_is_set)
{
if (file_exists($this->_getSqlPath()))
{
$this->_emptyDataBase();
$options = $this->_connection_manager->connection()->getOptions();
$dsn_parts = $this->_connection_manager->parsePdoDsn($options['dsn']);
exec("mysql -u{$options['username']} -p{$options['password']} {$dsn_parts['dbname']} < {$this->_getSqlPath()}");
return $this;
}
else
{
$this->error("Nothing to load : sql file was not found in ".$this->_getDataDir());
exit;
}
}
else
{
$this->error("Nothing to load : sql dump was not set");
exit;
}
}
$this->_connection_manager is an instance of Doctrine_Manager. Any help will with that?
Try with something like that:
public function loadSqlFiles(sfEvent $event)
{
$task = $event->getSubject();
$taskName = $task->getName();
if ($taskName == 'insert-sql') {
$conn = Doctrine_Manager::connection();
$filesPath = sfConfig::get('sf_data_dir') . '/sql/full-data';
// get all files
$files = sfFinder::type('file')->sort_by_name()->name('*.sql')->in($filesPath);
foreach ($files as $file) {
$task->logSection('custom-sql', sprintf('Inserting custom sql file (%s)', $file));
$res = $conn->getDbh()->exec(file_get_contents($file));
}
}
}
The script works fine and is setting the data, but the website code is unable to use it and is instead setting its own memcached values. My website code is written in codeIgniter framework. I don't know why this is happening.
My script code :-
function getFromMemcached($string) {
$memcached_library = new Memcached();
$memcached_library->addServer('localhost', 11211);
$result = $memcached_library->get(md5($string));
return $result;
}
function setInMemcached($string,$result,$TTL = 1800) {
$memcached_library = new Memcached();
$memcached_library->addServer('localhost', 11211);
$memcached_library->set(md5($string),$result, $TTL);
}
/*---------- Function stores complete product page as one function call cache -----------------*/
function getCachedCompleteProduct($productId,$brand)
{
$result = array();
$result = getFromMemcached($productId." product page");
if(true==empty($result))
{
//------- REST CODE storing data in $result------
setInMemcached($productId." product page",$result,1800);
}
return $result;
}
Website Code :-
private function getFromMemcached($string) {
$result = $this->memcached_library->get(md5($string));
return $result;
}
private function setInMemcached($string,$result,$TTL = 1800) {
$this->memcached_library->add(md5($string),$result, $TTL);
}
/*---------- Function stores complete product page as one function call cache -----------------*/
public function getCachedCompleteProduct($productId,$brand)
{
$result = array();
$result = $this->getFromMemcached($productId." product page");
if(true==empty($result))
{
// ----------- Rest Code storing data in $result
$this->setInMemcached($productId." product page",$result,1800);
}
return $result;
}
This is saving data in memcached. I checked by printing inside the if condition and checking the final result
Based on the CodeIgniter docs, you can make use of:
class YourController extends CI_Controller() {
function __construct() {
$this->load->driver('cache');
}
private function getFromMemcached($key) {
$result = $this->cache->memcached->get(md5($key));
return $result;
}
private function setInMemcached($key, $value, $TTL = 1800) {
$this->cache->memcached->save(md5($key), $value, $TTL);
}
public function getCachedCompleteProduct($productId,$brand) {
$result = array();
$result = $this->getFromMemcached($productId." product page");
if( empty($result) ) {
// ----------- Rest Code storing data in $result
$this->setInMemcached($productId." product page",$result,1800);
}
return $result;
}
}
Personally try to avoid 3rd party libraries if it already exists in the core framework. And I have tested this, it's working superbly, so that should fix this for you :)
Just remember to follow the instructions at http://ellislab.com/codeigniter/user-guide/libraries/caching.html#memcached to set the config as needed for the memcache server
Inherited an old CakePHP site and I'm trying to figure out what some functions do. I have several functions that have the same name as another function but with an underscore first, e.g. save() and _save(). However the function _save() is never called in any context, though save() is.
I read this question and it looks like it's from an old worst-practices exercise, but that doesn't really explain why it's in my code; you still have to call function _save() as _save() right? If there's no calls to _save() is it safe to remove?
I want it gone, even the save() function wasn't supposed to be there, rewriting perfectly good framework functionality. It looks like an older version of the same function, but there's no comments and I don't know if there's some weird context in which php/Cake will fall back to the underscored function name.
Here's the code for the curious. On closer inspection it appears the underscored functions were old versions of a function left in for some reason. At least one was a "private" method being called (from a public function of the same name, minus the underscore...):
function __save() {
$user = $this->redirectWithoutPermission('product.manage','/',true);
if ($this->data) {
$this->Prod->data = $this->data;
$saved_okay = false;
if ($this->Prod->validates()) {
if ($this->Prod->save()) $saved_okay = true;
}
if ($saved_okay) {
$product_id = ($this->data['Prod']['id']) ? $this->data['Prod']['id'] : $this->Prod->getLastInsertId();
if ($this->data['Plant']['id']) {
$this->data['Prod']['id'] = $product_id;
$this->Prod->data = $this->data;
$this->Prod->save_plants();
$this->redirect('/plant/products/'.$this->data['Plant']['id']);
} else {
$this->redirect('/product/view/'.$product_id);
}
die();
} else {
die('did not save properly');
}
} else {
die('whoops');
}
}
function save() {
$user = $this->redirectWithoutPermission('product.manage','/products',true);
if ($this->data) {
$this->Prod->data = $this->data;
if ($this->Prod->validates()) {
$this->Prod->save();
$gotoURL = isset($this->data['Navigation']['goto'])?$this->data['Navigation']['goto']:'/';
$gotoURL = str_replace('%%Prod.id%%', $this->data['Prod']['id'], $gotoURL);
if (isset($this->data['Navigation']['flash'])) {
$this->Session->setFlash($this->data['Navigation']['flash']);
}
if (isset($this->params['url']['ext']) && $this->params['url']['ext']=='ajax') {
$value = array(
'success'=>true
,'redirect'=>$gotoURL
);
print $this->Json->encode($value);
} else {
$this->redirect($gotoURL);
}
} else {
$value = array(
'success'=>false
,'message'=>"You have invalid fields."
,'reason'=>'invalid_fields'
,'fields'=>array(
'Prod'=>$this->Prod->invalidFields()
)
);
print $this->Json->encode($value);
}
} else {
$this->redirect('/products');
}
die();
}
I had hoped to learn whether or not some convention applied to this situation, but from testing I've found the functions are not called which is really the answer to the question I asked.
How do I check if field has changed?
I'd like to trigger an action in preSave() only if specific field has changed, e.q.
public function preSave() {
if ($bodyBefore != $bodyNow) {
$this->html = $this->_htmlify($bodyNow);
}
}
The question is how to get this $bodyBefore and $bodyNow
Please don't fetch the database again! This works for Doctrine 1.2, I haven't tested lower versions.
// in your model class
public function preSave($event) {
if (!$this->isModified())
return;
$modifiedFields = $this->getModified();
if (array_key_exists('title', $modifiedFields)) {
// your code
}
}
Check out the documentation, too.
Travis's answer was almost right, because the problem is that the object is overwritten when you do the Doctrine query. So the solution is:
public function preSave($event)
{
// Change the attribute to not overwrite the object
$oDoctrineManager = Doctrine_Manager::getInstance();
$oDoctrineManager->setAttribute(Doctrine::ATTR_HYDRATE_OVERWRITE, false);
$newRecord = $event->getInvoker();
$oldRecord = $this->getTable()->find($id);
if ($oldRecord['title'] != $newRecord->title)
{
...
}
}
Try this out.
public function preSave($event)
{
$id = $event->getInvoker()->id;
$currentRecord = $this->getTable()->find($id);
if ($currentRecord->body != $event->getInvoker()->body)
{
$event->getEnvoker()->body = $this->_htmlify($event->getEnvoker()->body);
}
}