1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
public T get(String key, Callable<? extends T> loader) { String cache = redisHelper.get(key); if (StringUtils.isNotBlank(cache)) { if ("null".equals(cache)) { return null; } return JSONObject.parseObject(cache, this.entityClass); } String lockKey = "lock:" + key; String lockVal = String.valueOf(RandomUtils.nextInt(1000, 9999)); if (redisHelper.lock(lockKey, lockVal, 3, TimeUnit.SECONDS)) { T entity = loader.call(); if (entity != null) { redisHelper.set(key, JsonHelper.toJson(entity), RandomUtils.nextInt(3600, 7200)); } else { redisHelper.set(key, "null", 10); } redisHelper.unlock(lockKey, lockVal); return entity; } else { TimeUnit.MILLISECONDS.sleep(RandomUtils.nextInt(100, 200)); return this.get(key, loader); } return null; }
|