LoginSignup
1
0

More than 5 years have passed since last update.

Redisのリストに登録された10件の値のうち後ろから5件以外削除する

Posted at
redis 127.0.0.1:6379> rpush foo 1
(integer) 1
redis 127.0.0.1:6379> rpush foo 2
(integer) 2
redis 127.0.0.1:6379> rpush foo 3
(integer) 3
redis 127.0.0.1:6379> rpush foo 4
(integer) 4
redis 127.0.0.1:6379> rpush foo 5
(integer) 5
redis 127.0.0.1:6379> rpush foo 6
(integer) 6
redis 127.0.0.1:6379> rpush foo 7
(integer) 7
redis 127.0.0.1:6379> rpush foo 8
(integer) 8
redis 127.0.0.1:6379> rpush foo 9
(integer) 9
redis 127.0.0.1:6379> rpush foo 10
(integer) 10
redis 127.0.0.1:6379> lrange foo 0 -1
 1) "1"
 2) "2"
 3) "3"
 4) "4"
 5) "5"
 6) "6"
 7) "7"
 8) "8"
 9) "9"
10) "10"
redis 127.0.0.1:6379> ltrim foo -5 -1
OK
redis 127.0.0.1:6379> lrange foo 0 -1
1) "6"
2) "7"
3) "8"
4) "9"
5) "10"
redis 127.0.0.1:6379> 

一般化すると ltrim {キー名} -{残したい件数} -1

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0