redis-缓存设计-统计最耗时方法
统计
public static void addLog(Jedis conn, String methodName, Long startTime, Long endTime) { conn.zadd("timeLog", endTime - startTime, methodName); //只保留前2 conn.zremrangeByRank("timeLog",0,-3); }
打印
public static void printTimeLog(Jedis conn){ Set<Tuple> tuples= conn.zrevrangeWithScores("timeLog",0,-1); for (Tuple tuple: tuples) { System.out.println(tuple.getElement()+":"+tuple.getScore()); } }
测试
public static void main(String[] args) throws Exception { Jedis conn = new Jedis("127.0.0.1", 6379); conn.flushDB(); test1(conn); test2(conn); test3(conn); printTimeLog(conn); } public static void test1(Jedis conn) throws InterruptedException { Long startTime = System.currentTimeMillis(); Thread.sleep(1000); Long endTime = System.currentTimeMillis(); addLog(conn, "test1", startTime, endTime); } public static void test2(Jedis conn) throws InterruptedException { Long startTime = System.currentTimeMillis(); Thread.sleep(3000); Long endTime = System.currentTimeMillis(); addLog(conn, "test2", startTime, endTime); } public static void test3(Jedis conn) throws InterruptedException { Long startTime = System.currentTimeMillis(); Thread.sleep(10000); Long endTime = System.currentTimeMillis(); addLog(conn, "test3", startTime, endTime); }
打印
test3:10003.0
test2:3005.0
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
2018-07-23 Zookeeper-单机/集群安装