Record the traffic transferred from the database to redis

Xiaobai is one. The simplest "field" + 1 method is often used to store access when doing traffic projects, which undoubtedly increases the workload of the database, and the data is not accurate enough. A good thing was found in browsing the extension package tutorial and applied to the project

awssat/laravel-visits

Its function is mainly used to distinguish IP access times, which is what I need most!!! tag is also an important thing to distinguish between storing information
Considering the problems such as the upgrade of the expansion package, put the github address on it. After the upgrade, you can view the relevant documents - > github address

  • Multiple statistical fields are supported in a model (tag is used to distinguish);
  • Support all data models (unlike some expansion packages, they are only User specific);
  • It supports distinguishing IP access times, and the number of views will not increase if the page is brushed several times (configurable);
  • Read the model list sorted by the most and least visits;
  • Countries with the highest number of visits;
  • Get the sorting list of month, year and all statistical traffic models.

1. First, install the expansion package using composer

$ composer require awssat/laravel-visits

2. Next, publish the configuration file

php artisan vendor:publish --provider="Awssat\Visits\VisitsServiceProvider"

3. Because redis is used to store traffic, it is necessary to ensure that redis runs normally

4. Create a new redis configuration in config/database.php for storage and access

.
.
.
    '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)
        ],
    ],
.
.
.

5. Since I'm doing the interface part, I've basically finished here. Next, I'll use visit () to store and read

The interface storage part $id is the accumulated quantity that can be filled in the ID increment() of each book in the encyclopedia. The default value is 1. The first parameter of visit is the instantiation of each model
 //Get encyclopedia details according to encyclopedia id
    public function getEncyclopedia($id)
    {
        $list=new Encyclopedium();
        //Reading amount + 1
        visits($list,$id)->increment();//Here $ID is the encyclopedia book ID, so you can set the tag to locate each book
        return $list->where('id',$id)->first(['id','title','quarry','created_at','textarea'])->toarray();
    }
The $list[$k] ['id'] in the loop of the interface reading part is the id of each book. Because it is a display list, it is read in the loop and added to the array
 //Search the encyclopedia list according to the primary classification and secondary classification
    public function getEncyclopediaList($stage_id,$cate_id)
    {
        $list=new Encyclopedium();
        //The database field is no longer used for traffic, and redis cache is used instead
        $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;
    }

In this way, the number of accesses is moved to redis. The following operations are performed for data isolation

6. Modify the visit configuration to realize ip isolation in config/visits.php. By default, ip isolation is also enabled, but the default time should be 15 minutes. If the configuration is completed, the data will be more accurate if each ip is recorded only once a day. If there are other requirements, you can add some redis configurations, and then add corresponding configurations in the configuration file, Modify connection and remeber_ip is OK. Because I am lazy, the whole configuration file is directly put on it

<?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'],

];

In this way, the whole task of moving the whole traffic from the database to redis is completed!!!

Give me an interface, I can connect a world!

Added by shiflett on Thu, 04 Nov 2021 13:54:57 +0200