PHP Memcached delete function not working as expected - php

So the problem is that the keys I want to delete from memcached are actually not being removed. I get no errors. At this point I don't really have a clue what the problem could be. It also shows the correct key it wants to delete so there is nothing wrong with that.
Down below my Class for using memcached, does anybody have any clue on what the problem might be? So far I didn't find any clues on what the problem might be or how to fix the problem.
class MemCacher extends Memcached{
function __construct(){
parent::__construct();
parent::addServer(MEMCACHED_SERVER,11211,1);
}
function add($p_sKey,$p_oData,$p_iTime = 43200){
$t_sKey = CACHE_NAME.$p_sKey;
parent::set($t_sKey,$p_oData,$p_iTime);
}
function get($p_sKey){
$t_sKey = CACHE_NAME.$p_sKey;
return parent::get($t_sKey);
}
function remove($p_sKey){
$t_sKey = CACHE_NAME.$p_sKey;
parent::delete($t_sKey);
debug("Deleted:".$t_sKey);
}
function show_all(){
if( $l_aCacheInfo = parent::getAllKeys() ){
foreach($l_aCacheInfo as $key){
if( strpos($key, CACHE_NAME) !== FALSE ){
debug($key);
}
}
}
}
function clear_all(){
if( $l_aCacheInfo = parent::getAllKeys() ){
foreach($l_aCacheInfo as $key){
if( strpos($key, CACHE_NAME) !== FALSE ){
parent::delete($key);
debug("Deleted:".$key);
}
}
}
}
}

So apparently the delete method requires the expiration time to be 0 in order to remove it else it doesn't work.
doesn't work
parent::delete($t_sKey);
works
parent::delete($t_sKey,0);

Related

codeigniter returning false from models

Just wondering if it is necessary to use else {return false;} in my codeigniter model functions or if () {} is enough and it returns false by default in case of failure?
controller:
if ($this->model_a->did()) {
$data["results"] = $this->model_a->did();
echo json_encode($data);
}
model:
public function did()
{
//some code here
if ($query && $query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
in your controller -- test the negative condition first - if nothing came back from the method in your model
if ( ! $data["results"] = $this->model_a->did() ) {
$this->showNoResults() ; }
else { echo json_encode($data); }
so thats saying - if nothing came back - then go to the showNoResults() method.
If results did come back then its assigned to $data
However - in this situation in the model i would also put ELSE return false - some people would say its extra code but for me it makes it clearer what is happening. Versus methods that always return some value.
I think this is more of a PHP question than a CodeIgniter question. You could easily test this by calling your model methods and var_dump-ing the result. If you return nothing from a method in PHP, the return value is NULL.
As much i have experience in CI returning false is not a plus point, because if you return false here then you need to have a condition back in controller which is useless you should be doing like this will save you at least some code of lines
if ($query && $query->num_rows() > 0) {
return $query->result_array();
} else {
return array();
}
so returning an array will save you from many other errors, like type error.

Why this codeigniter function parameter isn't working as it should be?

I'm trying to create callback script for Coinbase bitcoin payments. Here is the below function from my payment controller. But somehow the key isn't working as it should. I am trying to access it like that: http://www.example.com/payments/callback?key=true but it's not getting affected basically not working. Take a note that the script is working itself but after adding the key function and validating it ... it's not anymore. The issue is caused by that but I don't know what exactly, so what may cause that in the script below?
Thanks for taking the time to check and possibly answer my question.
function is_valid_key($key) {
// logic to check key
$valid = true;
if($valid) {
return true;
}
else {
return false;
}
}
function callback()
{
//Check if key is valid.
$key = $this->input->get('key');
if( ! $this->is_valid_key($key)) {
//If key above is valid order is "completed", please proceed.
$data = json_decode(file_get_contents('php://input'), TRUE);
$status = $data['order']['status'];
$userid = '507';
if (($status === 'completed')) {
$this->db->query( 'update users set user_money=user_money+15, user_credits=user_credits+5 WHERE users_id=' . $userid );
}
}
}
it has to do with your configuration settings. check out this question Enabling $_GET in codeigniter
Also you can use
parse_str($_SERVER['QUERY_STRING'], $_GET);
which would push GET right back on

if statement running regardless of true false

I have tested each method individually with default values and it all seems to work. There is something going on when they are all mixed together.
Here is the code and i'll do my best to write it in an easy to follow way:
Starting with the controller:
if ($active['newcliq'])
{
$newcliqid = $this->create_m->create_keyword($cliq, $cliqid);
if (!$newcliqid) {
echo json_encode(array('success' => false));
} else {
$this->logic_m->change_active($newcliqid, $cliq);
}
}
$active['newcliq'] is true or false and pulled from userdata('active')
Of course, the next thing it runs is create_keyword($cliq, $cliqid) seen below:
$this->db->insert('cliq', $insert);
$newcliqid = $this->db->insert_id();
if ($newcliqid) {
return $newcliqid;
} else {
return false;
}
Again, I have checked it all manually, and I know that $newcliqid is returning the correct insert_id and the overall function is returning the correct value.
So $newcliqid is returned to the controller and goes runs logic_m->change_active seen below:
if (!$this->logic_m->cliqidcheck($cliqid)){
$cliqid = 6;
}
The above line is what is giving me problems. No matter what value, $cliqid is ALWAYS set to 6. Whether cliqidcheck returns true or false.
Here is cliqidcheck($cliqid)
public function cliqidcheck($cliqid)
{
if ((ctype_digit($cliqid)) AND ($this->checkcliqidexist($cliqid)))
{
return true;
} else {
return false;
}
}
I have tested cliqidcheck with manually entered values and it always returns the correct value. In addition, i've flat out removed the cliqidcheck from the change_active model and it works perfectly.
I also echo'ed the variable $newcliqid in the controller and found the correct value.
I am hoping this is just a simple problem that I'm overlooking. Thanks for the help! Please let me know if more info is required.
Instead of verbal explanations, wouldn't be it better to post either the debugging code
var_dump($cliqid);
$tmp = $this->logic_m->cliqidcheck($cliqid);
if (!$tmp) {
$cliqid = 6;
}
var_dump($tmp, $cliqid);
die;
and it's output.
Even without posting it here it will convince you that if statement actually never "running regardless of true false"
Setting full error reporting also helps (with finding typos and such)
ini_set('display_errors',1);
error_reporting(E_ALL);
Also a note on excessive code. This statement
if (condition)
{
return true;
} else {
return false;
}
can (and should, in my opinion) be shortened to
return (condition);
Same goes for insert id. Why not to make it just
return $this->db->insert_id();
without all that windy
if ($newcliqid) {
return $newcliqid;
} else {
return false;
}
which is actually a mere tautology

PHP - override an output, multiple config

I have an echo:
echo 'Hello world';
and there are two places where I can mange if this echo will visible or not, I can check them using:
global_option('show_echo');
and
post_option('show_echo');
Pleae tell how to override global_option from post_option, Like:
In global_option('show_echo') echo was set to hidden, and visible in post_option('show_echo'); then output the echo.
How to do I make a else if which give priority to post_option, mean post_option can override global_option.
Suggest me a simplest way to achieve this
It sounds like you need a function which implements the order of priority:
function check_config( $key) {
// Your question is unclear as to the exact logic necessary here
$post = post_option( $key);
if( isset( $post)) return $post;
$global = global_option( $key);
if( isset( $global)) return $global;
return false;
}
Then just call it with:
if( check_config( 'show_echo')) {
}

Are these underscored PHP functioned ever called?

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.

Categories