connect('127.0.0.1', 6379); // 模拟行情数据,每秒更新一次 while (true) { $time = time(); $data = array( 'symbol' => 'BTC/USD', 'price' => rand(50000, 60000), 'time' => $time, ); // 存入 Redis $redis->zAdd('quotes', $time, json_encode($data)); $redis->expire('quotes', 30); // 设置过期时间为30秒 // 获取最新20条数据 $quotes = $redis->zRevRange('quotes', 0, 19, true); $quotes = array_reverse($quotes); // 打印结果 echo "最新20条行情数据:\n"; foreach ($quotes as $quote) { $data = json_decode($quote, true); echo "{$data['symbol']} {$data['price']} {$data['time']}\n"; } // 等待1秒 sleep(1); } ? 编写代码 php redis 动态实时存入行情数据根据 ts时间戳存入 过期时间30秒 一直保存最新20条数据获取 最新20条数据

原文地址: https://www.cveoy.top/t/topic/fkz5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录