|
@@ -0,0 +1,125 @@
|
|
|
+package com.jpsoft.smart.config;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+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.JdkSerializationRedisSerializer;
|
|
|
+import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
+
|
|
|
+import java.lang.reflect.Method;
|
|
|
+
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@EnableCaching
|
|
|
+public class RedisConfig extends CachingConfigurerSupport {
|
|
|
+ public final static String WX_COMMON_ACCESS_TOKEN = "wx_common_accessToken";
|
|
|
+ public static final String JS_API_TICKET = "js_api_ticket";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 注入 RedisConnectionFactory,注意maven中要有redis.clients.jedis
|
|
|
+ */
|
|
|
+ @Autowired
|
|
|
+ RedisConnectionFactory redisConnectionFactory;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 实例化 RedisTemplate 对象
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public RedisTemplate<String, Object> getRedisTemplate() {
|
|
|
+ RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
|
|
+ initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
|
|
|
+ return redisTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置数据存入 redis 的序列化方式
|
|
|
+ *
|
|
|
+ * @param redisTemplate
|
|
|
+ * @param factory
|
|
|
+ */
|
|
|
+ private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
|
|
|
+ redisTemplate.setKeySerializer(new StringRedisSerializer());
|
|
|
+ redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
|
|
|
+ redisTemplate.setHashKeySerializer(new StringRedisSerializer());
|
|
|
+ redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
|
|
|
+ redisTemplate.setConnectionFactory(factory);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public KeyGenerator keyGenerator() {
|
|
|
+ return new KeyGenerator() {
|
|
|
+ @Override
|
|
|
+ public Object generate(Object target, Method method, Object... params) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append(target.getClass().getName());
|
|
|
+ sb.append(method.getName());
|
|
|
+ for (Object obj : params) {
|
|
|
+ sb.append(obj.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 实例化 HashOperations 对象,可以使用 Hash 类型操作
|
|
|
+ *
|
|
|
+ * @param redisTemplate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
|
|
|
+ return redisTemplate.opsForHash();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 实例化 ValueOperations 对象,可以使用 String 操作
|
|
|
+ *
|
|
|
+ * @param redisTemplate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
|
|
|
+ return redisTemplate.opsForValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 实例化 ListOperations 对象,可以使用 List 操作
|
|
|
+ *
|
|
|
+ * @param redisTemplate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
|
|
|
+ return redisTemplate.opsForList();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 实例化 SetOperations 对象,可以使用 Set 操作
|
|
|
+ *
|
|
|
+ * @param redisTemplate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
|
|
|
+ return redisTemplate.opsForSet();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 实例化 ZSetOperations 对象,可以使用 ZSet 操作
|
|
|
+ *
|
|
|
+ * @param redisTemplate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
|
|
|
+ return redisTemplate.opsForZSet();
|
|
|
+ }
|
|
|
+}
|