松饼 / hits
Hits (查看) 计数器,用于 CakePHP 3 ORM。
Requires
- cakephp/orm: 3.*
Requires (Dev)
- cakephp/cakephp: ~3.0
- cakephp/cakephp-codesniffer: 2.*
- phpunit/phpunit: 4.1.*
This package is auto-updated.
Last update: 2024-08-28 00:50:24 UTC
README
Hits (查看) 计数器,用于 CakePHP 3 ORM。
安装
使用 Composer
composer require muffin/hits:1.0.x-dev
然后需要加载插件。您可以使用以下 shell 命令
bin/cake plugin load Muffin/Hits
或者通过手动将以下语句添加到 bootstrap.php
Plugin::load('Muffin/Hits');
用法
设置行为与 CounterCacheBehavior
类似,它接受一个字段列表作为配置。字段名可以是当前表或另一个表的名称(例如 view_count
或 OtherTable.posts_view_count
)。
$this->addBehavior('Muffin/Hits.Hits', ['view_count']);
或者多个字段,其他字段基于某些条件
$this->addBehavior('Muffin/Hits.Hits', [ // count only if the post is published 'view_count' => ['is_published' => true], // count all views 'total_view_count' ]);
或基于传递给 Model.beforeFind
事件的某些选项(例如认证用户)
$this->addBehavior('Muffin/Hits.Hits', [ // count only if the user viewing it is not an admin 'view_count' => function (\Cake\ORM\Query $query, \ArrayObject $options, $counter) { return !isset($options['_footprint']) || $options['_footprint']->is_admin === false; }, // count all views 'total_view_count' ]);
您还可以定义递增计数器的值(默认为 1
)
$this->addBehavior('Muffin/Hits.Hits', [ 'view_count' => ['increment' => 2] ]);
一次性使用所有这些功能
$this->addBehavior('Muffin/Hits.Hits', [ 'view_count' => [ 'conditions' => ['is_published' => true], 'callback' => function (\Cake\ORM\Query $query, \ArrayObject $options, $counter) { return !isset($options['_footprint']) || $options['_footprint']->is_admin === false; }, 'increment' => 2, ], 'total_view_count' ]);
策略
提供不同的策略来跟踪计数。
DefaultStrategy(array $conditions = [], $offset = 1) (默认)
此策略在有些情况下可能较慢,但它是使用最广泛的。它也是唯一允许传递额外条件的策略。在每次递增操作时都会击中数据库。
CacheStrategy(CacheEngine $cache, $threshold = 100, $offset = 1)
此策略用于最常用的计数器。它将计数缓存为间隔,并在达到阈值时才击中数据库。
SamplingStrategy(StrategyInterface $strategy, $size = 100)
为了最小化数据库访问次数,如果精确的数字不是问题,大型网站通常使用一种称为 抽样 的策略。它依赖于样本大小来生成一个随机数,在触发包装策略的 increment
方法之前(该策略的阈值应乘以样本大小)。
补丁和功能
- 分支
- 修改,修复
- 测试 - 这很重要,所以它不会无意中损坏
- 提交 - 不要修改许可证,todo,版本等。(如果您确实更改了任何内容,请将它们放入自己的提交中,以便我可以忽略在拉取时)
- 拉取请求 - 主题分支的加分项
为了确保您的 PR 被上游考虑,您必须遵循 CakePHP 编码标准。
错误和反馈
http://github.com/usemuffin/hits/issues
许可证
版权所有 (c) 2015, Use Muffin,许可协议为 MIT 许可协议。