track hits, closes #16

This commit is contained in:
cornernote 2015-08-11 22:07:23 +09:30
parent 778e58594b
commit 220f04c21e
3 changed files with 19 additions and 16 deletions

View File

@ -39,8 +39,15 @@ class ModController extends Controller
public function actionView($name)
{
$model = $this->findModel($name);
//$model->hits++;
//$model->save(false, ['hits']); // todo, this breaks serialize
// add a hit once per user per 24 hours
$cacheKey = $model->name . '_' . Yii::$app->request->userIP;
if (!Yii::$app->cache->get($cacheKey)) {
Yii::$app->cache->set($cacheKey, true, (60 * 60 * 24));
$model->hits++;
$model->save(false, ['hits']);
}
return $this->render('view', [
'model' => $model,
]);
@ -99,15 +106,4 @@ class ModController extends Controller
throw new NotFoundHttpException('The requested page does not exist.');
}
}
public function actionTest()
{
$test = Yii::$app->cache->get('test');
if (!$test) {
$test = 0;
}
$test++;
Yii::$app->cache->set('test', $test);
echo $test;
}
}

View File

@ -69,8 +69,15 @@ class PackageController extends Controller
->where(['name' => $name])
->one();
if ($package) {
//$package->hits++;
//$package->save(false, ['hits']); // todo, this breaks serialize
// add a hit once per user per 24 hours
$cacheKey = $package->name . '_' . Yii::$app->request->userIP;
if (!Yii::$app->cache->get($cacheKey)) {
Yii::$app->cache->set($cacheKey, true, (60 * 60 * 24));
$package->hits++;
$package->save(false, ['hits']);
}
return $package;
}
throw new HttpException(404, 'Package not found.');

View File

@ -51,7 +51,7 @@ $this->params['breadcrumbs'][] = $this->title;
'value' => $model->getLicenseHtml(),
'format' => 'raw',
],
//'hits',
'hits',
'created_at',
[
'attribute' => 'updated_at',