2, Springcloud+mybatis+redis
Redis is a NoSQL database that stores data in the form of a key value <key,value>, which is compared to a database such as MySQL, which is equivalent to the difference between read and write memory and hard disk reading, so it is often used as a cache for less write-and-read scenarios. It is much faster to take data directly from the cache than from the database (database to I/O operations).
Not much to say, followed by the previous chapter, "Springcloud+mybatis+redis integration-Ultra-detailed example (a)" to build Springcloud+mybatis+redis environment:
- First step: Add in the Pom.xml file
1 <!--Redis Cache Consolidation starts -2 <Dependency>3 <groupId>Org.springframework.boot</groupId>4 <Artifactid>Spring-boot-starter-data-redis</Artifactid>5 </Dependency> 6 <!--Redis Cache Consolidation ended -
- Step two: Download the Windows version of Redis https://github.com/MSOpenTech/redis/releases
Open a cmd window using the CD command to switch directories to F:\dev-space\workspaces\newPlatform-2018\RedisForWindow run Redis-server.exe Redis.windows.conf . (Put the actual path of the file)
Step Three: Change the Controller class, add service class Let method have Redis cache
1 @Service2 Public classUserService {3 @Autowired4 PrivateUsermapper Usermapper;5 6@Cacheable (value= "user", key= "' User '")① 7 PublicUser Selectbyprimarykey (Integer id) {8System.out.println ("Start query ...");9 Try {TenThread.Sleep (3 * 1000l); One}Catch(interruptedexception e) { A e.printstacktrace (); - } -System.out.println ("End of query ..."); theUser user=Usermapper.selectbyprimarykey (ID); - - returnuser; - } + -}
1 @RestController2 Public classHellocontroller {3 @Autowired4 PrivateUserService UserService;5 6@RequestMapping ("/hello")7 PublicString Index () {8 LongBegintime=System.currenttimemillis ();9User user = Userservice.selectbyprimarykey (1);Ten LongTime=system.currenttimemillis ()-BeginTime; One return"Hello springboot" +user.getname () + ", Consumption query time:" +Time ; A - } - the -}
- Fourth step: In the page input Http://127.0.0.1:1111/hello, the first time you can see through the query and delay three seconds
After the second input, the query is fetched from the Redis cache, so the time is not delayed by three seconds and is very fast
From the cmd in this directory, enter REDIS-CLI get user to get:
Describes the object to be stored in the Redis cache.
At this point, Springcloud+mybats+redis build success!
Springcloud+mybatis+redis Integration--ultra-detailed example (ii)