Redis之數據結構HashMap詳細使用

一般大家使用redis的string數據結構比較多,在想使用其他數據結構,但是感覺操作複雜的現象身邊很普遍,
爲簡化大家操作redis hashmap,特整理此文,方便大家使用。

 

一、Redis數據結構 hashmap,RedisDesktopManager使用如下:

 

 

二、  通過代碼 Java端使用

增:

Map<String, UserRankDto> userDtoMap = new HashMap<>();
UserRankDto rankDto = null;
for (Stu stu : stuList) {
	rankDto = new UserRankDto();
	//屬性賦值略

	userDtoMap.put(String.valueOf(stu.getUserid()), rankDto);
}
RedisCacheUtils.setMap(AnalysisConstant.REDIS_PERSONAL_MAP, userDtoMap, AnalysisConstant.REDIS_DATABASE_INDEX);


AnalysisConstant.REDIS_PERSONAL_MAP:自定義業務主key
AnalysisConstant.REDIS_DATABASE_INDEX: redis對應的數據庫(0-15)
/**
     * 將map寫入緩存
     *
     * @param key
     * @param map
     */
    public static <T> void setMap(String key, Map<String, T> map, Integer dbIndex) {
        setMap(key, map, dbIndex, NOT_EXPIRE);
    }

    /**
     * 將map寫入緩存
     *
     * @param key
     * @param map
     * @param expire 失效時間(秒)
     */
    public static <T> void setMap(String key, Map<String, T> map, Integer dbIndex, Integer expire) {
        setDbIndex(dbIndex);

        template.opsForHash().putAll(key, map);
        if (expire != NOT_EXPIRE) {
            expire(key, expire);
        }
    }

刪:

RedisCacheUtils.deleteByPrefix(AnalysisConstant.REDIS_PERSONAL_MAP, AnalysisConstant.REDIS_DATABASE_INDEX);

/**
 * 刪除 指定數據庫 前綴模糊匹配 索引
 *
 * @param dbIndex 數據庫索引 範圍 0-15 默認0
 */
public static void deleteByPrefix(String prefix, Integer dbIndex) {
	setDbIndex(dbIndex);
	Set<String> keys = template.keys(prefix + "*");
	template.delete(keys);
}


/**
 * 刪除 指定數據庫索引
 *
 * @param key     鍵
 * @param dbIndex 數據庫索引 範圍 0-15 默認0
 */
public static void delete(String key, Integer dbIndex) {
	setDbIndex(dbIndex);
	template.delete(key);
}

改:

// 放入hashmap中一個Userid對應值  key - value
// 沒有就新增,如果有對應的key爲 userid,則覆蓋更新
RedisCacheUtils.setMapValue(AnalysisConstant.REDIS_PERSONAL_MAP, String.valueOf(model.getUserid()), userRankDto, AnalysisConstant.REDIS_DATABASE_INDEX);


/**
 * 插入值-對象,指定數據庫索引,指定過期時間
 *
 * @param key      鍵
 * @param mapKey   map鍵
 * @param mapValue map值
 * @param dbIndex  數據庫索引 範圍 0-15 默認0
 */
public static <T> void setMapValue(String key, String mapKey, T mapValue, Integer dbIndex) {
	setMapValue(key, mapKey, mapValue, dbIndex, NOT_EXPIRE);
}

/**
 * 插入值-對象,指定數據庫索引,指定過期時間
 *
 * @param key      鍵
 * @param mapKey   map鍵
 * @param mapValue map值
 * @param dbIndex  數據庫索引 範圍 0-15 默認0
 * @param expire   過期時間 單位:秒
 */
public static <T> void setMapValue(String key, String mapKey, T mapValue, Integer dbIndex, int expire) {
	// 選擇數據庫
	setDbIndex(dbIndex);
	BoundHashOperations<String, String, T> boundHashOperations = template.boundHashOps(key);
	boundHashOperations.put(mapKey, mapValue);
	if (expire != NOT_EXPIRE) {
		expire(key, expire);
	}
}

查:

// 獲取map中的單個特定key的value值
UserRankDto dto = RedisCacheUtils.getMapValue(AnalysisConstant.REDIS_PERSONAL_MAP,String.valueOf(userId),DB_INDEX);

/**
 * 獲取值-字符串,指定數據庫索引,設置過期時間
 *
 * @param key     鍵
 * @param mapKey  map鍵
 * @param dbIndex 數據庫索引 範圍 0-15 默認0
 * @return
 */
public static <T> T getMapValue(String key, String mapKey, Integer dbIndex) {
	// 選擇數據庫
	setDbIndex(dbIndex);
	BoundHashOperations<String, String, T> boundHashOperations = template.boundHashOps(key);
	return boundHashOperations.get(mapKey);
}
// 獲取整個hashmap

/**
 * 獲取map緩存
 *
 * @param key
 * @return
 */
public static <T> Map<String, T> getMap(String key, Integer dbIndex) {
	setDbIndex(dbIndex);
	BoundHashOperations<String, String, T> boundHashOperations = template.boundHashOps(key);
	return boundHashOperations.entries();
}

補充:

/**
     * redis 模板
     */
    private static StringRedisTemplate template = SpringContextHolder.getBean("stringRedisTemplate");

    /**
     * 不設置過期時長
     */
    public final static int NOT_EXPIRE = -1;

redis版本: 

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