* Write configuration file , Set storage data format and serialization , Injection connection factory
* Direct use in use @AutoWired Injection class
* Deposit for use set() method , Take out and use get() method
configuration file
@Configuration public class RedisConfig { @Bean public RedisTemplate<String,
Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); // Set serialization
redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.
setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.
setHashKeySerializer(new StringRedisSerializer()); redisTemplate.
setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // Injection connection factory
redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate
; } }
Basic use
@Autowired RedisTemplate redisTemplate; public void test(){ // Get operation class
ValueOperations valueOperations = redisTemplate.opsForValue(); // Deposit
valueOperations.set("asdf", "asdf"); // take out , Default is Object, Strong rotation can be carried out Object asdf =
valueOperations.get("asdf"); // judge redis Exists in key Boolean hasKey = redisTemplate.
hasKey("key"); }
Technology