Tag: INF Port this operation Alt. class share CTI File
Introduce dependencies:
To add a redis dependency in the Pom file:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-data-redis</artifactid></dependency>
Configure the data source
Spring.redis.host=localhostspring.redis.port=6379#spring.redis.password=spring.redis.database= 1spring.redis.pool.max-active=8spring.redis.pool.max-wait=-1spring.redis.pool.max-idle= 500spring.redis.pool.min-idle=0spring.redis.timeout=0
If your redis has a password, you can configure it. With this two-step operation, you can access the Redis data.
Data Access Layer DAO
Access to Redis via Redistemplate.
@Repositorypublic class Redisdao { @Autowired private stringredistemplate template; Public void Setkey (String key,string value) { valueoperations<string, string> ops = Template.opsforvalue (); Ops.set (key,value,1, timeunit.minutes);//1 minute expires } public String GetValue (string key) { valueoperations <string, string> Ops = This.template.opsForValue (); return Ops.get (key);} }
Unit Test
@RunWith (springrunner.class) @SpringBootTestpublic class Springbootredisapplicationtests {public static Logger Logger= Loggerfactory.getlogger (springbootredisapplicationtests.class); @Test public void Contextloads () { } @Autowired Redisdao Redisdao; @Test public void Testredis () { redisdao.setkey ("name", "Forezp"); Redisdao.setkey ("Age", "one"); Logger.info (Redisdao.getvalue ("name")); Logger.info (Redisdao.getvalue ("Age"));} }
To start the unit test, you find that the console is printed: unit test passed;
Forezp11
Source Source
Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (23) Integrated Redis