spring boot(三):Spring Boot中Redis的使用

來源:互聯網
上載者:User

spring boot對常用的資料庫支援外,對nosql 資料庫也進行了封裝自動化。 redis介紹

Redis是目前業界使用最廣泛的記憶體資料存放區。相比memcached,Redis支援更豐富的資料結構,例如hashes, lists, sets等,同時支援資料持久化。除此之外,Redis還提供一些類資料庫的特性,比如事務,HA,主從庫。可以說Redis兼具了緩衝系統和資料庫的一些特性,因此有著豐富的應用情境。本文介紹Redis在Spring Boot中兩個典型的應用情境。 如何使用

1、引入 spring-boot-starter-redis

<dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-redis</artifactId>  </dependency>  

2、添加設定檔

# REDIS (RedisProperties)# Redis資料庫索引(預設為0)spring.redis.database=0  # Redis伺服器位址spring.redis.host=192.168.0.58# Redis伺服器串連連接埠spring.redis.port=6379  # Redis伺服器串連密碼(預設為空白)spring.redis.password=  # 串連池最大串連數(使用負值表示沒有限制)spring.redis.pool.max-active=8  # 串連池最大阻塞等待時間(使用負值表示沒有限制)spring.redis.pool.max-wait=-1  # 串連池中的最大空閑串連spring.redis.pool.max-idle=8  # 串連池中的最小空閑串連spring.redis.pool.min-idle=0  # 連線逾時時間(毫秒)spring.redis.timeout=0  

3、添加cache的配置類

@Configuration@EnableCachingpublic class RedisConfig extends CachingConfigurerSupport{        @Bean    public KeyGenerator keyGenerator() {        return new KeyGenerator() {            @Override            public Object generate(Object target, Method method, Object... params) {                StringBuilder sb = new StringBuilder();                sb.append(target.getClass().getName());                sb.append(method.getName());                for (Object obj : params) {                    sb.append(obj.toString());                }                return sb.toString();            }        };    }    @SuppressWarnings("rawtypes")    @Bean    public CacheManager cacheManager(RedisTemplate redisTemplate) {        RedisCacheManager rcm = new RedisCacheManager(redisTemplate);        //設定緩衝到期時間        //rcm.setDefaultExpiration(60);//秒        return rcm;    }        @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;    }}

3、好了,接下來就可以直接使用了

@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(Application.class)public class TestRedis {    @Autowired    private StringRedisTemplate stringRedisTemplate;        @Autowired    private RedisTemplate redisTemplate;    @Test    public void test() throws Exception {        stringRedisTemplate.opsForValue().set("aaa", "111");        Assert.assertEquals("111", stringRedisTemplate.
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.