Redis is a high-performance key-value database, using the C language, Redis full name: Remote Dictionary Server (Remote Data Service), Redis used to store some data require frequent retrieval, saving memory overhead, but also greatly enhance the speed, stored in some hot Redis data, use the time, taken from memory, which greatly improves the speed and saving the overhead of the server directly.
Use redis cache ideas: First, establish a local cache: concurrentHashmap, then build RedisTemplate object data to determine when to take the local cache and redis there is no data.
Then someone asked, so why redis it, with the map as a local cache directly on the line ah. In fact, the local cache only reduces the number and time of the query redis, but when the situation tomcat reboot occurs, the local cache will be cleared, but the cached data is also of redis.
Traditional relational database to meet the ACID, and Redis is a single process, the default 16 database.
The classic noSql non-relational database to meet two of the CAP, can not meet the three kinds
C: strong consistency
A: High Availability
P: partition fault tolerance
Here we look at the configuration of the connection pool Redis
package com. terse. develop. loginmanage. config;
import com. fasterxml. jackson. annotation. JsonAutoDetect;
import com. fasterxml. jackson. annotation. PropertyAccessor;
import com. fasterxml. jackson. databind. ObjectMapper;
import org. slf4j. Logger;
import org. slf4j. LoggerFactory;
import org. springframework. beans. factory. annotation. Qualifier;
import org. springframework. beans. factory. annotation. Value;
import org. springframework. boot. autoconfigure. EnableAutoConfiguration;
import org. springframework. boot. context. properties. ConfigurationProperties;
import org. springframework. context. annotation. Bean;
import org. springframework. context. annotation. Configuration;
import org. springframework. data. redis. connection. RedisConnectionFactory;
import org. springframework. data. redis. connection. jedis. JedisConnectionFactory;
import org. springframework. data. redis. core. RedisTemplate;
import org. springframework. data. redis. serializer. Jackson2JsonRedisSerializer;
import org. springframework. data. redis. serializer. StringRedisSerializer;
import redis. clients. jedis. JedisPoolConfig;
@Configuration
@EnableAutoConfiguration
public class RedisConfig {
private Logger logger = LoggerFactory. getLogger ( this . getClass ( ) ) ;
@Value ( "${spring.redis.hostName}" )
private String host;
@Value ( "${spring.redis.port}" )
private int port;
@Value ( "${spring.redis.password}" )
private String password;
@Value ( "${spring.redis.timeout}" )
private int timeout;
@Value ( "${redis.pool.maxWaitMillis}" )
private long maxWaitMillis;
@Value ( "${redis.pool.maxIdle}" )
private int maxIdle;
@Bean ( "JPC" )
@ConfigurationProperties ( prefix = "redis.pool" )
public JedisPoolConfig getRedisConfig ( ) {
JedisPoolConfig config = new JedisPoolConfig ( ) ;
config. setMaxIdle ( maxIdle) ;
config. setMaxWaitMillis ( maxWaitMillis) ;
return config;
}
@Bean ( "JCF" )
@ConfigurationProperties ( prefix = "spring.redis" )
public JedisConnectionFactory getConnectionFactory ( ) {
JedisConnectionFactory factory = new JedisConnectionFactory ( ) ;
JedisPoolConfig config = getRedisConfig ( ) ;
factory. setPoolConfig ( config) ;
factory. setHostName ( host) ;
factory. setPassword ( password) ;
factory. setTimeout ( timeout) ;
logger. info ( "JedisConnectionFactory bean init success." ) ;
return factory;
}
@Bean ( "RT" )
public RedisTemplate< String, Object> redisTemplate ( @Qualifier ( "JCF" ) RedisConnectionFactory factory) {
RedisTemplate< String, Object> template = new RedisTemplate < > ( ) ;
template. setConnectionFactory ( factory) ;
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer ( Object. class ) ;
ObjectMapper om = new ObjectMapper ( ) ;
om. setVisibility ( PropertyAccessor. ALL, JsonAutoDetect. Visibility. ANY) ;
om. enableDefaultTyping ( ObjectMapper. DefaultTyping. NON_FINAL) ;
jacksonSeial. setObjectMapper ( om) ;
template. setValueSerializer ( jacksonSeial) ;
template. setKeySerializer ( new StringRedisSerializer ( ) ) ;
template. setHashKeySerializer ( new StringRedisSerializer ( ) ) ;
template. setHashValueSerializer ( jacksonSeial) ;
template. afterPropertiesSet ( ) ;
return template;
}
}
After writing configuration class loves to write an interface
import java. util. Map;
public interface RedisHashConsumerManager < T> {
boolean hasConsumer ( String token) ;
String addConsumer ( Map< String, Object> consumer)
void deleteConsumer ( String token) ;
T getConsumer ( String token) ;
void addParameter ( String token, String key, String value) ;
void delParameter ( String token, String key) ;
String getParameter ( String token, String key) ;
}
Then we write an implementation class that implements the above interface, the operation of Redis
import org. springframework. data. redis. core. *;
import javax. annotation. Resource;
import java. util. Map;
public class HashConsumerManageImpl < P, PS> implements RedisHashConsumerManager < Map< String, Object> > {
protected final HashOperations< String, String, Object> redisHas;
protected final RedisTemplate< String, Object> redisTemplate;
protected long activityTime = 7 ;
protected TimeUnit activityTimeUnit = TimeUnit. DAYS;
public HashConsumerManageImpl ( RedisTemplate< String, Object> redisTemplate) {
this . redisHas = redisTemplate. opsForHash ( ) ;
this . redisTemplate = redisTemplate;
}
public long getActivityTime ( ) {
return activityTime;
}
public TimeUnit getActivityTimeUnit ( ) {
return activityTimeUnit;
}
@Override
public boolean hasConsumer ( String token) {
return redisTemplate. hasKey ( token) ;
}
@Override
public String addConsumer ( Map< String, Object> consumer) {
String consumerToken = ( String) consumer. get ( "token" ) ;
redisHas. putAll ( consumerToken, consumer) ;
redisTemplate. expire ( consumerToken, getActivityTime ( ) , getActivityTimeUnit ( ) ) ;
return consumerToken;
}
@Override
public void deleteConsumer ( String token) {
redisTemplate. delete ( token) ;
}
@Override
public Map< String, Object> getConsumer ( String token) {
return redisHas. entries ( token) ;
}
@Override
public void addParameter ( String token, String key, String value) {
if ( hasConsumer ( token) )
redisHas. put ( token, key, value) ;
}
@Override
public void delParameter ( String token, String key) {
if ( hasConsumer ( token) )
redisHas. delete ( token, key) ;
}
@Override
public String getParameter ( String token, String key) {
if ( hasConsumer ( token) )
return ( String) redisHas. get ( token, key) ;
return null;
}
}
After writing these, you can directly use
@RestController
@RequestMapping ( "/consumer/" )
public class TestConsumerApi {
@Autowired
@Lazy
RedisHashConsumerManager redisHashConsumerManager;
@RequestMapping ( "login" )
public void login ( Map< String, Object> consumer) {
redisHashConsumerManager. addConsumer ( consumer) ;
}
}