コード例 #1
0
 /**
  * @param $type
  *
  * @return string|\yii\web\Response
  * @throws NotFoundHttpException
  */
 public function actionCreate($type)
 {
     if (!in_array($type, array_keys(Redisman::$types))) {
         throw new NotFoundHttpException(Redisman::t('redisman', 'Unsupported type'));
     }
     $model = new RedisItem();
     $model->type = $type;
     $model->scenario = 'create';
     $lastlog = \Yii::$app->session->get('RedisManager_createlog', '');
     $lastlog = explode('[~lastlog~]', $lastlog);
     if (\Yii::$app->request->isPost) {
         if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
             $model->on(RedisItem::EVENT_AFTER_CHANGE, function ($event) use($lastlog) {
                 array_unshift($lastlog, $event->command);
                 \Yii::$app->session->set('RedisManager_createlog', implode('[~lastlog~]', $lastlog));
             });
             $model->create();
             \Yii::$app->session->setFlash('success', Redisman::t('redisman', 'Key created!'));
         } else {
             \Yii::$app->session->setFlash('error', Html::errorSummary($model, ['encode' => true]));
         }
         return $this->redirect(['create', 'type' => $type]);
     }
     return $this->render('create', compact('model', 'lastlog'));
 }
コード例 #2
0
 public function testCreate()
 {
     $model = new RedisItem();
     $model->scenario = 'create';
     $model->setAttributes(['key' => 'newstringkey', 'ttl' => 2000, 'formatvalue' => 'h ehfif ierireh ei', 'type' => 'string']);
     $this->assertTrue($model->validate());
     $model->create();
     $this->assertEquals(1, $this->module->executeCommand('EXISTS', ['newstringkey']));
     $tmodel = RedisItem::find('newstringkey')->findValue();
     $this->assertLessThanOrEqual(2000, $tmodel->ttl);
     $this->assertNotEquals(-1, $tmodel->ttl);
 }