使用redis记录产品的每日浏览量

全体听令,除我之外~冲锋!

配置文件database.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
'redis' => [

'cluster' => false,

'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],

'product' => [
'host' => 'localhost',
'password' => null,
'port' => 6379,
'database' => 1,
]
],

连接存储

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 连接
public static function connectDatabase($name)
{
return Redis::connection($name);
}

// 存储
$redisConnection = parent::getRedisConnectName();
$redis = RedisHelper::connectDatabase($redisConnection['store']);
$redis->rpush('productView', $product_id);

// 取出
$products = array_count_values($redis->lrange('productView',0,-1));

// 删除
$redis->del('productView');