/** * Executed wrapped command upon {@link RedisClusterConnection}. * * @param callback must not be {@literal null}. * @return execution result. Can be {@literal null}. */ @Nullable public <T> T execute(RedisClusterCallback<T> callback) { Assert.notNull(callback, "ClusterCallback must not be null!"); RedisClusterConnection connection = template.getConnectionFactory().getClusterConnection(); try { return callback.doInRedis(connection); } finally { connection.close(); } } }
private void initMessageListenerContainer() { this.messageListenerContainer = new RedisMessageListenerContainer(); this.messageListenerContainer.setConnectionFactory(((RedisTemplate<?, ?>) redisOps).getConnectionFactory()); this.messageListenerContainer.afterPropertiesSet(); this.messageListenerContainer.start(); }
@Override public Map<Long, CIMServerResVO> loadRouteRelated() { Map<Long, CIMServerResVO> routes = new HashMap<>(64); RedisConnection connection = redisTemplate.getConnectionFactory().getConnection(); ScanOptions options = ScanOptions.scanOptions() .match(ROUTE_PREFIX + "*") .build(); Cursor<byte[]> scan = connection.scan(options); while (scan.hasNext()) { byte[] next = scan.next(); String key = new String(next, StandardCharsets.UTF_8); LOGGER.info("key={}", key); parseServerInfo(routes, key); } try { scan.close(); } catch (IOException e) { LOGGER.error("IOException",e); } return routes; }
private void processInPipeline(PipelineCallback callback) { RedisConnectionFactory connectionFactoryForPipeline = this.redisTemplate.getConnectionFactory(); Assert.state(connectionFactoryForPipeline != null, "RedisTemplate returned no connection factory"); RedisConnection connection = RedisConnectionUtils.bindConnection(connectionFactoryForPipeline); try { connection.openPipeline(); callback.process(); } finally { connection.closePipeline(); RedisConnectionUtils.unbindConnection(connectionFactoryForPipeline); } }
/** * 获取链接工厂 */ public RedisConnectionFactory getConnectionFactory() { return this.redisTemplate.getConnectionFactory(); }
/** * 获取链接工厂 */ public RedisConnectionFactory getConnectionFactory() { return this.redisTemplate.getConnectionFactory(); }
@Override public boolean isLeader() { byte[] leaderIdBytes = redisTemplate.getConnectionFactory().getConnection().get( key.getBytes() ); String leaderId = null; if ( leaderIdBytes != null ) { leaderId = new String( leaderIdBytes ); } return nodeId.equals( leaderId ); }
@Override public void renewLeader() { if ( isLeader() ) { log.debug( "Renewing leader with nodeId:" + this.nodeId ); redisTemplate.getConnectionFactory().getConnection().expire( key.getBytes(), timeToLiveSeconds ); } }
/** * @Title: generate * @Description: Atomically adds the given value to the current value. * @param key * @param increment * @return */ public long generate(String key, int increment) { RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory()); return counter.addAndGet(increment); }
@Override public void publish(String topic, String message) { logger.trace("[MQ] publish {}:{}", topic, message); RedisConnection connection = null; try { connection = redisTemplate.getConnectionFactory().getConnection(); connection.publish(topic.getBytes(), message.getBytes()); } finally { if (connection != null && !connection.isClosed()) { connection.close(); } } }
@Override public void request(String address, String message) { logger.trace("[MQ] request {}:{}", address, message); RedisConnection connection = null; try { connection = redisTemplate.getConnectionFactory().getConnection(); connection.lPush(address.getBytes(), message.getBytes()); } finally { if (connection != null && !connection.isClosed()) { connection.close(); } } }
@RateLimit(key = "test", time = 10, count = 10) @GetMapping("/test") public String luaLimiter() { RedisAtomicInteger entityIdCounter = new RedisAtomicInteger("entityIdCounter", redisTemplate.getConnectionFactory()); String date = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"); return date + " 累计访问次数:" + entityIdCounter.getAndIncrement(); } }
@PostConstruct public void init() throws IOException { logger.info("Enabled Dew Test"); redisServer = new RedisServer(); if (!redisServer.isActive()) { try { redisServer.start(); redisTemplate.getConnectionFactory().getConnection(); } catch (Exception e) { logger.error("Start embedded redis error.", e); } } }
@PostConstruct public void init() throws IOException { log.info("======================= Enabled Devx Test ======================="); redisServer = new RedisServer(); if (!redisServer.isActive()) { try { redisServer.start(); redisTemplate.getConnectionFactory().getConnection(); } catch (Exception e) { log.error("Start embedded redis error.", e); } } }
/** * @Title: generate * @Description: Atomically increments by one the current value. * @param key * @return */ public long generate(String key, Date expireTime) { RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory()); counter.expireAt(expireTime); return counter.incrementAndGet(); }
@Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { //全局redis缓存过期时间 RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(1)); RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory()); return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration); } }
private void initMessageListenerContainer() { this.messageListenerContainer = new RedisMessageListenerContainer(); this.messageListenerContainer.setConnectionFactory(((RedisTemplate<?, ?>) redisOps).getConnectionFactory()); this.messageListenerContainer.afterPropertiesSet(); this.messageListenerContainer.start(); }
private boolean putLockKey(long leaseMillSec) { RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection(); String res = ((JedisCommands) redisConnection.getNativeConnection()).set(key, getCurrThreadId(), "NX", "PX", leaseMillSec); redisConnection.close(); return "OK".equalsIgnoreCase(res); }
@Override public void afterPropertiesSet() throws Exception { messageListener.setCacheManager(this); container.setConnectionFactory(getRedisTemplate().getConnectionFactory()); container.afterPropertiesSet(); messageListener.afterPropertiesSet(); BeanFactory.getBean(StatsService.class).setCacheManager(this); if (getStats()) { // 采集缓存命中率数据 BeanFactory.getBean(StatsService.class).syncCacheStats(); } }
@Override public void afterPropertiesSet() throws Exception { messageListener.setCacheManager(this); container.setConnectionFactory(getRedisTemplate().getConnectionFactory()); container.afterPropertiesSet(); messageListener.afterPropertiesSet(); BeanFactory.getBean(StatsService.class).setCacheManager(this); if (getStats()) { // 采集缓存命中率数据 BeanFactory.getBean(StatsService.class).syncCacheStats(); } }