Introduction to redis
Redis is the most widely used memory data storage in the industry. Compared with memcached, Redis supports richer data structures, such as hashes, lists, sets, etc., and also supports data persistence. In addition, Redis also provides some database-like features, such as transactions, HA, and master-slave libraries. It can be said that Redis has some characteristics of a caching system and a database, so it has rich application scenarios.
introduction
For unit testing, we should make it try to maintain a single environment and not communicate with network resources, so as to ensure the stability and objectivity of the test. For springboot framework, it integrates unit testing JUNIT. At the same time, when designing projects, you can use a variety of embedded storage tools, such as mongodb, redis, mysql, etc. Today, I will mainly talk about the use of embedded-redis.
How to use it is as follows:
Add package reference build.gradle
testCompile( 'com.github.kstyrc:embedded-redis:0.6' )
Add configuration injection
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.HashOperations;import org.springframework.data.redis.core.ListOperations;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.SetOperations;import org.springframework.data.redis.core.ValueOperations;import org.springframework.data.redis.core.ZSetOperations;import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;@Configurationpublic class RedisConfig { /** * Inject RedisConnectionFactory */ @Autowired RedisConnectionFactory redisConnectionFactory; /** * Instantiate the RedisTemplate object* * @return */ @Bean public RedisTemplate<String, Object> functionDomainRedisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); initDomainRedisTemplate(redisTemplate, redisConnectionFactory); return redisTemplate; } /** * Set the serialization method of data storage into redis* * @param redisTemplate * @param factory */ private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) { redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setConnectionFactory(factory); } /** * Instanced HashOperations object, you can use the Hash type operation* * @param redisTemplate * @return */ @Bean public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForHash(); } /** * Instanced ValueOperations object, you can use String Operation * * @param redisTemplate * @return */ @Bean public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForValue(); } /** * Instantiate the ListOperations object, you can use List operations* * @param redisTemplate * @return */ @Bean public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForList(); } /** * Instantiating the SetOperations object, you can use the Set operation* * @param redisTemplate * @return */ @Bean public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForSet(); } /** * Instantiating the ZSetOperations object, you can use the ZSet operation* * @param redisTemplate * @return */ @Bean public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForZSet(); }}Using redis in the business layer
@Autowired RedisTemplate<String, Object> redisCacheTemplate;
During use, our RedisTemplate object has been injected by Autowired.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.