@Override public void clearGroup(final String groupName, final boolean containPkCache) { String cacheGroupKey = groupName + CacheHandler.GROUPKEY_SUFFIX; JedisCommands commands = JedisProviderFactory.getJedisCommands(null); try { Set<String> keys = commands.zrange(cacheGroupKey, 0, -1); //删除实际的缓存 if (keys != null && keys.size() > 0) { RedisBatchCommand.removeObjects(keys.toArray(new String[0])); } commands.del(cacheGroupKey); //删除按ID缓存的 if (containPkCache) { keys = JedisProviderFactory.getMultiKeyCommands(null).keys(groupName + ".id:*"); if (keys != null && keys.size() > 0) { RedisBatchCommand.removeObjects(keys.toArray(new String[0])); } } } finally { JedisProviderFactory.getJedisProvider(null).release(); } }
public Set<String> hkeys(String key) { logger.trace("hkeys {}", key); JedisCommands client = dynoClient; Set<String> keys = new HashSet<>(); int cursor = 0; do { ScanResult<Entry<String, String>> sr = client.hscan(key, "" + cursor); cursor = Integer.parseInt(sr.getStringCursor()); List<Entry<String, String>> result = sr.getResult(); for (Entry<String, String> e : result) { keys.add(e.getKey()); } } while (cursor > 0); return keys; }
public Set<String> smembers(String key) { logger.trace("smembers {}", key); JedisCommands client = dynoClient; Set<String> r = new HashSet<>(); int cursor = 0; ScanParams sp = new ScanParams(); sp.count(50); do { ScanResult<String> sr = client.sscan(key, "" + cursor, sp); cursor = Integer.parseInt(sr.getStringCursor()); r.addAll(sr.getResult()); } while (cursor > 0); return r; }
@Before public void init() throws Exception { JedisCommands jedisMock = new JedisMock(); dao = new DynoQueueDAO(jedisMock, jedisMock, new ShardSupplier() { @Override public Set<String> getQueueShards() { return Arrays.asList("a").stream().collect(Collectors.toSet()); } @Override public String getCurrentShard() { return "a"; } }, new TestConfiguration()); }
@Override public void clearGroup(final String groupName,final boolean containPkCache) { String cacheGroupKey = groupName + CacheHandler.GROUPKEY_SUFFIX; JedisCommands commands = JedisProviderFactory.getJedisCommands(null); try { Set<String> keys = commands.zrange(cacheGroupKey, 0, -1); //删除实际的缓存 if(keys != null && keys.size() > 0){ RedisBatchCommand.removeObjects(keys.toArray(new String[0])); } commands.del(cacheGroupKey); //删除按ID缓存的 if(containPkCache){ keys = JedisProviderFactory.getMultiKeyCommands(null).keys(groupName +".id:*"); if(keys != null && keys.size() > 0){ RedisBatchCommand.removeObjects(keys.toArray(new String[0])); } } } finally{ JedisProviderFactory.getJedisProvider(null).release(); } }
@Override public WintCookie commitForCookie(final int expire) { redisClient.getRedisTemplate().executeNoResult(new RedisCommandNoResult() { @Override public void doInExec(JedisCommands commands) { String key = getRedisKey(); if (commands.exists(key)) { commands.expire(key, expire); } } }); WintCookie cookie = new WintCookie(config.getSessionIdName(), sessionId); String domain = config.getDomain(); if (!StringUtil.isEmpty(domain)) { cookie.setDomain(domain); } cookie.setHttpOnly(true); // 设置为非持久化cookie // cookie.setMaxAge(config.getExpire()); cookie.setPath(config.getPath()); return cookie; }
@Override public boolean exists(final String key) { JedisPool jedisPool = PoolThreadLocal.getPool(); return new AbstractCommandCall() { @Override public <T> T exec(JedisCommands commands) { return (T) commands.exists(key); } }.call(jedisPool); }
@Override public boolean expireat(final String key, final String val, final int cacheTime) { JedisPool jedisPool = PoolThreadLocal.getPool(); return new AbstractCommandCall() { @Override public <T> T exec(JedisCommands commands) { commands.set(key, val); if (cacheTime > 0) { commands.expire(key, cacheTime); } return (T) Boolean.TRUE; } }.call(jedisPool); }
@Override public String get(final String key) { JedisPool jedisPool = PoolThreadLocal.getPool(); return new AbstractCommandCall() { @Override public <T> T exec(JedisCommands commands) { return (T) commands.get(key); } }.call(jedisPool); }
@Override public boolean del(final String key) { JedisPool jedisPool = PoolThreadLocal.getPool(); return new AbstractCommandCall() { @Override public <T> T exec(JedisCommands commands) { commands.del(key); return (T) Boolean.TRUE; } }.call(jedisPool); }
private void interceptJedisCommands() { Class<JedisCommands> clazz = JedisCommands.class; Method[] methods = clazz.getDeclaredMethods(); Set<String> methodNames = new HashSet<>(); for (Method method : methods) { if (methodNames.contains(method.getName())) { continue;//over-loaded method } methodNames.add(method.getName()); addRulesForOperation(method); } }
@Override public void putGroup(String cacheGroupKey, String key, long expireSeconds) { long score = calcScoreInRegionKeysSet(expireSeconds); JedisCommands commands = JedisProviderFactory.getJedisCommands(null); try { commands.zadd(cacheGroupKey, score, key); commands.pexpire(cacheGroupKey, expireSeconds * 1000); } finally { JedisProviderFactory.getJedisProvider(null).release(); } }
@Override public void removeFromGroup(String cacheGroupKey, String key) { JedisCommands commands = JedisProviderFactory.getJedisCommands(null); try { commands.zrem(cacheGroupKey, key); // commands.del(key); } finally { JedisProviderFactory.getJedisProvider(null).release(); } }
@Override public void clearExpiredGroupKeys(String cacheGroup) { long maxScore = System.currentTimeMillis() / 1000 - this.baseScoreInRegionKeysSet; JedisCommands commands = JedisProviderFactory.getJedisCommands(null); try { commands.zremrangeByScore(cacheGroup, 0, maxScore); } finally { JedisProviderFactory.getJedisProvider(null).release(); } logger.debug("clearExpiredGroupKeys runing:cacheName:{} , score range:0~{}", cacheGroup, maxScore); }
public Map<String, String> hgetAll(String key) { Map<String, String> m = new HashMap<>(); JedisCommands dyno = dynoClient; int cursor = 0; do { ScanResult<Entry<String, String>> sr = dyno.hscan(key, "" + cursor); cursor = Integer.parseInt(sr.getStringCursor()); for (Entry<String, String> r : sr.getResult()) { m.put(r.getKey(), r.getValue()); } } while (cursor > 0); return m; }
public DynoQueueDAO(JedisCommands dynoClient, JedisCommands dynoClientRead, ShardSupplier ss, Configuration config) { this.dynoClient = dynoClient; this.dynoClientRead = dynoClient; this.ss = ss; this.config = config; init(); }
@Before public void init() { Configuration config = new TestConfiguration(); JedisCommands jedisMock = new JedisMock(); DynoProxy dynoClient = new DynoProxy(jedisMock); dao = new RedisMetadataDAO(dynoClient, om, config); }
@Override protected void configure() { System.setProperty("workflow.system.task.worker.callback.seconds", "0"); System.setProperty("workflow.system.task.worker.queue.size", "10000"); System.setProperty("workflow.system.task.worker.thread.count", "10"); configureExecutorService(); ConductorConfig config = new ConductorConfig(); bind(Configuration.class).toInstance(config); JedisCommands jedisMock = new JedisMock(); DynoQueueDAO queueDao = new DynoQueueDAO(jedisMock, jedisMock, new ShardSupplier() { @Override public Set<String> getQueueShards() { return Arrays.asList("a").stream().collect(Collectors.toSet()); } @Override public String getCurrentShard() { return "a"; } }, config); bind(MetadataDAO.class).to(RedisMetadataDAO.class); bind(ExecutionDAO.class).to(RedisExecutionDAO.class); bind(DynoQueueDAO.class).toInstance(queueDao); bind(QueueDAO.class).to(DynoQueueDAO.class); bind(IndexDAO.class).to(MockIndexDAO.class); DynoProxy proxy = new DynoProxy(jedisMock); bind(DynoProxy.class).toInstance(proxy); install(new CoreModule()); bind(UserTask.class).asEagerSingleton(); }
public ServerModule(JedisCommands jedis, HostSupplier hs, ConductorConfig config, ConductorServer.DB db) { this.dynoConn = jedis; this.hs = hs; this.config = config; this.region = config.getRegion(); this.localRack = config.getAvailabilityZone(); this.db = db; }
private WestCacheItem executeAndPut(Callable<WestCacheItem> callable, String redisKey, JedisCommands redis) { val item = Envs.execute(callable); Redis.expirePut(redis, redisKey, item); return item; }
private void setVersionToRedis(String cacheKey, String version, JedisCommands redis, String redisKey) { val versionKey = prefix + "version:" + cacheKey; val versionRedis = redis.get(versionKey); if (version.equals(versionRedis)) return; redis.del(redisKey); redis.set(versionKey, version); }
public static JedisCommands createJedisCommands( String host, int port, int maxTotal) { val poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(maxTotal); val pool = new JedisPool(poolConfig, host, port); return proxyJedisCommands(pool); }
public static boolean waitRedisLock(JedisCommands redis, String lockKey) { int maxWaitTimes = 10; while (maxWaitTimes-- > 0) { Long lock = redis.setnx(lockKey, "lock"); if (lock == 1L) return true; Envs.sleepMillis(50L); } return false; }
public static WestCacheItem getWestCacheItem(WestCacheOption option, JedisCommands redis, String redisKey) { val jsonValue = redis.get(redisKey); if (jsonValue == null) return null; val value = FastJsons.parse(jsonValue, option.getMethod(), true); if (value == null && !"null".equals(jsonValue)) return null; val optional = Optional.fromNullable(value); return new WestCacheItem(optional, option); }
public static String expirePut(JedisCommands redis, String redisKey, WestCacheItem item) { val duration = item.getDurationSeconds(); val json = FastJsons.json(item.orNull()); val result = redis.set(redisKey, json); if (duration > 0) { log.info("redis set {}={} in ttl {} seconds", redisKey, json, duration); redis.expire(redisKey, (int) duration); } return result; }
@Override public void putGroup(String cacheGroupKey, String key,long expireSeconds) { long score = calcScoreInRegionKeysSet(expireSeconds); JedisCommands commands = JedisProviderFactory.getJedisCommands(null); try { commands.zadd(cacheGroupKey, score, key); commands.pexpire(cacheGroupKey, expireSeconds * 1000); } finally{ JedisProviderFactory.getJedisProvider(null).release(); } }
@Override public void removeFromGroup(String cacheGroupKey, String key) { JedisCommands commands = JedisProviderFactory.getJedisCommands(null); try { commands.zrem(cacheGroupKey, key); // commands.del(key); } finally{ JedisProviderFactory.getJedisProvider(null).release(); } }
@Override public Map<String, Float> execute(final JedisCommands client, final SolrParams params) { final String key = ParamUtil.assertGetStringByName(params, "key"); log.debug("Fetching HVALS from Redis for key: {}", key); return ResultUtil.stringIteratorToMap(client.hvals(key)); }
@Override public Map<String, Float> execute(final JedisCommands client, final SolrParams params) { final String key = ParamUtil.assertGetStringByName(params, "key"); final long start = ParamUtil.tryGetIntByName(params, "range_start", 0); final long end = ParamUtil.tryGetIntByName(params, "range_end", -1); final boolean withScores = ParamUtil.tryGetBooleanByName(params, "with_scores", true); log.debug("Fetching ZREVRANGE from Redis for key: {} ({}, {})", key, start, end); if (withScores) { return ResultUtil.tupleIteratorToMap(client.zrevrangeWithScores(key, start, end)); } else { return ResultUtil.stringIteratorToMap(client.zrevrange(key, start, end)); } }
@Override public Map<String, Float> execute(final JedisCommands client, final SolrParams params) { final String key = ParamUtil.assertGetStringByName(params, "key"); log.debug("Fetching HKEYS from Redis for key: {}", key); return ResultUtil.stringIteratorToMap(client.hkeys(key)); }
@Override public Map<String, Float> execute(final JedisCommands client, final SolrParams params) { final String key = ParamUtil.assertGetStringByName(params, "key"); final String min = ParamUtil.tryGetStringByName(params, "min", "-inf"); final String max = ParamUtil.tryGetStringByName(params, "max", "+inf"); final boolean withScores = ParamUtil.tryGetBooleanByName(params, "with_scores", true); log.debug("Fetching ZREVRANGEBYSCORE from Redis for key: {} ({}, {})", key, min, max); if (withScores) { return ResultUtil.tupleIteratorToMap(client.zrevrangeByScoreWithScores(key, max, min)); } else { return ResultUtil.stringIteratorToMap(client.zrevrangeByScore(key, max, min)); } }
@Override public Map<String, Float> execute(final JedisCommands client, final SolrParams params) { final String key = ParamUtil.assertGetStringByName(params, "key"); final String min = ParamUtil.tryGetStringByName(params, "min", "-inf"); final String max = ParamUtil.tryGetStringByName(params, "max", "+inf"); final boolean withScores = ParamUtil.tryGetBooleanByName(params, "with_scores", true); log.debug("Fetching ZRANGEBYSCORE from Redis for key: {} ({}, {})", key, min, max); if (withScores) { return ResultUtil.tupleIteratorToMap(client.zrangeByScoreWithScores(key, min, max)); } else { return ResultUtil.stringIteratorToMap(client.zrangeByScore(key, min, max)); } }
@Override public Map<String, Float> execute(final JedisCommands client, final SolrParams params) { final String key = ParamUtil.assertGetStringByName(params, "key"); final String[] fields = ParamUtil.getStringByPrefix(params, "field"); log.debug("Fetching HMGET from Redis for key: {} ({})", key, fields); return ResultUtil.stringIteratorToMap(client.hmget(key, fields)); }
@Override public Map<String, Float> execute(final JedisCommands client, final SolrParams params) { final String key = ParamUtil.assertGetStringByName(params, "key"); final long start = ParamUtil.tryGetIntByName(params, "range_start", 0); final long end = ParamUtil.tryGetIntByName(params, "range_end", -1); final boolean withScores = ParamUtil.tryGetBooleanByName(params, "with_scores", true); log.debug("Fetching ZRANGE from Redis for key: {} ({}, {})", key, start, end); if (withScores) { return ResultUtil.tupleIteratorToMap(client.zrangeWithScores(key, start, end)); } else { return ResultUtil.stringIteratorToMap(client.zrange(key, start, end)); } }
@Override public Map<String, Float> execute(final JedisCommands client, final SolrParams params) { final String key = ParamUtil.assertGetStringByName(params, "key"); final int index = ParamUtil.tryGetIntByName(params, "index", 0); log.debug("Fetching LINDEX from Redis for key: {} ({})", key, index); return ResultUtil.stringIteratorToMap(Collections.singletonList(client.lindex(key, index))); }
@Override public Map<String, Float> execute(final JedisCommands client, final SolrParams params) { final String key = ParamUtil.assertGetStringByName(params, "key"); log.debug("Fetching SMEMBERS from Redis for key: {}", key); return ResultUtil.stringIteratorToMap(client.smembers(key)); }
@Override public Map<String, Float> execute(final JedisCommands client, final SolrParams params) { final String key = ParamUtil.assertGetStringByName(params, "key"); final int count = ParamUtil.tryGetIntByName(params, "count", 1); log.debug("Fetching SRANDMEMBER from Redis for key: {} ({})", key, count); // Workaround for https://github.com/xetorthio/jedis/issues/665 return client instanceof Jedis ? ResultUtil.stringIteratorToMap(((Jedis) client).srandmember(key, count)) : null; }
@Override public Map<String, Float> execute(final JedisCommands client, final SolrParams params) { final String key = ParamUtil.assertGetStringByName(params, "key"); final String field = ParamUtil.assertGetStringByName(params, "field"); log.debug("Fetching HGET from Redis for key: {} ({})", key, field); return ResultUtil.stringIteratorToMap(Collections.singletonList(client.hget(key, field))); }
@Override public String get(final String key) { return exec(new RedisCallback<String>() { @Override public String doCallback(JedisCommands jedis) { return jedis.get(key); } }); }