<?php
namespace App\Http\Controllers;
use Mockery\Exception;
use \Redis;
class GoodsController extends Controller
{
protected $cate; // 分类
protected $redis; // redis实例
protected $goodsCategoryPrefix = 'goods_category_';
protected $goodsListInfoKey = 'goods_list_info';
protected $goodsCategoryExpireTime = 10; // 分钟
public function __construct()
{
$redis = new Redis;
$redis->connect('127.0.0.1');
$redis->select(6);
$this->redis = $redis;
$this->init();
$this->setGoodsList();
}
/**
* 初始化
*/
public function init()
{
$this->cate = $cate = intval(request('cate', 1));
}
/**
* 商品数据写入redis (维护数据)
*/
public function setGoodsList()
{
$cate = $this->cate;
$goods = [];
$goods[] = ['id' => 1, 'name' => 'asd1', 'price' => 10, 'sales' => 5, 'weight' => 10];
$goods[] = ['id' => 2, 'name' => 'asd2', 'price' => 20, 'sales' => 6, 'weight' => 9];
$goods[] = ['id' => 3, 'name' => 'asd3', 'price' => 30, 'sales' => 7, 'weight' => 5];
$goods[] = ['id' => 4, 'name' => 'asd4', 'price' => 40, 'sales' => 8, 'weight' => 7];
$goods[] = ['id' => 5, 'name' => 'asd5', 'price' => 50, 'sales' => 1, 'weight' => 1];
$goods[] = ['id' => 6, 'name' => 'asd6', 'price' => 50, 'sales' => 5, 'weight' => 5];
$goods[] = ['id' => 7, 'name' => 'asd7', 'price' => 70, 'sales' => 2, 'weight' => 2];
$currentTime = time();
// 初始化导入商品
if (!empty($cate) && $this->redis->hLen($this->goodsListInfoKey) < 1) {
//dd($this->redis->zRange($goodsCateKey ,0 ,-1,true));
//if( $this->redis->ttl($goodsCateKey) < 0){
$goodsHsahData = [];
foreach ($goods as $val) {
$goodsCateKey = $this->goodsCategoryPrefix . 'price_' . $cate;
$this->redis->zAdd($goodsCateKey, $val['price'], $val['id']);
$goodsCateKey = $this->goodsCategoryPrefix . 'sales_' . $cate;
$this->redis->zAdd($goodsCateKey, $val['sales'], $val['id']);
$goodsCateKey = $this->goodsCategoryPrefix . 'weight_' . $cate;
$this->redis->zAdd($goodsCateKey, $val['weight'], $val['id']);
$goodsHsahData[$val['id']] = serialize($val); // 将所有商品信息写入哈希表
}
$this->redis->hMSet($this->goodsListInfoKey, $goodsHsahData);
//$this->redis->expireAt($goodsCateKey, $currentTime + $this->goodsCategoryExpireTime * 60);
//}
}
}
/**
* 排序
* @return array
*/
public function sort()
{
$sortField = request('field', 'weight'); // 排序字段
$sortType = request('order', 'asc'); // 升降序 [ 'asc' , 'desc']
$scoreMin = request('min'); // 分数区间最小值 默认 -inf
$scoreMax = request('max'); // 分数区间最大值 默认 +inf
$page = intval(request('page', 1)); // 分页参数
$cate = $this->cate; // 商品分类
if (!in_array($sortField, ['price', 'sales', 'weight']) ||
!in_array($sortType, ['asc', 'desc'])
) {
throw new Exception('排序参数错误~~~~~~');
}
// 分页偏移
list($start, $end) = $this->setPage($page, 3);
// 分数区间
list($fieldScoreMin, $fieldScoreMax, $isNeedScore) = $this->setScore($scoreMin, $scoreMax);
// 需要排序的有序集合
$sortCateKey = $this->goodsCategoryPrefix . $sortField . '_' . $cate;
if ($sortType == 'asc' && !$isNeedScore) {
// 无区间排序(升序)
$goodsIds = $this->redis->zRange($sortCateKey, $start, $end);
} else if ($sortType == 'desc' && !$isNeedScore) {
// 无区间排序(降序序)
$goodsIds = $this->redis->zRevRange($sortCateKey, $start, $end);
} else if ($sortType == 'asc' && $isNeedScore) {
// 分区排序(升序)
$goodsIds = $this->redis->zRangeByScore($sortCateKey, $fieldScoreMin, $fieldScoreMax,
['limit' => [$start ,$end]]);
} else {
// 分区排序(降序)
$goodsIds = $this->redis->zRevRangeByScore($sortCateKey, $fieldScoreMax, $fieldScoreMin,
['limit' => [$start ,$end],'withscores' => false]);
}
// 从商品列表里面找出对应的商品
$lists = $this->redis->hMGet($this->goodsListInfoKey, $goodsIds);
if(false === $lists){
dd('没找到商品数据');
return [ 'status' => 0 , 'msg' => '没找到商品数据' ];
}
foreach ($lists as &$list) {
$list = json_encode(unserialize($list));
}
dd($lists);
}
/**
* 设置分页
* @param int $page
* @param int $offset
* @return array
*/
public function setPage(int $page = 1, $offset = 10)
{
if ($page < 1) $page = 1;
$start = ($page - 1) * $offset;
$end = $page * $offset;
return [$start, $end];
}
/**
* 设置区间
* @param string $min
* @param string $max
* @return array
*/
public function setScore($min = '-inf', $max = '+inf')
{
$numericMin = $min + 0; // 支持小数
$numericMax = $max + 0;
$isNeedScore = true;
if ((is_null($min) && is_null($max))) {
$isNeedScore = false;
} else if (!is_null($min) && !is_null($max)) {
} else {
$min = is_null($min) ? '-inf' : $numericMin;
$max = is_null($max) ? '+inf' : $numericMax;
}
return [$min, $max, $isNeedScore];
}
}
相关推荐
AI大模型学习(13)小白也能学会的10分钟豆包大模型的python教程1
豆包AI大模型API是字节跳动推出的智能对话接口服务,开发者可以通过简单的HTTP请求调用强大的AI能力,实现智能问答、内容生成等功能。
基于 Redis 有序集合实现 Laravel 热门浏览文章排行榜功能
原文:基于 Redis 有序集合实现 Laravel 热门浏览文章排行榜功能 | 实战入门篇 | 高性能 Redis 实战 自己修改的部分 <?php namespace App\Console\Commands; use App\Models\Post; use Illuminate\Console\Command; // use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Redis; //引用 use G
vue3+ts项目里如何使用状态管理pinia以及数据持久化
vue3 pinia 数据持久化
laravel + redis 实现排行榜
1.控制器 这里为什么要用redis有序集合 (sorted set) 因为 Redis 有序集合和集合一样也是 string 类型元素的集合,且不允许重复的成员。 不同的是每个元素都会关联一个 double 类型的分数。redis 正是通过分数来为集合中的成员进行从小到大的排序。 有序集合的成员是唯一的,但分数(score)却可以重复。 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。 集合中最大的成员数为 232- 1 (4294967295,...
从RRT到平滑轨迹:机械臂避障规划仿真全流程解析
本文详细解析了机械臂避障规划从RRT算法到平滑轨迹优化的全流程,包括MATLAB实现、碰撞检测工程实践及轨迹平滑优化技巧。通过RRT算法在关节空间的路径搜索与Minimum Jerk/Snap优化,实现机械臂在复杂环境中的高效安全运动,适用于工业自动化与机器人控制领域。
HFSS初学笔记(二)——75Ω带状线设计
本文利用HFSS软件设计并优化了一条75Ω的带状线结构,通过调整几何参数和介质材料,实现了精确的阻抗控制。仿真结果显示,该带状线在高频信号传输中的表现优越,具有良好的S参数、低插入损耗和极小的信号反射损耗。
Laravel中Redis有序合集常用指令
学习 laravel 框架中的redis 有序合集指令
R语言影像组学实战:从医学图像到预测模型的完整流程(附代码)
本文详细介绍了使用R语言进行影像组学分析的完整实战流程。从环境搭建、数据准备,到医学图像的特征提取与筛选,再到利用机器学习构建预测模型并进行评估,提供了清晰的步骤和可运行的R代码示例,帮助研究者和数据分析师掌握从医学图像到临床预测模型的核心技术。
laravel从入门到精通之redis配置和使用
1:首先我们先使用 Composer 安装依赖: $ composer require "predis/predis:~1.0" 2:确保运行环境下已安装redis 具体安装可查看:https://blog.csdn.net/qq_29099209/article/details/79787067 3:操作说明 3.1:实例化Redis use Illuminate\Suppo...
Ubuntu声卡检测与故障排查:从基础命令到驱动修复
本文详细介绍了在Ubuntu系统中进行声卡检测与故障排查的完整流程。从使用lspci、aplay等基础命令识别硬件,到排查驱动冲突、修复权限问题,再到调整PulseAudio高级配置,提供了一套系统性的解决方案。文章结合实战案例,帮助用户快速诊断并解决Ubuntu系统常见的“无声”问题,恢复音频功能。
QT日志输出终极指南:从qDebug到qFatal的完整文件记录方案
本文深入探讨了QT日志系统的企业级文件记录方案,从基础的qDebug、qWarning、qCritical、qFatal等宏的使用,到构建自定义消息处理器、实现异步写入、分级存储、日志轮转及性能监控等高级功能。旨在帮助开发者构建一个稳定、高效且易于维护的专业级日志管理架构,适用于桌面应用、嵌入式系统及服务端程序。
laravel redis操作大全 (转载)
入门级操作普通 set / get 操作; set操作,如果键名存在,则会覆盖原有的值;$redis = app('redis.connection'); $redis->set('library', 'predis'); // 存储 key 为 library, 值为 predis 的记录; $redis->get('library'); // 获取 key 为 library 的记...
汉书HE8,好用的汇编代码编写工具
汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大
laravel Redis 锁机制代码
<?php namespace App\Service; use Illuminate\Support\Facades\Redis; class RedisLockService { public static function lock($key, $expire = 10) { if(!$key) { return false; } do { if($acquired = (
visio安装包
Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。
计算机控制技术及应用
《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。
redis有序集合实现实时排名
需求:如何快速简单的进行论坛文章浏览次数排名,减少对数据库、后端程序的请求处理等。。。 解决方案:采用redis中的有序集合,针对实时排名的功能 1.根据帖子id,作为value,用zadd(新增)或者zincrby更新(添加) 2.移除指定元素,用zrem 3.用zrevrange(从大到小)或者zrange(从小到大),获取排序后的数据 4.排序后的数据作为查询条件,进行mysql数据查询...
关于Redis在Laravel中设置过期时间的使用纠正
(String)过期时间正确设置方法:Redis::setex( $key , $expired_at , $value );$expired_at 就是过期时间,单位秒
laravel基于redis实现的一个简单的秒杀系统
说明:网上很多redis秒杀系统的文章,看的都是一头雾水,然后自己来实现一个,也方便以后自己学习 实现的方式是用的redis的list队列,框架为laravel 核心部分为list的pop操作,此操作为原子性,即使很多用户同时到达,也是依次执行 示例: <?php /** * Created by PhpStorm. * User: chenyRain * D...
php操作redis大全
php.ini文件添加:extension=php_redis.dll 重启php, phpinfo可以看到redis则证明安装成功 php连接redis测试 <?php $redis = new Redis(); $redis->connect('127.0.0.1', 6379);//serverip port $redis->auth('mypassword');//my redis password $redis ->set( "test" , "Hello Worl
Laravel自定义排序
如果数据库的status字段有0,1,2,3几种状态,如果想让status为1,2的状态排在最前面 那么可以这样: $obj = $obj->orderByRaw(DB::raw('FIELD(status, 1,2) desc')); 得到的sql是 select * from 表 where 条件 order by FIELD('status',[1,2])...
laravel + redis 实现积分排行榜
laravel + redis 实现积分排行榜
产品列表页分类筛选、排序的算法实现(PHP)
一、简单的单条件查询 工作都是从简单的开始,先从最简单的单表查询开始,这个一般用在首页以及一些比较独立的页面,只需要查找几个符合条件的产品展示出来即可,可以使用分页或者不使用分页。下面这个是产品控制器 ProductController 中的一个函数,用于简单的查询,比如199元专区就可以使用 getTypeSimPro('price=199'); /**简单的筛选条件分类产品,单表查询 ...
1008



被折叠的 条评论
为什么被折叠?



