| package com.mzl.flower.config; | 
|   | 
| import org.springframework.cache.annotation.CachingConfigurerSupport; | 
| import org.springframework.cache.annotation.EnableCaching; | 
| import org.springframework.cache.interceptor.KeyGenerator; | 
| 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.*; | 
| import org.springframework.data.redis.serializer.StringRedisSerializer; | 
| import org.springframework.util.StringUtils; | 
|   | 
| import java.lang.reflect.Method; | 
|   | 
| @Configuration | 
| @EnableCaching | 
| public class RedisConfig extends CachingConfigurerSupport { | 
|   | 
|     /** | 
|      * redisTemplate 序列化使用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类,方便调试redis | 
|      * | 
|      * @param redisConnectionFactory | 
|      * @return | 
|      */ | 
|     @Bean | 
|     public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { | 
|   | 
|         RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); | 
|   | 
|         //使用StringRedisSerializer来序列化和反序列化redis的key | 
|         redisTemplate.setValueSerializer(new StringRedisSerializer()); | 
|         redisTemplate.setHashValueSerializer(new StringRedisSerializer()); | 
|         //使用StringRedisSerializer来序列化和反序列化redis的key | 
|         redisTemplate.setKeySerializer(new StringRedisSerializer()); | 
|         redisTemplate.setHashKeySerializer(new StringRedisSerializer()); | 
|   | 
|         //开启事务 | 
|         redisTemplate.setEnableTransactionSupport(false); | 
|   | 
|         redisTemplate.setConnectionFactory(redisConnectionFactory); | 
|   | 
|         return redisTemplate; | 
|     } | 
|   | 
|     /** | 
|      * 自定义生成key的策略 | 
|      * | 
|      * @return | 
|      */ | 
|     @Bean | 
|     @Override | 
|     public KeyGenerator keyGenerator() { | 
|         return new KeyGenerator() { | 
|             @Override | 
|             public Object generate(Object target, Method method, Object... params) { | 
|                 return target.getClass().getSimpleName() + "_" | 
|                         + method.getName() + "_" | 
|                         + StringUtils.arrayToDelimitedString(params, "_"); | 
|             } | 
|         }; | 
|     } | 
|   | 
|     /** | 
|      * 实例化 HashOperations 对象,可以使用 Hash 类型操作 | 
|      * | 
|      * @param redisTemplate | 
|      * @return | 
|      */ | 
|     @Bean | 
|     public HashOperations<String, String, String> hashOperations(StringRedisTemplate redisTemplate) { | 
|         return redisTemplate.opsForHash(); | 
|     } | 
|   | 
|     /** | 
|      * 实例化 ValueOperations 对象,可以使用 String 操作 | 
|      * | 
|      * @param redisTemplate | 
|      * @return | 
|      */ | 
|     @Bean | 
|     public ValueOperations<String, String> valueOperations(StringRedisTemplate redisTemplate) { | 
|         return redisTemplate.opsForValue(); | 
|     } | 
|   | 
|     /** | 
|      * 实例化 ListOperations 对象,可以使用 List 操作 | 
|      * | 
|      * @param redisTemplate | 
|      * @return | 
|      */ | 
|     @Bean | 
|     public ListOperations<String, String> listOperations(StringRedisTemplate redisTemplate) { | 
|         return redisTemplate.opsForList(); | 
|     } | 
|   | 
|     /** | 
|      * 实例化 SetOperations 对象,可以使用 Set 操作 | 
|      * | 
|      * @param redisTemplate | 
|      * @return | 
|      */ | 
|     @Bean | 
|     public SetOperations<String, String> setOperations(StringRedisTemplate redisTemplate) { | 
|         return redisTemplate.opsForSet(); | 
|     } | 
| } |