Spring Boot Integrated Redis

Source: Internet
Author: User
Tags install redis redis server

Own development environment needs to install Redis service, Baidu a lot, the following main description Springboot integrated Redis explained

My version java8 + redis3.0 + springboot 1.5.9. Spring Redis integrates the Jedis

The bytes is stored in Redis

1 Spring boot already supports integrated Redis, and only adds dependencies in MVN. The POM configuration fragment is as follows

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId> spring-boot-starter-parent</artifactid>    <version>1.5.  9. release</version>  </parent> ... <!--adding Redis cache Support-    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-data-redis</artifactId>    </dependency>

1.2 Configuring Redis, modifying configuration Files Application.properties

# # Redis Configuration # # Redis Database index (default is 0) spring.redis.database=0# # Redis server address Spring.redis.host=127.0.0.1# # Redis Server connection Port Spring.redis.port=6379# # Redis Server connection password (default is empty) Spring.redis.password=# # Connection pool Maximum number of connections (use negative values to indicate no limit) Spring.redis.pool.max-active=8# # Connection pool maximum blocking wait time (using negative values to indicate no limit) Spring.redis.pool.max-wait=-1# # Maximum idle connection in the connection pool Spring.redis.pool.max-idle=8# # The minimum idle connection in the connection pool spring.redis.pool.min-idle=0# # Connection time-out (ms) Spring.redis.timeout=0

2 connecting Redis requires Redisconnection and redisconnectionfactory,

Redisconnection is created by Redisconnectionfactory.

Redisconnection provides lower-level data operations ( byte arrays)

2.1 Creating a Configuration Class Redisconfig

@Configuration  Public class redisconfig {    @Autowired    private  redisconnectionfactory redisconnectionfactory;    } 

3 redistemplate

Redistemplate is the core class of the Redis module and is a rich feature of the high abstraction of redis operations. His focus is on serialization and connection management. He is thread safe and provides the following interface for operation

Hashoperationshyperloglogoperationslistoperationssetoperationsvalueoperationszsetoperations

Spring also provides a stringredistemplate to facilitate the processing of strings. Objects are often encountered in Java, and you can use Jackson for object-string conversions. can provide a tool class.

Jsonutil

 Public classJsonutil {Private StaticObjectmapper Objectmapper =NewObjectmapper ();  Public StaticString convertobj2string (ObjectObject) {String s=NULL; Try{s= Objectmapper.writevalueasstring (Object); } Catch(jsonprocessingexception e) {e.printstacktrace (); }        returns; }     Public Static<T> T Convertstring2obj (String s, class<t>clazz) {T T=NULL; Try{T=Objectmapper.readvalue (S, clazz); } Catch(IOException e) {e.printstacktrace (); }        returnT; }}

At this point, the Redis configuration is as follows

@Configuration  Public class redisconfig {    @Autowired    private  redisconnectionfactory redisconnectionfactory;    @Bean    public redistemplate<string, string> redistemplate () {         New  stringredistemplate (redisconnectionfactory);         return redistemplate;}    }

Description: By default, the Bean name and method name are the same, and you can use the Name property to specify. A bean with ID redistemplate is registered in the IOC container above.

4 using Redistemplate for additional pruning and checking

@Service Public classRedisservice {@AutowiredPrivatestringredistemplate redistemplate; /** * How many seconds a week*/    Private StaticFinalLongWeek_seconds =7* -* -* -; /** * Store key,value in Redis database with default setting expiration of one week * * @param key * @param value*/     Public void Set(String key, Object value) {Redistemplate.opsforvalue ().Set(Key, jsonutil.convertobj2string (value), Week_seconds, timeunit.seconds); }    /** * Store the Key,value in the Redis database, set the expiration unit in seconds * * @param key * @param value * @param expiretime */     Public void Set(String key, Object value,Longexpiretime) {Redistemplate.opsforvalue ().Set(Key, jsonutil.convertobj2string (value), Expiretime, timeunit.seconds); }    /** * determine if key is in the Redis database * * @param key * @return*/     PublicBoolean exists (final String key) {returnRedistemplate.haskey (key); }    /** * Get the object corresponding to key * @param key * @param clazz target Object type * @param <T> * @return*/     Public<T> TGet(String key, class<t>clazz) {String s=Get(key); if(s = =NULL) {            return NULL; }        returnJsonutil.convertstring2obj (S, clazz); }    /** * Get the string corresponding to key * @param key * @return*/     PublicStringGet(String key) {returnRedistemplate.opsforvalue ().Get(key); }    /** * Delete key corresponding to value * @param key*/     Public voidDelete (String key) {redistemplate.delete (key); }}

Spring Boot Integrated Redis

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.