哈希hash又稱為散列、雜湊等,是將任意長度的輸入通過散列算法變換為固定長度的輸出,最終輸出也就是哈希值。這種轉換是一種壓縮映射。也就是說,散列值的空間通常要遠小于輸入控件,不同的輸入可能會散列成相同的輸出,所以不可能通過散列值來確定唯一的輸入值。
哈希表hash table是為了將數據映射到數組中某個位置,通過數組下標訪問元素以提高數據的查詢速度,這種查詢的平均期望時間復雜度為O(1)。
Redis hash 是一個string類型的field和value的映射表,hash特別適合用于存儲對象。Redis 中每個 hash 可以存儲 232 - 1 鍵值對(40多億)。
讀取
var?list?=?new?List<InsAuthenticationInfo>();Dictionary<string,?string>?cache;using?(var?redisClient?=?RedisManager.GetReadOnlyClient()){cache?=?redisClient.GetAllEntriesFromHash(CacheKeys.InsAuthenticationMap);}foreach?(var?item?in?cache){if?(string.IsNullOrWhiteSpace(item.Key)?||?item.Value?==?null)continue;try{var?info?=?JsonHelper.GetObject<InsAuthenticationInfo>(item.Value);if?(info?!=?null)list.Add(info);}catch?(Exception?e){Logger.Error(e,?$"解析InsAuthenticationInfo失敗,JSON:{item.Value}");}}if?(ListIsNull(list))return?null;return?list.OrderBy(x?=>?x.No,?noComparer).ToList();
修改
using?(var?redisClient?=?RedisManager.GetClient()){string?infoStr?=?redisClient.GetValueFromHash(CacheKeys.InsAuthenticationMap,?info.No);if?(string.IsNullOrWhiteSpace(infoStr))throw?new?Exception($"身份信息不存在,No:{info.No}");try{var?updateItem?=?JsonHelper.GetObject<InsAuthenticationInfo>(infoStr);updateItem.Mid?=?info.Mid;updateItem.SessionId?=?info.SessionId;updateItem.CanBeUse?=?info.CanBeUse;updateItem.UpdateTime?=?DateTime.Now;redisClient.SetEntryInHash(CacheKeys.InsAuthenticationMap,?info.No,?JsonHelper.GetJson(updateItem));}catch?(Exception?e){Logger.Error(e,?$"解析InsAuthenticationInfo失敗,JSON:{infoStr}");}}