Springboot + Redis (stand-alone version)

Source: Internet
Author: User

This is to share with you the use of Redis in Springboot, where the Redis Jedis client is used (here I docker running Redis, can refer to Docker to quickly build several popular third-party services ), Add dependencies as follows:

<dependency>  <groupId>redis.clients</groupId>  <artifactid>jedis</artifactid ></dependency>

Then you need the relevant configuration of Redis (my Redis password is empty here), in application.yml settings such as:

Spring:  redis:    192.168.  146.28:6378    Jedis:      pool:        Max8        Max8               maxpassword:    

This is the general configuration of Redis, where you can set these parameters, and read these settings in the Jedisconfig class below:

1@Value ("${spring.redis.single}")2     PrivateString Strsinglenode;3 4@Value ("${spring.redis.jedis.pool.max-idle}")5     PrivateInteger Maxidle;6 7@Value ("${spring.redis.jedis.pool.max-active}")8     PrivateInteger maxactive;9 Ten@Value ("${spring.redis.jedis.pool.max-wait}") One     PrivateInteger maxawait; A  -@Value ("${spring.redis.timeout}") -     PrivateInteger timeout; the  -@Value ("${spring.redis.password}") -     PrivateString password;

With the above configuration, you need to have the code inside the setup, here to create a return Jedispoolconfig method

1     /**2 * Jedis Configuration3      *4 * @return5      */6      Publicjedispoolconfig Getjedispoolconfig () {7Jedispoolconfig config =Newjedispoolconfig ();8 Config.setmaxidle (maxidle); #最大空闲数9 Config.setmaxwaitmillis (maxawait); #最大等待时间Ten config.setmaxtotal (maxactive); #最大连接数 One         returnconfig; A}

With the configuration, the next step is to create the Jedispool, where the Jedispool is hosted in spring

1   /**2 * Get Jedispool3      *4 * @return5      */6 @Bean7      PublicJedispool Getjedispool () {8Jedispoolconfig config =getjedispoolconfig ();9System. out. println ("Strsinglenode:"+ This. Strsinglenode);Tenstring[] Nodearr = This. Strsinglenode.split (":"); One  AJedispool Jedispool =NULL; -         if( This. Password.isempty ()) { -Jedispool =NewJedispool ( the Config, -nodearr[0], -Integer.valueof (nodearr[1]), -                      This. Timeout); +}Else { -Jedispool =NewJedispool ( + Config, Anodearr[0], atInteger.valueof (nodearr[1]), -                      This. Timeout, -                      This. password); -         } -         returnJedispool; -}

The above simple to distinguish the case of no password, to this Jedis configuration and connection pool is basically finished, the following is the method of encapsulation use, here is set and get as an example; first create a jediscomponent component, the code is as follows:

/** * Created by Administrator on 2018/8/18.*/@Component Public classjediscomponent {@Autowired jedispool jedispool;  PublicBooleanSet(String key, String val) {Jedis Jedis=NULL; Try{Jedis=Jedispool.getresource (); returnJedis.Set(Key, Val). Equalsignorecase ("OK"); } finally {            if(Jedis! =NULL) {jedis.close (); }        }    }     Public<T> BooleanSet(string key, T t) {string Strjson=jacksonconvert.serilize (t); if(Strjson.isempty ()) {return false; }        return  This.Set(key, Strjson); }     PublicStringGet(String key) {Jedis Jedis=NULL; Try{Jedis=Jedispool.getresource (); returnJedis.Get(key); } finally {            if(Jedis! =NULL) {jedis.close (); }        }    }     Public<T> TGet(String key, class<t>Tclass) {String Strjson= This.Get(key); returnjacksonconvert.deserilize (Strjson, Tclass); }}

With a call to Jedis encapsulation, our test cases at the controller layer are as follows:

1 @Autowired2 jediscomponent Jedis;3 4@GetMapping ("/setjedis/{val}")5      Publicboolean Setjedis (@PathVariable String val) {6         returnJedis.Set("token", Val);7     }8 9@GetMapping ("/getjedis")Ten      PublicString Getjedis () { One         returnJedis.Get("token"); A}

The interface effects that run set and get are as follows:

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.