SpringData Redis 常见操作
作者:互联网
String类型:ValueOperations
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext-redis.xml") public class RedisStringTest { @Autowired private RedisTemplate redisTemplate; private ValueOperations<String, String> ops = null; @Before public void init() { redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); ops = redisTemplate.opsForValue(); } @Test public void testSet() { ops.set("name", "zhangsan"); ops.set("sex", "男", 10, TimeUnit.SECONDS); // 有效时间10秒 ops.set("name", "xx", 2); // 替换,索引从0开始 zhxxgsan // 当key不存在的时候,执行保存操作;当key存在的时候,什么都不做 ops.setIfAbsent("name", "lily"); // 批量保存 Map map = new HashMap(); map.put("name2", "fan2"); map.put("name3", "fan3"); map.put("name4", "fan4"); ops.multiSet(map); // 追加 当key存在时,会执行追加操作;当key不存在时,会执行保存操作 ops.append("name5", "fan5"); // 执行两次,结果:fan5fan5 } @Test public void testGet() { // 根据key获取value String value = ops.get("name"); System.out.println(value); // 首先根据key获取value,然后再根据value进行截取,从start位置截取到end位置[包含start和end],索引从0开始 String name = ops.get("name", 2, 5); System.out.println(name); // 批量获取 List<String> keys = new ArrayList<>(); keys.add("name2"); keys.add("name3"); keys.add("name4"); List<String> values = ops.multiGet(keys); for (String s : values) { System.out.println(s); } // 根据key获取value的长度 Long size = ops.size("name"); System.out.println(size); } @Test public void testIncrement() { ops.set("age", "20"); ops.increment("age"); // 自增1 System.out.println(ops.get("age")); // 21 ops.increment("age", 5); // 自增5 System.out.println(ops.get("age"));// 26 ops.decrement("age"); // 自减 System.out.println(ops.get("age")); // 25 } /** * 注意:删除使用的是RedisTemplate(redisTemplate),而不是ValueOperations(ops) */ @Test public void testDelete() { // 单个删除 redisTemplate.delete("name2"); // 批量删除 List<String> keys = new ArrayList<>(); keys.add("name3"); keys.add("name4"); keys.add("name5"); redisTemplate.delete(keys); } }
Hash类型:HashOperations
/** * 注意:实体类必须实现序列化接口 */ public class Article implements Serializable { //作者 private String author; //创建时间 private Date createTime; //标题 private String title; /************* get/set方法 *************/ }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext-redis.xml") public class RedisHashTest { @Autowired private RedisTemplate redisTemplate; private HashOperations<String, String, Article> ops = null; @Before public void init() { redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); ops = redisTemplate.opsForHash(); } /** * 保存 */ @Test public void testPut() { Article article = new Article(); article.setAuthor("猫腻"); article.setTitle("将夜"); article.setCreateTime(new Date()); ops.put("article", "1", article); // ops.putAll(Map); // ops.putIfAbsent(); } /** * 获取 */ @Test public void testGet() { // 判断hashkey是否存在 Boolean flag = ops.hasKey("article", "2"); System.out.println(flag); // 根据key和hashkey获取值 Article article = ops.get("article", "1"); System.out.println(article); // 根据key获取所有的hashkey Set<String> set = ops.keys("article"); for (String s : set) { System.out.println(s); } // 根据key获取所有的值 List<Article> list = ops.values("article"); for (Article a : list) { System.out.println(a); } // 同时获取hashkey和value Map<String, Article> map = ops.entries("article"); Set<Map.Entry<String, Article>> entries = map.entrySet(); for (Map.Entry<String, Article> entry : entries) { System.out.println(entry.getKey() + "---" + entry.getValue()); } } /** * 删除 */ @Test public void testDelete() { // 这里通过删除hashkey,来删除对应的值 // 如果要把整个hash删除,参考String类型的 redisTemplate.delete(key); ops.delete("article", "1"); // ops.delete("article", "1", "2", ...); } }
List类型:ListOperations
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext-redis.xml") public class RedisListTest { @Autowired private RedisTemplate redisTemplate; private ListOperations<String, String> ops = null; @Before public void init() { redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); ops = redisTemplate.opsForList(); } /** * 添加 */ @Test public void testAdd() { // 从左边添加 ops.leftPush("student", "lily"); ops.leftPushAll("student", "小明", "小红", "小芳"); ArrayList<String> list = new ArrayList<>(); list.add("aaa"); list.add("bbb"); ops.leftPushAll("student", list); // 从右边添加 ops.rightPush("student", "ddd"); // ops.rightPushAll(); } /** * 查询 */ @Test public void testGet() { String student = ops.index("student", 1); // 索引从0开始 String student1 = ops.index("student", -1); // -1代表右边开始第一个元素 String student2 = ops.index("student", -2); // -2代表右边开始第二个元素 System.out.println(student); System.out.println(student1); System.out.println(student2); // 范围查询,包括开始索引和结束索引 List<String> list = ops.range("student", 0, 3); System.out.println(list); } /** * 删除 */ @Test public void testRemove() { String student = ops.leftPop("student"); System.out.println(student); String student1 = ops.rightPop("student"); System.out.println(student1); // 弹出指定的元素 // count > 0:删除左边起第几个等于指定值的元素 // count < 0:删除右边起第几个等于指定值的元素 // count = 0:删除所有等于value的元素 ops.remove("student", 2, "小红"); } }
Set类型:SetOperations
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext-redis.xml") public class RedisSetTest { @Autowired private RedisTemplate redisTemplate; private SetOperations<String, String> ops = null; @Before public void init() { redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); ops = redisTemplate.opsForSet(); } @Test public void testAdd() { ops.add("person", "张三", "李四", "王五", "赵六"); ops.add("person", "张三"); } /** * 查询 */ @Test public void testFind() { Set<String> person = ops.members("person"); for (String s : person) { System.out.println(s); } // 随机获取一个 String s = ops.randomMember("person"); System.out.println(s); // 随机获取多个(可能会重复) List<String> list = ops.randomMembers("person", 3); for (String name : list) { System.out.println(name); } } /** * 删除 */ @Test public void testRemove() { // 返回移除成功的个数 Long count = ops.remove("person", "张三", "李四", "赵六"); System.out.println(count); // 随机移除指定个数元素 List<String> person = ops.pop("person", 2); System.out.println(person); String p = ops.pop("person");// 随机移除一个 System.out.println(p); } /** * 多集合操作 */ @Test public void testMoreSet() { ops.add("names1", "zhangsan", "lisi", "wangwu"); ops.add("names2", "zhangsan", "lisi", "zhaoliu"); // 求交集 Set<String> set1 = ops.intersect("names1", "names2"); System.out.println(set1); // 求并集 Set<String> set2 = ops.union("names1", "names2"); System.out.println(set2); // 求差集 Set<String> set3 = ops.difference("names1", "names2"); System.out.println(set3); // wangwu Set<String> set4 = ops.difference("names2", "names1"); System.out.println(set4); // zhaoliu } }
ZSet类型:ZSetOperations
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext-redis.xml") public class RedisZSetTest { @Autowired private RedisTemplate redisTemplate; private ZSetOperations<String, String> ops = null; @Before public void init() { redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); ops = redisTemplate.opsForZSet(); } /** * 添加 */ @Test public void testAdd() { ops.add("student", "aaa", 40); ops.add("student", "bbb", 80); ops.add("student", "ccc", 60); } /** * 分数的增减 */ @Test public void testScore() { ops.incrementScore("student", "aaa", 60); // 增加60分 ops.incrementScore("student", "bbb", -50); // 减少50分 } /** * 查询一个元素 */ @Test public void testFindOne() { // 查询个人分数 Double score = ops.score("student", "aaa"); System.out.println(score); // 查询一个元素在集合中的排名 从0开始 Long rank = ops.rank("student", "bbb"); System.out.println(rank); } /** * 根据一个区间获取一个列表 */ @Test public void testFindList() { // 根据排名区间获取列表 Set<String> set1 = ops.range("student", 1, 3); System.out.println(set1); Set<ZSetOperations.TypedTuple<String>> set2 = ops.rangeWithScores("student", 1, 3); for (ZSetOperations.TypedTuple<String> tuple : set2) { System.out.println(tuple.getValue() + "---" + tuple.getScore()); } // 根据分数区间获取列表 Set<String> set3 = ops.rangeByScore("student", 60, 90); System.out.println(set3); Set<ZSetOperations.TypedTuple<String>> set4 = ops.rangeByScoreWithScores("student", 60, 90); for (ZSetOperations.TypedTuple<String> tuple : set4) { System.out.println(tuple.getValue() + "--" + tuple.getScore()); } } /** * 统计 */ @Test public void testCount() { // 统计一个集合中的元素 Long zCard = ops.zCard("student"); System.out.println(zCard); Long count = ops.count("student", 60, 90); // 根据一个分数区间统计元素数量 System.out.println(count); } /** * 删除 */ @Test public void testRemove() { // 通过key--value删除 value允许传入多个 Long remove = ops.remove("student", "aaa", "bbb"); System.out.println(remove); // 返回成功删除的个数 // 通过排名区间删除 Long count1 = ops.removeRange("student", 1, 2); System.out.println(count1); // 通过分数区间删除 Long count2 = ops.removeRangeByScore("student", 30, 60); System.out.println(count2); } }
标签:SpringData,ops,常见,Redis,System,student,println,public,out 来源: https://www.cnblogs.com/roadlandscape/p/12466250.html