标签:closed system 整合 col opened pac oam serialize ack
<!-- 添加redis支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
1 package com.xsjt.redis; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.data.redis.connection.RedisConnectionFactory; 7 import org.springframework.data.redis.core.HashOperations; 8 import org.springframework.data.redis.core.ListOperations; 9 import org.springframework.data.redis.core.RedisTemplate; 10 import org.springframework.data.redis.core.SetOperations; 11 import org.springframework.data.redis.core.ValueOperations; 12 import org.springframework.data.redis.core.ZSetOperations; 13 import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; 14 import org.springframework.data.redis.serializer.StringRedisSerializer; 15 /** 16 * ClassName:RedisConfig Date: 2017年11月14日 下午3:39:34 17 * 将 五种数据类型 注入到 redis中 18 * @author Joe 19 * @version 20 * @since JDK 1.8 21 * 参考地址:https://www.cnblogs.com/skyessay/p/6485187.html 22 */ 23 @Configuration 24 public class RedisConfig { 25 26 // 注入 RedisConnectionFactory 27 @Autowired 28 private RedisConnectionFactory redisConnectionFactory; 29 30 @Bean 31 public RedisTemplate<String, Object> functionDomainRedisTemplate() { 32 RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); 33 initDomainRedisTemplate(redisTemplate, redisConnectionFactory); 34 return redisTemplate; 35 } 36 37 /** 38 * 设置数据存入 redis 的序列化方式 39 * 40 * @param redisTemplate 41 * @param factory 42 */ 43 private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) { 44 redisTemplate.setKeySerializer(new StringRedisSerializer()); 45 redisTemplate.setHashKeySerializer(new StringRedisSerializer()); 46 redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); 47 redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); 48 redisTemplate.setConnectionFactory(factory); 49 } 50 51 /** 52 * 实例化 HashOperations 对象,可以使用 Hash 类型操作 53 * 54 * @param redisTemplate 55 * @return 56 */ 57 @Bean 58 public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) { 59 return redisTemplate.opsForHash(); 60 } 61 62 /** 63 * 实例化 ValueOperations 对象,可以使用 String 操作 64 * 65 * @param redisTemplate 66 * @return 67 */ 68 @Bean 69 public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) { 70 return redisTemplate.opsForValue(); 71 } 72 73 /** 74 * 实例化 ListOperations 对象,可以使用 List 操作 75 * 76 * @param redisTemplate 77 * @return 78 */ 79 @Bean 80 public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) { 81 return redisTemplate.opsForList(); 82 } 83 84 /** 85 * 实例化 SetOperations 对象,可以使用 Set 操作 86 * 87 * @param redisTemplate 88 * @return 89 */ 90 @Bean 91 public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) { 92 return redisTemplate.opsForSet(); 93 } 94 95 /** 96 * 实例化 ZSetOperations 对象,可以使用 ZSet 操作 97 * 98 * @param redisTemplate 99 * @return 100 */ 101 @Bean 102 public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) { 103 return redisTemplate.opsForZSet(); 104 } 105 }
package com.xsjt.redis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Component; /** * ClassName: StringRedisUtil * String 数据类型 * date: 2017年11月14日 下午8:15:07 * @author Joe * @version * @since JDK 1.8 */ @Component("stringRedis") public class StringRedisUtil { @Autowired private ValueOperations<String, Object> redisTemplate; /** * set:(保存数据). * @author Joe * Date:2017年11月14日下午8:15:01 * * @param key * @param value */ public void set(String key, String value){ redisTemplate.set(key, value); } /** * get:(得到数据). * @author Joe * Date:2017年11月14日下午8:15:38 * * @param key * @return */ public Object get(String key) { return redisTemplate.get(key); } // 可自行扩展其他方法 }
1 package com.xsjt.redis; 2 import java.util.List; 3 import java.util.Set; 4 import java.util.concurrent.TimeUnit; 5 import javax.annotation.Resource; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.data.redis.core.HashOperations; 8 import org.springframework.data.redis.core.RedisTemplate; 9 import org.springframework.stereotype.Component; 10 11 /** 12 * ClassName:HashRedisUtil Date: 2017年11月14日 下午8:17:47 13 * Hash 数据类型 14 * @author Joe 15 * @version 16 * @param <T> 17 * @since JDK 1.8 18 */ 19 @Component("hashRedis") 20 public class HashRedisUtil<T> { 21 22 @Autowired 23 protected RedisTemplate<String, Object> redisTemplate; 24 @Resource 25 protected HashOperations<String, String, Object> hashOperations; 26 27 /** 28 * 添加 29 * 30 * @param key key 31 * @param doamin 对象 32 * @param expire 过期时间(单位:秒),传入 -1 时表示不设置过期时间 33 */ 34 public void put(String key, String hashKey, T doamin, long expire) { 35 hashOperations.put(key, hashKey, doamin); 36 if (expire != -1) { 37 redisTemplate.expire(key, expire, TimeUnit.SECONDS); 38 } 39 } 40 41 /** 42 * 删除 43 * 44 * @param key 传入key的名称 45 */ 46 public void remove(String key, String field) { 47 hashOperations.delete(key, field); 48 } 49 50 /** 51 * 查询 52 * 53 * @param key 查询的key 54 * @return 55 */ 56 public Object get(String key, String field) { 57 return hashOperations.get(key, field); 58 } 59 60 /** 61 * 获取当前redis库下所有对象 62 * 63 * @return 64 */ 65 public List<Object> getAll(String key) { 66 return hashOperations.values(key); 67 } 68 69 /** 70 * 查询查询当前redis库下所有key 71 * 72 * @return 73 */ 74 public Set<String> getKeys(String key) { 75 return hashOperations.keys(key); 76 } 77 78 /** 79 * 判断key是否存在redis中 80 * 81 * @param key 传入key的名称 82 * @return 83 */ 84 public boolean isKeyExists(String key, String field) { 85 return hashOperations.hasKey(key, field); 86 } 87 88 /** 89 * 查询当前key下缓存数量 90 * 91 * @return 92 */ 93 public long count(String key) { 94 return hashOperations.size(key); 95 } 96 }
package com.xsjt.redis; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * ClassName:TestRedis * Date: 2017年11月14日 下午8:09:54 * @author Joe * @version * @since JDK 1.8 */ @RunWith(SpringRunner.class) @SpringBootTest public class TestRedis { /********************************测试String***********************************/ @Autowired private StringRedisUtil stringRedis; @Test public void setString() { stringRedis.set("name", "张三"); } @Test public void getString() { Object value = stringRedis.get("name"); System.out.println("value=" + value); } /**********************************测试Hash************************************/ @Autowired private HashRedisUtil<Object> hashRedisUtil; @Test public void setHash() { hashRedisUtil.put("user", "userName", new Integer(6868), 5); } @Test public void getHash() { Integer a = (Integer) hashRedisUtil.get("user", "userName"); System.out.println("a==" + a); } }
标签:closed system 整合 col opened pac oam serialize ack
原文地址:http://www.cnblogs.com/xbq8080/p/7867939.html