redis-快取設計-統計最耗時方法
阿新 • • 發佈:2020-07-23
統計
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) throwsInterruptedException { 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