Spring Boot Practice 2--using Redis in projects

Source: Internet
Author: User

Background: Based on Practice 1, we use Redis as a cache.

(reprint please indicate source: cnblogs Coder-fang)

  1. Pom Added dependency:
      <  dependency  >  <  groupid  >  org.springframework.boot</  groupid  >  <  artifactid  >  Spring-boot-starter-data-redis</ artifactid  >  </ dependency  >  

     

  2. To add a configuration to the application.properties:
    Spring.redis.host=127.0.0.1spring.redis.port=6379spring.redis.pool.max-idle=8spring.redis.pool.max-active=8

  3. Create the Redisconfig class and inject the redistemplate bean:
     PackageCom.test.demo.config;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.data.redis.connection.RedisConnectionFactory;Importorg.springframework.data.redis.core.RedisTemplate;Importorg.springframework.data.redis.core.StringRedisTemplate; @Configuration Public classRedisconfig {@Bean PublicRedistemplate<string, string>redistemplate (Redisconnectionfactory factory) {stringredistemplate stringredistemplate=Newstringredistemplate (Factory); returnstringredistemplate; }}

  4. Implement Redisrepostory Universal functionality:
     PackageCom.test.demo.db.repo.nosql;ImportJava.util.concurrent.TimeUnit;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.data.redis.core.RedisTemplate;Importorg.springframework.stereotype.Repository; @Repository Public classredisrepository {@Autowired redistemplate<string, string>redistemplate;  Public voidAdd (String key,string value) {Redistemplate.opsforvalue (). Set (key, value); }     Public voidAdd (String key,string value,long time) {Redistemplate.opsforvalue (). Set (key, value, time, timeunit.minutes); }         Publicstring Get (String key) {returnRedistemplate.opsforvalue (). get (key); }         Public voidDelete (String key) {Redistemplate.opsforvalue (). GetOperations (). Delete (key); }    }

  5. To create a unit test and run:
     PackageCom.test.demo;Import Staticorg.junit.Assert.assertEquals;Import StaticOrg.junit.Assert.assertNull;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.boot.test.context.SpringBootTest;ImportOrg.springframework.test.context.junit4.SpringRunner;Importcom.test.demo.db.repo.nosql.RedisRepository; the @RunWith (Springrunner.class) @SpringBootTest Public classredistest {@Autowired redisrepository redisrepo; @Test Public voidTestredis () {Redisrepo.add ("Test", "123", 1L); String Val= Redisrepo.get ("Test"); Assertequals (Val,"123");        System.out.println (Val); Val= Redisrepo.get ("321");        System.out.println (Val);    Assertnull (Val); }}
    View Code

YES, Redis is completed in the project.

Spring Boot Practice 2--using Redis in projects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.