List操作
redis中的列表:
- 一個列表最多可以存儲2^32 -1個元素
- 可以對列表兩端插入(push)和彈出(pop)
- 元素有序且可重復
命令 | 操作 | 返回值 |
---|---|---|
range(K key, long start, long end) | 獲取元素【lrange】 | List<V> |
trim(K key, long start, long end) | 截取列表的內容,從start到end中間的留下,兩端的刪除【ltrim】 | void |
size(K key) | 獲取列表長度【llen】 | Long |
leftPush(K key, V value) | 從列表左側插入元素【lpush】 | Long |
leftPushAll(K key, V... values) | ~ | Long |
leftPushAll(K key, Collection<V> values) | ~ | Long |
leftPushIfPresent(K key, V value) | 左側添加元素(如果存在的話)【lpush】 | Long |
leftPush(K key, V pivot, V value) | 在pivot(匹配到的第一個)之前(左側)添加value【linsert】 | Long |
rightPush(K key, V value) | 從列表右側插入元素【rpush】 | Long |
rightPushAll(K key, V... values) | 【rpush】 | Long |
rightPushAll(K key, Collection<V> values) | 【rpush】 | Long |
rightPushIfPresent(K key, V value) | 右側添加元素(如果存在的話)【rpush】 | Long |
rightPush(K key, V pivot, V value) | 在pivot(匹配到的第一個)之后(右側)添加value【linsert】 | Long |
set(K key, long index, V value) | 設置值,有則覆蓋,沒有則新增【lset】 | void |
remove(K key, long count, Object value) | 刪除元素,見下方說明【lrem】 | Long |
index(K key, long index) | 查找元素【lindex】 | V |
leftPop(K key) | 從列表左側彈出元素【lpop】 | V |
leftPop(K key, long timeout, TimeUnit unit) | 彈出列表左側元素,timeout為超時時間,TimeUnit時間單位【blpop】 | V |
rightPop(K key) | 從列表右側彈出元素【rpop】 | V |
rightPop(K key, long timeout, TimeUnit unit) | 彈出列表右側元素,timeout為超時時間,TimeUnit時間單位【brpop】 | V |
rightPopAndLeftPush(K sourceKey, K destinationKey) | 彈出右側元素並向左側插入元素 | V |
rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit) | 彈出右側元素並向左側插入元素。timeout 超時時間 | V |
getOperations() | RedisOperations<K, V> |
- remove(K key, long count, Object value) :
conut = 0,刪除所有匹配的元素
count > 0 刪除匹配元素開始,從左到右最多count個元素
count < 0 刪除匹配元素開始,從右到左最多count個元素
RedisTemplate常用集合使用說明-opsForList(三)
列表類型的內部編碼有兩種。
- ziplist(壓縮列表):當列表的元素個數小於list-max-ziplist-entries配置 (默認512個),同時列表中每個元素的值都小於list-max-ziplist-value配置時 (默認64字節),Redis會選用ziplist來作為列表的內部實現來減少內存的使 用。
- linkedlist(鏈表):當列表類型無法滿足ziplist的條件時,Redis會使用 linkedlist作為列表的內部實現。