Redis Support
code block
<!--Https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis-->
< dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-starter-redis</artifactid>
<version>1.0.1.RELEASE</version>
</ Dependency>
More support
Http://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis Redis Configuration
# Redis (redisproperties)
spring.redis.database= # database name
spring.redis.host=localhost # Server Host
spring.redis.password= # server password
spring.redis.port=6379 # connection Port
Spring.redis.pool.max-idle=8 # Pool Settings ...
Spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
Spring.redis.sentinel.master= # Name of Redis server
spring.redis.sentinel.nodes= # comma-separated List of host: Port pairs
Java Code
Package com.pay.channel.business;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.data.redis.connection.RedisConnection;
Import Org.springframework.data.redis.connection.RedisConnectionFactory;
Import org.springframework.stereotype.Component; /** * Redis * com.pay.channel.business.RedisBusiness * * * @email h_anke@163.com * @create 2017-03-06 13:57 @Compo
Nent public class Redisbusiness {private static final Logger Logger = Loggerfactory.getlogger (Redisbusiness.class);
@Autowired private Redisconnectionfactory redisconnectionfactory; public void Setex (string key, string value, int expire) throws exception{redisconnection connection = Redisconne
Ctionfactory.getconnection ();
try {Connection.setex (key.getbytes (), expire, value.getbytes ());
}catch (Exception e) {logger.warn ("Redis save data occurrence, now initiates a repeat connection", e); if (connection!= null) connection.close ();
Connection = Redisconnectionfactory.getconnection ();
Connection.setex (Key.getbytes (), expire, value.getbytes ());
}finally {if (connection!= null) connection.close (); } public void Del (String key) throws exception{redisconnection connection = Redisconnectionfactory.get
Connection ();
try {Connection.del (key.getbytes ());
finally {if (connection!= null) connection.close (); } public String get (string key) throws exception{redisconnection connection = REDISCONNECTIONFACTORY.G
Etconnection ();
byte[] bytes = NULL;
try {bytes = Connection.get (Key.getbytes ());
}finally {if (connection!= null) connection.close (); } if (bytes = = null) {Logger.warn ("Redis cache does not have this key, may be timed outExpired ");
throw new Exception ("Redis failure");
Return to New String (bytes); public void Set (String key,string value) throws exception{redisconnection connection = redisconnectionfact
Ory.getconnection ();
try {connection.set (key.getbytes (), value.getbytes ());
}finally {if (connection!= null) connection.close ();
}
}
}