Redis数据结构及Spring-data-redis API使用

一、redis数据类型

Redis目前可以存储键与5种不同数据结构类型之间的映射,分别是:

  • String(字符串)
  • List(列表)
  • Hash(散列)
  • Set(集合)
  • Zset(有序集合)
结构类型 结构存储的值 结构的读写能力
String 可以是字符串、整数或者浮点数 对整个字符串或者字符串的其中一部分执行操作;对象和浮点数执行自增(increment)或者自减(decrement)
List 一个链表,链表上的每个节点都包含了一个字符串 从链表的两端推入或者弹出元素;根据偏移量对链表进行修剪(trim);读取单个或者多个元素;根据值来查找或者移除元素
Set 包含字符串的无序收集器(unorderedcollection),并且被包含的每个字符串都是独一无二的、各不相同 添加、获取、移除单个元素;检查一个元素是否存在于某个集合中;计算交集、并集、差集;从集合里卖弄随机获取元素
Hash 包含键值对的无序散列表 添加、获取、移除单个键值对;获取所有键值对
Zset 字符串成员(member)与浮点数分值(score)之间的有序映射,元素的排列顺序由分值的大小决定 添加、获取、删除单个元素;根据分值范围(range)或者成员来获取元素

二、Spring-data-redis介绍

在Java中通过Jedis来操作Redis,由于Jedis的操作过于底层,或者说封装得不够彻底,当我们要存储一个对象的时候,其实是比较麻烦的,所以使用spring-data-redis越来越流行。Spring-data-redis是Spring-Data项目的一个子项目,就是Spring在Jedis的基础上封装的工具,比Jedis更加易用,而且使得操作Redis以更加面向对象的方式。

RedisTemplate中定义了对5种数据结构操作:

  • redisTemplate.opsForValue(),操作string;
  • redisTemplate.opsForHash(),操作hash;
  • redisTemplate.opsForList(),操作list;
  • redisTemplate.opsForSet(),操作set;
  • redisTemplate.opsForZSet(),操作有序set。

三、RedisTemplate通用方法

/**
 * 判断缓存是否存在
 *
 * @param key
 */
public static boolean exists(String key) {
    return jedisUtil.redisTemplate.hasKey(key);
}

/**
 * 删除缓存
 *
 * @param key
 * @return if exists 1 else 0
 */
public static long delete(String key) {
    if (exists(key)) {
        jedisUtil.redisTemplate.delete(key);
        return 1;
    }
    return 0;
}

/**
 * 设置Key过期时间
 *
 * @param key
 * @param cacheSeconds
 */
public void setExpireTime(String key, Long cacheSeconds) {
    jedisUtil.redisTemplate.expire(key, cacheSeconds, TimeUnit.SECONDS);
}

/**
 * 获取Key过期时间
 *
 * @param key
 * @return
 */
public static Long getExpireTime(String key) {
    return jedisUtil.redisTemplate.getExpire(key, TimeUnit.SECONDS);
}

四、RedisTemplate操作String类型

public interface ValueOperations<K,V>

/**
 * 设置缓存
 *
 * @param key          键
 * @param value        值
 * @param cacheSeconds 超时时间,0为不超时
 * @return
 */
public static void setStr(String key, String value, Long cacheSeconds) {
    if (cacheSeconds != 0) {
        jedisUtil.redisTemplate.opsForValue().set(key, value, cacheSeconds, TimeUnit.SECONDS);
    } else {
        jedisUtil.redisTemplate.opsForValue().set(key, value);
    }
}

/**
 * 获取缓存(String)
 *
 * @param key
 * @return
 */
public static String getStr(String key) {
    if (exists(key)) {
        String value = jedisUtil.redisTemplate.opsForValue().get(key).toString();
        return StringUtil.notEmpty(value) && !"nil".equalsIgnoreCase(value) ? value : null;
    }
    return null;
}

/**
 * 设置缓存(T)
 *
 * @param key          键
 * @param value        值
 * @param cacheSeconds 超时时间,0为不超时
 * @return
 */
public static <T> void setValue(String key, T value, Long cacheSeconds) {
    if (cacheSeconds != 0) {
        jedisUtil.redisTemplate.opsForValue().set(key, value, cacheSeconds, TimeUnit.SECONDS);
    } else {
        jedisUtil.redisTemplate.opsForValue().set(key, value);
    }
}

/**
 * 获取缓存(Object)
 *
 * @param key 键
 * @return
 */
public static Object getObject(String key) {
    if (exists(key)) {
        return jedisUtil.redisTemplate.opsForValue().get(key);
    }
    return null;
}

/**
 * 如果键不存在则新增
 * 存在则不作修改
 *
 * @param key
 * @param value
 * @param cacheSeconds
 * @return
 */
public static boolean setIfAbsent(String key, String value, Long cacheSeconds) {
    boolean result = false;
    result = jedisUtil.redisTemplate.opsForValue().setIfAbsent(key, value);
    if (cacheSeconds != 0) {
        // 设置key失效时间
        jedisUtil.redisTemplate.expire(key, cacheSeconds, TimeUnit.SECONDS);
    }
    return result;
}

/**
 * 获取原来Key键对应的value并重新赋新值
 * 若原来Key键不存在则返回null,并且要新建Key再赋值
 *
 * @param key
 * @param value
 * @return
 */
public static String getAndSet(String key, String value, Long cacheSeconds) {
    String res = (String) jedisUtil.redisTemplate.opsForValue().getAndSet(key, value);
    if (cacheSeconds != 0) {
        jedisUtil.redisTemplate.expire(key, cacheSeconds, TimeUnit.SECONDS);
    }
    return res;
}

/**
 * 为多个key分别设置value
 * 已存在的key则覆盖value
 *
 * @param map
 */
public static void multiSet(Map<String, String> map) {
    jedisUtil.redisTemplate.opsForValue().multiSet(map);
}

/**
 * 以自增形式存储long类型value
 * 相同的key value值累加
 *
 * @param
 */
public static Long incrLong(String key, Long delta) {
    return jedisUtil.redisTemplate.opsForValue().increment(key, delta);
}

/**
 * 以自增形式存储doble类型value
 * 相同的key value值累加
 *
 * @param key
 */
public static Double incrDouble(String key, double delta) {
    return jedisUtil.redisTemplate.opsForValue().increment(key, delta);
}

五、RedisTemplate操作List列表

List是简单的字符串<String>列表,按照插入顺序排序,可以添加一个元素到列表的头部(左边)或者尾部(右边)
public interface ListOperations<K,V>,ListOperations专门操作list列表:

/**
 * 存储整个List<T>
 * 若key相同,则后list加入到前list
 * right批量添加在右边
 *
 * @param key
 * @param value
 * @param cacheSeconds
 */
public static <T> void setList(String key, List<T> value, Long cacheSeconds) {
    jedisUtil.redisTemplate.opsForList().rightPushAll(key, value);
    if (cacheSeconds != 0) {
        jedisUtil.redisTemplate.expire(key, cacheSeconds, TimeUnit.SECONDS);
    }
}

/**
 * 添加单个value到list
 *
 * @param key
 * @param value
 * @param cacheSeconds
 */
public static <T> void setListV(String key, T value, Long cacheSeconds) {
    jedisUtil.redisTemplate.opsForList().rightPush(key, value);
    if (cacheSeconds != 0) {
        jedisUtil.redisTemplate.expire(key, cacheSeconds, TimeUnit.SECONDS);
    }
}

/**
 * 返回Key对应的List长度
 * 如果Key不存在,则解释为空表,并返回0
 *
 * @param key
 * @return
 */
public static Long getSize(String key) {
    return jedisUtil.redisTemplate.opsForList().size(key);
}

/**
 * 返回指定范围内的元素
 *
 * @param key
 * @param start 开始 0
 * @param end   结束
 *              0 到 -1 代表全部长度
 */
public static <T> List<T> getList(String key, long start, long end) {
    if (exists(key)) {
        return jedisUtil.redisTemplate.opsForList().range(key, start, end);
    }
    return null;
}

/**
 * 获取list头部的第一条value
 *
 * @param key
 */
public static Object leftPopList(String key) {
    if (exists(key)) {
        return jedisUtil.redisTemplate.opsForList().leftPop(key);
    }
    return null;
}

六、RedisTemplate操作Hash散列

Redis的散列可以让用户将多个键值对存储到一个Redis键里面
public interface HashOperations<H,HK,HV>,HashOperations提供一系列方法操作hash:

/**
 * 获取Key键对应的哈希HashKey
 *
 * @param key
 * @param hashKey
 * @return
 */
public static Object getHashK(String key, String hashKey) {
    return jedisUtil.redisTemplate.opsForHash().get(key, hashKey);
}

/**
 * 删除Key键对应的哈希HashKey
 *
 * @param key
 * @param hashKey
 * @return
 */
public static Long delHashK(String key, String hashKey) {
    return jedisUtil.redisTemplate.opsForHash().delete(key, hashKey);
}

/**
 * 判断Key键对应的哈希HashKey是否存在
 *
 * @param key
 * @param hashKey
 * @return
 */
public static boolean hashKey(String key, String hashKey) {
    return jedisUtil.redisTemplate.opsForHash().hasKey(key, hashKey);
}

/**
 * 通过给定的delta增加散列HashKey的值(Long)
 *
 * @param key
 * @param hashKey
 * @param delta
 */
public static Long incrHashLong(String key, String hashKey, long delta) {
    return jedisUtil.redisTemplate.opsForHash().increment(key, hashKey, delta);
}

/**
 * 通过给定的delta增加散列HashKey的值(Double)
 *
 * @param key
 * @param hashKey
 * @param delta
 * @return
 */
public static Double incrHashDouble(String key, String hashKey, Double delta) {
    return jedisUtil.redisTemplate.opsForHash().increment(key, hashKey, delta);
}


/**
 * 存储Key键 - HashKey - Map<String, T>
 *
 * @param key
 * @param hashKey
 * @param value
 * @param <T>
 */
public static <T> void setHKMap(String key, String hashKey, Map<String, T> value) {
    jedisUtil.redisTemplate.opsForHash().put(key, hashKey, value);
}


/**
 * 获取HashKey - Map<String, Object>
 *
 * @param key
 * @return
 */
public static Map<String, Map<String, Object>> getHKMap(String key) {
    Map<String, Map<String, Object>> value = null;
    if (exists(key)) {
        jedisUtil.redisTemplate.opsForHash().entries(key);
    }
    return value;
}

/**
 * 存储Key键 - HashKey - List<T>
 *
 * @param key
 * @param hashKey
 * @param value
 * @param <T>
 */
public static <T> void setHKList(String key, String hashKey, List<T> value) {
    jedisUtil.redisTemplate.opsForHash().put(key, hashKey, value);
}

/**
 * 存储Key键 - 所有(HashKey - List<T>)Map集合
 *
 * @param key
 * @param value
 * @param cacheSeconds
 * @param <T>
 */
public static <T> void setMapListAll(String key, Map<String, List<T>> value, Long cacheSeconds) {
    jedisUtil.redisTemplate.opsForHash().putAll(key, value);
    if (cacheSeconds != 0) {
        jedisUtil.redisTemplate.expire(key, cacheSeconds, TimeUnit.SECONDS);
    }
}

/**
 * 获取HashKey - List<T>
 *
 * @param key
 * @return
 */
public static <T> Map<String, List<T>> getHKList(String key) {
    Map<String, List<T>> value = null;
    if (exists(key)) {
        value = jedisUtil.redisTemplate.opsForHash().entries(key);
    }
    return value;
}

/**
 * 存储Key键 - HashKey - T value
 *
 * @param key
 * @param hashKey
 * @param value
 * @param <T>
 */
public static <T> void setHKObject(String key, String hashKey, T value) {
    jedisUtil.redisTemplate.opsForHash().put(key, hashKey, value);
}

/**
 * 存储Key键 - 所以 (HashKey - T value)Map集合
 *
 * @param key
 * @param value
 * @param cacheSeconds
 * @param <T>
 */
public static <T> void setHKObjectAll(String key, Map<String, T> value, Long cacheSeconds) {
    jedisUtil.redisTemplate.opsForHash().putAll(key, value);
    if (cacheSeconds != 0) {
        jedisUtil.redisTemplate.expire(key, cacheSeconds, TimeUnit.SECONDS);
    }
}

/**
 * 获取HashKey - T
 *
 * @param key
 * @param <T>
 * @return
 */
public static <T> Map<String, T> getHKObject(String key) {
    Map<String, T> value = null;
    if (exists(key)) {
        value = jedisUtil.redisTemplate.opsForHash().entries(key);
    }
    return value;
}

七、RedisTemplate操作Set集合

Redis的Set是String类型的无序集合,集合成员是唯一的,这就意味着集合中不能出现重复的数据。Set是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。
public interface SetOperations<K,V>,SetOperations提供了对无序集合的一系列操作:

/**
 * 无序集合中添加元素,返回添加的个数
 *
 * @param strings
 * @return
 */
public static Long addSet(String key, String[] strings) {
    return jedisUtil.redisTemplate.opsForSet().add(key, strings);
}

/**
 * 返回无序集合的大小长度
 *
 * @param key
 * @return
 */
public static Long getSetSize(String key) {
    return jedisUtil.redisTemplate.opsForSet().size(key);
}

/**
 * 返回集合中的所有成员
 *
 * @param key
 * @param <T>
 * @return
 */
public static <T> Set<T> getMembers(String key) {
    return jedisUtil.redisTemplate.opsForSet().members(key);
}

/**
 * 随机获取Key对应无序集合中的一个元素
 *
 * @param key
 * @return
 */
public static Object getRanMembers(String key) {
    return jedisUtil.redisTemplate.opsForSet().randomMember(key);
}

/**
 * 判断value是否是对应集合的成员
 *
 * @param key
 * @param value
 * @return
 */
public static Boolean isMembers(String key, Object value) {
    return jedisUtil.redisTemplate.opsForSet().isMember(key, value);
}

/**
 * 移除集合中多个成员,并返回删除个数
 *
 * @param key
 * @param strings
 * @return
 */
public static Long removeMembers(String key, String[] strings) {
    return jedisUtil.redisTemplate.opsForSet().remove(key, strings);
}

/**
 * 将元素value从一个集合移动到另一个集合
 *
 * @param startKey
 * @param endKey
 * @param value
 * @return
 */
public static Boolean moveSet(String startKey, String endKey, Object value) {
    return jedisUtil.redisTemplate.opsForSet().move(startKey, value, endKey);
}

八、RedisTemplate操作Zset集合

Redis 有序集合Zset和无序集合Set一样也是string类型元素的集合,且不允许重复的成员。不同的是每个元素都会关联一个double类型的分数,Redis正是通过分数来为集合中的成员进行从小到大的排序。有序集合的成员是唯一的,但分数(score)却可以重复。
public interface ZSetOperations<K,V>

参考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章