小白一枚,曾经做访问量项目的时候经常会用到最简单的 “字段”+1 方式存储访问,这样无疑加重了数据库的工作量,并且数据也不够精确准确,在浏览扩展包教程发现了一个好东西并运用到了项目上面
他的功能主要用于 区分IP访问次数就是我最需要的了!!!tag也是用于区别存储信息很重要的东西
考虑到扩展包升级之类的问题就把github地址放在上面了,升级后观看相关文档就可以了->github地址
$ composer require awssat/laravel-visits
php artisan vendor:publish --provider="Awssat\Visits\VisitsServiceProvider"
.
.
.
'redis' => [
'client' => 'predis',
'default' => [
.
.
.
],
'laravel-visits' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 3, // anything from 1 to 15, except 0 (or what is set in default)
],
],
.
.
.
//根据百科id获取百科详细信息
public function getEncyclopedia($id)
{
$list=new Encyclopedium();
//阅读量+1
visits($list,$id)->increment();//这里$id为百科书籍ID,这样设置tag就能定位到每本书籍了
return $list->where('id',$id)->first(['id','title','quarry','created_at','textarea'])->toarray();
}
//根据一级分类以及二级分类查找百科列表
public function getEncyclopediaList($stage_id,$cate_id)
{
$list=new Encyclopedium();
//访问量不再使用数据库字段改用redis缓存
$list=$list->where(['encyclopedia_stage_id'=>$stage_id,'encyclopedia_cate_id'=>$cate_id])->get(['id','title','text'])->toarray();
foreach ($list as $k=>$v) {
$list[$k]['count_read']=visits(new Encyclopedium(),$list[$k]['id'])->count();
}
return $list;
}
这样访问数量就搬到了redis中,下面进行数据隔离的操作
<?php
return [
/*
|--------------------------------------------------------------------------
| Database Engine & Connection Name
|--------------------------------------------------------------------------
|
| Supported Engines: "redis", "eloquent"
| Connection Name: see config/database.php
|
*/
'engine' => 'redis',
'connection' => 'laravel-visits',
/*
|--------------------------------------------------------------------------
| Counters periods
|--------------------------------------------------------------------------
|
| Record visits (total) of each one of these periods in this set (can be empty)
|
*/
'periods' => [
'day',
'week',
'month',
'year',
],
/*
|--------------------------------------------------------------------------
| Redis prefix
|--------------------------------------------------------------------------
*/
'keys_prefix' => 'visits',
/*
|--------------------------------------------------------------------------
| Remember ip for x seconds of time
|--------------------------------------------------------------------------
|
| Will count only one visit of an IP during this specified time.
|
*/
'remember_ip' => 24 * 60 * 60,
/*
|--------------------------------------------------------------------------
| Always return uncached fresh top/low lists
|--------------------------------------------------------------------------
*/
'always_fresh' => false,
/*
|--------------------------------------------------------------------------
| Ignore Crawlers
|--------------------------------------------------------------------------
|
| Ignore counting crawlers visits or not
|
*/
'ignore_crawlers' => true,
/*
|--------------------------------------------------------------------------
| Global Ignore Recording
|--------------------------------------------------------------------------
|
| stop recording specific items (can be any of these: 'country', 'refer', 'periods', 'operatingSystem', 'language')
|
*/
'global_ignore' => ['country'],
];