微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java实现RedisUtils操作五大集合(增删改查)

本文主要介绍了Java实现RedisUtils操作五大集合,文中通过示例代码介绍的非常详细,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前排提示,我在这个工具类加了@Component注解,如果在springboot的项目使用,记得通过@Autowired注入使用。

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.*; import org.springframework.stereotype.Component; import java.io.Serializable; import java.util.List; import java.util.Set; @Component public class RedisUtils { @Autowired private Redistemplate redistemplate; /** * 写入String型 [ 键,值] * * @param key * @param value * @return */ public boolean set(final String key, Object value) { boolean result = false; try { ValueOperations operations = redistemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printstacktrace(); } return result; } /** * 写入String型,顺便带有过期时间 [ 键,值] * * @param key * @param value * @return */ public boolean setWithTime(final String key, Object value,int seconds) { boolean result = false; try { ValueOperations operations = redistemplate.opsForValue(); operations.set(key, value,seconds, TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printstacktrace(); } return result; } /** * 批量删除对应的value * * @param keys */ public void remove(final String... keys) { for (String key : keys) { remove(key); } } /** * 批量删除key * * @param pattern */ public void removePattern(final String pattern) { Set keys = redistemplate.keys(pattern); if (keys.size() > 0) redistemplate.delete(keys); } /** * 删除对应的value * * @param key */ public void remove(final String key) { if (exists(key)) { redistemplate.delete(key); } } /** * 判断缓存中是否有对应的value * * @param key * @return */ public boolean exists(final String key) { return redistemplate.hasKey(key); } /** * 读取缓存 * * @param key * @return */ public Object get(final String key) { Object result = null; ValueOperations operations = redistemplate.opsForValue(); result = operations.get(key); return result; } /** * 哈希 添加 * hash 一个键值(key->value)对集合 * * @param key * @param hashKey * @param value */ public void hmSet(String key, Object hashKey, Object value) { HashOperations hash = redistemplate.opsForHash(); hash.put(key, hashKey, value); } /** * Hash获取数据 * * @param key * @param hashKey * @return */ public Object hmGet(String key, Object hashKey) { HashOperations hash = redistemplate.opsForHash(); return hash.get(key, hashKey); } /** * 列表添加 * list:lpush key value1 * * @param k * @param v */ public void lPush(String k, Object v) { ListOperations list = redistemplate.opsForList(); list.rightPush(k, v); } /** * 列表List获取 * lrange: key 0 10 (读取的个数 从0开始 读取到下标为10 的数据) * * @param k * @param l * @param l1 * @return */ public List lRange(String k, long l, long l1) { ListOperations list = redistemplate.opsForList(); return list.range(k, l, l1); } /** * Set集合添加 * * @param key * @param value */ public void add(String key, Object value) { Setoperations set = redistemplate.opsForSet(); set.add(key, value); } /** * Set 集合获取 * * @param key * @return */ public Set setMembers(String key) { Setoperations set = redistemplate.opsForSet(); return set.members(key); } /** * Sorted set :有序集合添加 * * @param key * @param value * @param scoure */ public void zAdd(String key, Object value, double scoure) { ZSetoperations zset = redistemplate.opsForZSet(); zset.add(key, value, scoure); } /** * Sorted set:有序集合获取 * * @param key * @param scoure * @param scoure1 * @return */ public Set rangeByscore(String key, double scoure, double scoure1) { ZSetoperations zset = redistemplate.opsForZSet(); return zset.rangeByscore(key, scoure, scoure1); } /** * 根据key获取Set中的所有值 * * @param key 键 * @return */ public Set sGet(String key) { try { return redistemplate.opsForSet().members(key); } catch (Exception e) { e.printstacktrace(); return null; } } /** * 根据value从一个set中查询,是否存在 * * @param key 键 * @param value 值 * @return true 存在 false不存在 */ public boolean sHasKey(String key, Object value) { try { return redistemplate.opsForSet().isMember(key, value); } catch (Exception e) { e.printstacktrace(); return false; } } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐