org.springframework.data.redis.connection.RedisConnectionFactory Java Examples
The following examples show how to use
org.springframework.data.redis.connection.RedisConnectionFactory.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomizedRedisCacheManager.java From jim-framework with Apache License 2.0 | 6 votes |
public CustomizedRedisCacheManager( RedisConnectionFactory connectionFactory, RedisOperations redisOperations, List<CacheItemConfig> cacheItemConfigList) { this( RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory), RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(30)), cacheItemConfigList .stream() .collect(Collectors.toMap(CacheItemConfig::getName,cacheItemConfig -> { RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration .defaultCacheConfig() .entryTtl(Duration.ofSeconds(cacheItemConfig.getExpiryTimeSecond())) .prefixKeysWith(cacheItemConfig.getName()); return cacheConfiguration; })) ); this.redisOperations=redisOperations; CacheContainer.init(cacheItemConfigList); }
Example #2
Source File: RedisCacheConfiguration.java From fw-cloud-framework with MIT License | 6 votes |
@Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, String> template = new RedisTemplate<>(); template.setConnectionFactory(factory); Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>( Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); // template.setKeySerializer(new StringRedisSerializer()); // template.setValueSerializer(new JdkSerializationRedisSerializer()); return template; }
Example #3
Source File: RedisConfig.java From biliob_backend with MIT License | 6 votes |
@Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式) Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); serializer.setObjectMapper(mapper); template.setValueSerializer(serializer); // 使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); template.afterPropertiesSet(); return template; }
Example #4
Source File: RedisConfigure.java From cms with Apache License 2.0 | 6 votes |
/** * 选择redis作为默认缓存工具 * * @param redisConnectionFactory * @return */ @Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { // 生成一个默认配置,通过config对象即可对缓存进行自定义配置 RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig() // 设置缓存的默认过期时间,也是使用Duration设置 .entryTtl(Duration.ofDays(7)) // 设置 key为string序列化 .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringSerializer())) // 设置value为json序列化 .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer())) // 不缓存空值 .disableCachingNullValues(); return RedisCacheManager .builder(redisConnectionFactory) .transactionAware() .cacheDefaults(configuration) .build(); }
Example #5
Source File: RedisUtils.java From yshopmall with Apache License 2.0 | 6 votes |
/** * 查找匹配key * @param pattern key * @return / */ public List<String> scan(String pattern) { ScanOptions options = ScanOptions.scanOptions().match(pattern).build(); RedisConnectionFactory factory = redisTemplate.getConnectionFactory(); RedisConnection rc = Objects.requireNonNull(factory).getConnection(); Cursor<byte[]> cursor = rc.scan(options); List<String> result = new ArrayList<>(); while (cursor.hasNext()) { result.add(new String(cursor.next())); } try { RedisConnectionUtils.releaseConnection(rc, factory); } catch (Exception e) { e.printStackTrace(); } return result; }
Example #6
Source File: RedisCacheConfig.java From ZTuoExchange_framework with MIT License | 6 votes |
/** * RedisTemplate配置 * @param factory * @return */ @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); //定义key序列化方式 //RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型会出现异常信息;需要我们上面的自定义key生成策略,一般没必要 //定义value的序列化方式 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); // template.setKeySerializer(redisSerializer); template.setValueSerializer(jackson2JsonRedisSerializer); template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; }
Example #7
Source File: RedisAutoConfig.java From yue-library with Apache License 2.0 | 6 votes |
/** * <p>支持FastJson进行Redis存储对象序列/反序列化 * <p>https://github.com/alibaba/fastjson/wiki/%E5%9C%A8-Spring-%E4%B8%AD%E9%9B%86%E6%88%90-Fastjson */ @Bean public RedisTemplate<String, Object> yueRedisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); // 支持FastJson进行Redis存储对象序列/反序列化 if (redisProperties.getRedisSerializerEnum() != RedisSerializerEnum.JDK) { redisTemplate.setDefaultSerializer(redisProperties.getRedisSerializerEnum().getRedisSerializer()); } StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); return redisTemplate; }
Example #8
Source File: RedisConfig.java From SpringBootLearn with Apache License 2.0 | 5 votes |
@Bean @ConditionalOnMissingBean(name = "redisTemplate") public RedisTemplate<Object, Object> redisTemplate( RedisConnectionFactory redisConnectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate<>(); //使用fastjson序列化 FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer<Object>(Object.class); // value值的序列化采用fastJsonRedisSerializer template.setValueSerializer(fastJsonRedisSerializer); // key的序列化采用StringRedisSerializer template.setKeySerializer(new StringRedisSerializer()); template.setConnectionFactory(redisConnectionFactory); return template; }
Example #9
Source File: DefaultRedisCacheWriter.java From api-gateway-old with Apache License 2.0 | 5 votes |
/** * @param connectionFactory must not be {@literal null}. * @param sleepTime sleep time between lock request attempts. Must not be {@literal null}. Use {@link Duration#ZERO} * to disable locking. */ DefaultRedisCacheWriter(RedisConnectionFactory connectionFactory, Duration sleepTime) { Assert.notNull(connectionFactory, "ConnectionFactory must not be null!"); Assert.notNull(sleepTime, "SleepTime must not be null!"); this.connectionFactory = connectionFactory; this.sleepTime = sleepTime; }
Example #10
Source File: RedisConnectionFactoryBeanPostProcessor.java From java-redis-client with Apache License 2.0 | 5 votes |
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof RedisConnectionFactory) { bean = new TracingRedisConnectionFactory((RedisConnectionFactory) bean, new TracingConfiguration.Builder(tracer).traceWithActiveSpanOnly(false).build()); } return bean; }
Example #11
Source File: JsonRedisSerializable.java From codeway_service with GNU General Public License v3.0 | 5 votes |
/** * 配置Redis * @param redisConnectionFactory redis连接工厂 */ @Bean @ConditionalOnMissingBean(name = "redisTemplate") public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); template.setDefaultSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); return template; }
Example #12
Source File: RedisConfig.java From JavaQuarkBBS with Apache License 2.0 | 5 votes |
@Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; }
Example #13
Source File: RedisUtil.java From spring-boot-demo with MIT License | 5 votes |
/** * 分页获取指定格式key,使用 scan 命令代替 keys 命令,在大数据量的情况下可以提高查询效率 * * @param patternKey key格式 * @param currentPage 当前页码 * @param pageSize 每页条数 * @return 分页获取指定格式key */ public PageResult<String> findKeysForPage(String patternKey, int currentPage, int pageSize) { ScanOptions options = ScanOptions.scanOptions() .match(patternKey) .build(); RedisConnectionFactory factory = stringRedisTemplate.getConnectionFactory(); RedisConnection rc = factory.getConnection(); Cursor<byte[]> cursor = rc.scan(options); List<String> result = Lists.newArrayList(); long tmpIndex = 0; int startIndex = (currentPage - 1) * pageSize; int end = currentPage * pageSize; while (cursor.hasNext()) { String key = new String(cursor.next()); if (tmpIndex >= startIndex && tmpIndex < end) { result.add(key); } tmpIndex++; } try { cursor.close(); RedisConnectionUtils.releaseConnection(rc, factory); } catch (Exception e) { log.warn("Redis连接关闭异常,", e); } return new PageResult<>(result, tmpIndex); }
Example #14
Source File: JedisLockProviderIntegrationTest.java From ShedLock with Apache License 2.0 | 5 votes |
private static RedisConnectionFactory createJedisConnectionFactory() { JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(); jedisConnectionFactory.setHostName(HOST); jedisConnectionFactory.setPort(PORT); jedisConnectionFactory.afterPropertiesSet(); return jedisConnectionFactory; }
Example #15
Source File: RedisListenerConfigure.java From spring-boot-tutorials with Apache License 2.0 | 5 votes |
@Bean public RedisMessageListenerContainer simpleListenerContainer(RedisConnectionFactory redisConnectionFactory){ RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(redisConnectionFactory); List<ChannelTopic> topics = Arrays.asList(new ChannelTopic(PubSubConstant.NEWS_CHANNEL)); container.addMessageListener(simpleMessageListenerAdapter(), topics); return container; }
Example #16
Source File: RedisCacheConfig.java From agile-service-old with Apache License 2.0 | 5 votes |
/** * 设置数据存入 redis 的序列化方式 * * @param redisTemplate redisTemplate * @param factory factory */ private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) { redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setConnectionFactory(factory); }
Example #17
Source File: RedisCacheConfig.java From syhthems-platform with MIT License | 5 votes |
@Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); template.setValueSerializer(this.jacksonSerializer); template.setKeySerializer(this.stringSerializer); template.setHashKeySerializer(this.stringSerializer); template.setHashValueSerializer(this.jacksonSerializer); template.afterPropertiesSet(); return template; }
Example #18
Source File: RedisUtils.java From yshopmall with Apache License 2.0 | 5 votes |
/** * 分页查询 key * @param patternKey key * @param page 页码 * @param size 每页数目 * @return / */ public List<String> findKeysForPage(String patternKey, int page, int size) { ScanOptions options = ScanOptions.scanOptions().match(patternKey).build(); RedisConnectionFactory factory = redisTemplate.getConnectionFactory(); RedisConnection rc = Objects.requireNonNull(factory).getConnection(); Cursor<byte[]> cursor = rc.scan(options); List<String> result = new ArrayList<>(size); int tmpIndex = 0; int fromIndex = page * size; int toIndex = page * size + size; while (cursor.hasNext()) { if (tmpIndex >= fromIndex && tmpIndex < toIndex) { result.add(new String(cursor.next())); tmpIndex++; continue; } // 获取到满足条件的数据后,就可以退出了 if(tmpIndex >=toIndex) { break; } tmpIndex++; cursor.next(); } try { RedisConnectionUtils.releaseConnection(rc, factory); } catch (Exception e) { e.printStackTrace(); } return result; }
Example #19
Source File: RedisHttpSessionConfigurationTests.java From spring-session with Apache License 2.0 | 5 votes |
@Test void primaryConnectionFactoryRedisConfig() { registerAndRefresh(RedisConfig.class, PrimaryConnectionFactoryRedisConfig.class); RedisIndexedSessionRepository repository = this.context.getBean(RedisIndexedSessionRepository.class); RedisConnectionFactory redisConnectionFactory = this.context.getBean("primaryRedisConnectionFactory", RedisConnectionFactory.class); assertThat(repository).isNotNull(); assertThat(redisConnectionFactory).isNotNull(); RedisOperations redisOperations = (RedisOperations) ReflectionTestUtils.getField(repository, "sessionRedisOperations"); assertThat(redisOperations).isNotNull(); assertThat(ReflectionTestUtils.getField(redisOperations, "connectionFactory")) .isEqualTo(redisConnectionFactory); }
Example #20
Source File: RedisUtils.java From rqueue with Apache License 2.0 | 5 votes |
public static <V> RedisTemplate<String, V> getRedisTemplate( RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, V> redisTemplate = new RedisTemplate<>(); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); RqueueRedisSerializer rqueueRedisSerializer = new RqueueRedisSerializer(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(rqueueRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setHashValueSerializer(rqueueRedisSerializer); return redisTemplate; }
Example #21
Source File: RedisConfig.java From spring-microservice-boilerplate with MIT License | 5 votes |
@Bean(name = "limitRedisTemplate") public RedisTemplate<String, RequestCount> limitRedisTemplate(RedisConnectionFactory cf) { RedisTemplate<String, RequestCount> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(RequestCount.class)); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer(RequestCount.class)); redisTemplate.setEnableTransactionSupport(true); redisTemplate.setConnectionFactory(cf); return redisTemplate; }
Example #22
Source File: RedisConfig.java From parker with MIT License | 5 votes |
@Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); // 配置连接工厂 template.setConnectionFactory(factory); //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式) Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jacksonSeial.setObjectMapper(om); // 值采用json序列化 template.setValueSerializer(jacksonSeial); //使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); // 设置hash key 和value序列化模式 template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(jacksonSeial); template.afterPropertiesSet(); return template; }
Example #23
Source File: RedisHttpSessionConfigurationClassPathXmlApplicationContextTests.java From spring-session with Apache License 2.0 | 5 votes |
static RedisConnectionFactory connectionFactory() { RedisConnectionFactory factory = mock(RedisConnectionFactory.class); RedisConnection connection = mock(RedisConnection.class); given(factory.getConnection()).willReturn(connection); given(connection.getConfig(anyString())).willReturn(new Properties()); return factory; }
Example #24
Source File: RedisConnectionFactoryBeanPostProcessor.java From java-redis-client with Apache License 2.0 | 5 votes |
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof RedisConnectionFactory) { bean = new TracingRedisConnectionFactory((RedisConnectionFactory) bean, new TracingConfiguration.Builder(tracer).traceWithActiveSpanOnly(false).build()); } return bean; }
Example #25
Source File: RqueueListenerBaseConfigTest.java From rqueue with Apache License 2.0 | 5 votes |
@Test public void testRqueueConfigSetConnectionFactoryFromBeanFactoryWithDbVersion() throws IllegalAccessException { SimpleRqueueListenerContainerFactory factory = new SimpleRqueueListenerContainerFactory(); RqueueListenerConfig rqueueSystemConfig = createConfig(factory); doReturn(redisConnectionFactory).when(beanFactory).getBean(RedisConnectionFactory.class); assertNotNull(rqueueSystemConfig.rqueueConfig(beanFactory, "__rq::version", 1)); assertNotNull(factory.getRedisConnectionFactory()); }
Example #26
Source File: RedisConfig.java From spring-boot-demo with MIT License | 5 votes |
/** * 配置使用注解的时候缓存配置,默认是序列化反序列化的形式,加上此配置则为 json 形式 */ @Bean public CacheManager cacheManager(RedisConnectionFactory factory) { // 配置序列化 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); RedisCacheConfiguration redisCacheConfiguration = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); return RedisCacheManager.builder(factory).cacheDefaults(redisCacheConfiguration).build(); }
Example #27
Source File: RedisConfiguration.java From spring-cloud-shop with MIT License | 5 votes |
@Bean public ShopRedisTemplate shopRedisTemplate( RedisConnectionFactory redisConnectionFactory) { ShopRedisTemplate template = new ShopRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; }
Example #28
Source File: RedisConnectionFactoryJavaConfigTest.java From spring-cloud-connectors with Apache License 2.0 | 5 votes |
@Test public void cloudRedisConnectionFactoryWithMaxPool() { ApplicationContext testContext = getTestApplicationContext(RedisConnectionFactoryConfigWithServiceConfig.class, createService("my-service")); RedisConnectionFactory connector = testContext.getBean("pool20Wait200", getConnectorType()); RedisConnectionFactoryCloudConfigTestHelper.assertPoolProperties(connector, 20, 0, 200); }
Example #29
Source File: RedisTestConfig.java From agile-service-old with Apache License 2.0 | 5 votes |
@Bean public RedisConnectionFactory connectionFactory() { RedisConnectionFactory factory = Mockito.mock(RedisConnectionFactory.class); RedisConnection connection = Mockito.mock(RedisConnection.class); Mockito.when(factory.getConnection()).thenReturn(connection); return factory; }
Example #30
Source File: CachingConfig2.java From Project with Apache License 2.0 | 5 votes |
@Bean public RedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory connectionFactory = new JedisConnectionFactory(); connectionFactory.setHostName("192.168.198.129"); connectionFactory.setPassword("123456"); return connectionFactory; }