1

I have in my config web

'cache' => [
        'class' => 'yii\caching\MemCache',
        'servers' => [
            [
                'host' => '127.0.0.1',
                'port' => 11211,
                'weight' => 60,
            ],
        ],
    ]

and a form with email. In my controller, I want to cache the count of how many time the same email posted.

if($model->load(Yii::$app->request->post()) && $model->validate()){
    $counter = \Yii::$app->cache->get('antiflood-'.$model->email);
    if($counter === false){
        \Yii::$app->cache->set('antiflood-'..$model->email,1,86400);
    } else {
        \Yii::$app->cache->set('antiflood-'.$model->email,$counter+1,86400);
    }
    var_dump(Yii::$app->cache->get(''antiflood-'.$model->email));
    exit;
}

After I submit form, count is increased.If I refresh my page, I have confirmation from chrome and I see

enter image description here

If I click continue it's working fine. But if I go back to form and enter same email, I see again 1 output i.e again go to if condition but need to go to else because I already set id with same email. What is wrong here? Can anyone suggest me. Thank You.

Bizley
  • 17,392
  • 5
  • 49
  • 59
Dev
  • 345
  • 1
  • 15
  • Are you 100% sure `$model->email` is `5001@direktpoint.pl`? – Bizley Jan 25 '17 at 10:36
  • Check if memcache works at all. – Bizley Jan 25 '17 at 10:38
  • @BIzley Please see the edit. I try to put hardcore email in place $model->email and is with same result. – Dev Jan 25 '17 at 10:39
  • Check if `Yii::$app->cache->set()` returns `true` and if it's not the case check if memcache server works. – Bizley Jan 25 '17 at 10:43
  • I see if I var_dump(\Yii::$app->cache->get('antiflood-'.$model->email)) just before post and fill my form with same email. My output is always bool(false).And how can I check memcache server is working or not? – Dev Jan 25 '17 at 10:55
  • Run `ps aux | grep memcache` and see if there is anything. – Bizley Jan 25 '17 at 10:58
  • I don't have access to server terminal but I checked with var_dump(class_exists('Memcache')). Its return me true. – Dev Jan 25 '17 at 11:07
  • Run code from [this answer](http://stackoverflow.com/a/11061667/3364821). – Bizley Jan 25 '17 at 11:10
  • Is the same if I fill up my data and submit I will always have fresh data in output. – Dev Jan 25 '17 at 11:35
  • Have you run the code I linked? Do you see `Memcache seem to be working fine!`? – Bizley Jan 25 '17 at 11:38
  • Yap I see Memcache seem to be working fine! in last line of output. – Dev Jan 25 '17 at 11:49
  • Hmm, I'm not sure how to help you in this case. You need to debug your code and see why cache `set()` is not working. – Bizley Jan 25 '17 at 11:58
  • Maybe the problem is the key (as far as i know there are some limitations on the characters in the key) are you able te set a a value if create a hash from the key like ```\Yii::$app->cache->set(utf8_encode(md5('antiflood-'..$model->email)),1,86400);``` or ```\Yii::$app->cache->set(md5('antiflood-'..$model->email),1,86400);``` – Tim Jan 25 '17 at 13:14
  • @Tim I try but is same problem – Dev Jan 25 '17 at 14:00

0 Answers0