Springcloud+mybatis+redis Integration--Super Detailed example (ii)

Source: Internet
Author: User

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)

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.