Spring-boot-starter-data-redis

Source: Internet
Author: User

Github:https://github.com/asd821300801/spring-boot/tree/spring-boot-redis Preliminary Preparation


Create Spring Boot project

......


Maven joins the necessary dependencies


<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId> spring-boot-starter-data-redis</artifactid>
    <version>1.5.7.RELEASE</version>
</ Dependency>


application.properties


spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=123456
Spring.redis.database=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=500
spring.redis.pool.min-idle=0
spring.redis.timeout=0


Start Redis



Connect Redis to do the corresponding data operation


View Source Redistemplate and Stringredistemplate have been automatically configured, so we can use it directly

Org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration.class

Redisdao.java

Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.data.redis.core.StringRedisTemplate;
Import org.springframework.data.redis.core.ValueOperations;
Import org.springframework.stereotype.Repository;

@Repository public
class Redisdao {

    @Autowired
    private stringredistemplate template;

    Public  void Setkey (String key,string value) {
        valueoperations<string, string> ops = Template.opsforvalue ();
        Ops.set (Key,value);
    }

    public string GetValue (string key) {
        valueoperations<string, string> ops = This.template.opsForValue ();
        return Ops.get (key);
    }


Rediscontroller.java

Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RestController;

Import Com.dao.RedisDao;

@RestController public
class Rediscontroller {
    @Autowired
    private Redisdao Redisdao;

    @RequestMapping ("/set") public
    string Set (string key,string value) {
        Redisdao.setkey (key, value);
        Return "Success";
    }
    @RequestMapping ("/get") public
    String get (string key) {return
        Redisdao.getvalue (key)}


Access Test


Set data: http://localhost:8080/set?key=lingdu&value=123456

Get Data: Http://localhost:8080/get?key=lingdu

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.