Hi,
I’m using memcache in a model for validation. It works fine when called by a web app API. I’m calling the same model from a console app and it doesn’t work.
I’ve added this to my main.php in Console/Config:
'cache' => [
'class' => 'yii\caching\MemCache',
'useMemcached' => false,
'servers' => [
[
'host' => '127.0.0.2',
'port' => 11211
],
],
],
This is the same settings that i have in my API config. From the console app it doesn’t give any errors but simply doesn’t work. It works fine from the API web app.
Any ideas what I might be missing or doing wrong? Any help would be much appreciated. Thanks.
evstevemd
(Stefano Mtangoo)
October 17, 2022, 8:06pm
2
And how are you using it in console code?
Hi Stefano,
It is used in a model (TransactionInput) as below:
public function prepareCache()
{
// array of valid Transaction Types...
$typesKey = 'txnTypes';
$this->transactionTypes = Yii::$app->cache->getOrSet($typesKey, function () {
return TransactionInput::prepareTypesCache();
}, 300);
// array of valid Transaction Categories...
$catsKey = 'txnCats';
//$this->transactionCats = $cache->getOrSet($catsKey, function () {
$this->transactionCats = Yii::$app->cache->getOrSet($catsKey, function () {
return TransactionInput::prepareCatsCache();
}, 300);
// Get institution processing date if not already cached...
$procDateKey = ['institutionProcDate', $this->institution_id];
$this->institutionProcDate = Yii::$app->cache->getOrSet($procDateKey, function () {
return TransactionInput::prepareProcDateCache($this->institution_id);
}, 300);
}
private function prepareTypesCache()
{
// get all Types from input_transaction_type table...
$data = InputTransactionType::findAll([
'status' => InputTransactionType::STATUS_ACTIVE,
]);
$validTypes = array_column ($data, 'type');
return $validTypes;
}
private function prepareCatsCache()
{
// get all Categories from input_transaction_cat table...
$data = InputTransactionCat::findAll([
'status' => InputTransactionCat::STATUS_ACTIVE,
]);
$validCats = array_column ($data, 'category');
return $validCats;
}
public function isValidType($attribute, $params)
{
$found = in_array($this->$attribute, $this->transactionTypes);
if (!$found) {
$this->addError($attribute, 'Invalid type ' . $this->$attribute);
}
return $found;
}
public function isValidCat($attribute, $params)
{
$found = in_array($this->$attribute, $this->transactionCats);
if (!$found) {
$this->addError($attribute, 'Invalid category ' . $this->$attribute);
}
return $found;
}
An extract of the console code is:
$model = new TransactionInput();
$model->loadTxn($txn);
$txnArray = $model->toArray();
if ($model->validate()) {
$errors = $model->processTransaction($txnArray);
// Set response data.
if (!$errors) {
$txnResponse = $model->getSuccessfulResponseData();
}
} else {
$errors = $model->getErrors();
}
Hi,
It wasn’t a caching problem in the end. More of a daft programming error. I had forgotten to call my prepareCache() method to build the cache data.
All fixed now.
Thanks for your help.
1 Like