Objective:
Really more and more like Springboot, this is one of the Springboot Learning series.
Body:
1: First add dependency in pom file, Remember is Spring-boot-starter-data-redis, not Spring-boot-starter-redis
1 <!--Redis -2 <Dependency>3 <groupId>Org.springframework.boot</groupId>4 <Artifactid>Spring-boot-starter-data-redis</Artifactid>5 </Dependency>
2: Step two Configure the data source in the properties file
1 # Redis 2 spring.redis.host=120.78.159.xxx 3 spring.redis.port=xxxx 4 spring.redis.password=xxxx
3: Third step, direct injection can
1 @Autowired 2 stringredistemplate Redis;
is not very cool, I used to be stunned, how can be so simple, so concise, too feel the open source community of the great God invented the Springboot this framework.
Attach the official api:https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/ Stringredistemplate.html
Usage:
1: Test whether Redis is successful, and take some key values out
1 //Test Redis2@RequestMapping (value = "/testredis", method =requestmethod.get)3 PublicString Testredis () {4 5list<string> list =NewArraylist<>();6List.add ("K1");7List.add ("K2");8List.add ("K3");9 System.out.println (Redis.opsforvalue (). Multiget (list));Ten One returnRedis.opsforvalue (). Multiget (list). toString (); A}
2: Test set data into Redis, and set timeout mechanism
1 //put key2@RequestMapping (value = "/putredis/{id}", method =requestmethod.get)3 PublicString Putredis (@PathVariable (value = "id") (String ID) {4 5String key = "Test:" +ID;6String value = "I Love Zxx";7 Redis.opsforvalue (). Set (key, value);8 Redis.expire (Key, Integer.max_value, timeunit.seconds);9 Ten return"Put Key to Redis"; One}
3: Test from Redis inside get data out
1 // Get key 2 @RequestMapping (value = "/getredis/{id}", Method = Requestmethod.get) 3 public string Getredis (@PathVariable (value = "id" 4 5 String key =" Test: "+ ID; 6 System.out.println (Redis.opsforvalue (). Get ( key)); 7 8 r Eturn "Get key from Redis" ; 9 }
"Springboot Series 2" Springboot Integrated Redis