Redis相關操作
String資料型別
命令列
- 建立獲取增減
192.168.1.103:6379> set online-users 50
OK
192.168.1.103:6379> set online-users 100
OK192.168.1.103:6379> keys *
- "online-users"
192.168.1.103:6379> get online-users
"100"192.168.1.103:6379> incr online-users
(integer) 101
192.168.1.103:6379> decr online-users
(integer) 100192.168.1.103:6379> incrby online-users 50
(integer) 150
192.168.1.103:6379> incrby online-users -30
(integer) 120
192.168.1.103:6379> decrby online-users -30
(integer) 150
- 獲取value長度
192.168.1.103:6379> strlen online-users
(integer) 3
- 設定超時時間
192.168.1.103:6379> set 18301581288 3545 ex 300
OK
- 建立獲取刪除多個kv
192.168.1.103:6379> mset k1 v1 k2 v2
OK192.168.1.103:6379> mget k1 k2
- "v1"
- "v2"
192.168.1.103:6379> del k1 k2
(integer) 2
API
import redis.clients.jedis.Jedis object StringValue { def main(args: Array[String]): Unit = { val jedis: Jedis = new Jedis("192.168.1.103", 6379) jedis.auth("root") // 選擇使用的database, 公有16個, 預設使用0號database // jedis.select(0) jedis.set("online-users", "100") val r1 = jedis.get("online-users") println(r1) jedis.incrBy("online-users", 50) val r2 = jedis.get("online-users") println(r2) jedis.del("online-users") } }
Hash資料型別
命令列
- 建立獲取
192.168.1.103:6379> hset Aang age 12
(integer) 1
192.168.1.103:6379> hget Aang age
"12"
- 增減
192.168.1.103:6379> hincrby Aang age 3
(integer) 15
- 刪除value中的key
192.168.1.103:6379> hdel Aang age
(integer) 1
- 刪除key
192.168.1.103:6379> del Aang
(integer) 0
- 建立獲取多個
192.168.1.103:6379> hmset Aang name Aang age 12 gender male
OK
192.168.1.103:6379> hmget Aang name age gender
- "Aang"
- "12"
- "male"
192.168.1.103:6379> hgetall Aang- "name"
- "Aang"
- "age"
- "12"
- "gender"
- "male"
- 判斷欄位是否存在
192.168.1.103:6379> hexists Aang name
(integer) 1
192.168.1.103:6379> hexists Aang grade
(integer) 0
- 只獲取keys或values
192.168.1.103:6379> hkeys Aang
- "name"
- "age"
- "gender"
192.168.1.103:6379> hvals Aang- "Aang"
- "12"
- "male"
- 獲取長度
192.168.1.103:6379> hlen Aang
(integer) 3
API
import java.util
import redis.clients.jedis.Jedis
object HashValue {
def main(args: Array[String]): Unit = {
val jedis: Jedis = new Jedis("192.168.1.103", 6379)
jedis.auth("root")
jedis.hset("sichuan", "chengdu", "1000")
jedis.hset("sichuan", "yaan", "1000")
jedis.hincrByFloat("sichuan", "yaan", 500.5)
val str: String = jedis.hget("sichuan", "yaan")
println(str)
val all: util.Map[String, String] = jedis.hgetAll("sichuan")
// jeids由java編寫, 取出的是java集合
import scala.collection.JavaConversions._
for(t <- all){
println(s"key: ${t._1} -> value: ${t._2}")
}
jedis.hdel("sichuan", "yaan")
jedis.del("sichuan")
}
}
List資料型別
命令列
- 插入資料
192.168.1.103:6379> lpush lst1 1 2 3
(integer) 3
192.168.1.103:6379> rpush lst1 a b c
(integer) 6
- 獲取資料
192.168.1.103:6379> lrange lst1 0 -1
- "3"
- "2"
- "1"
- "a"
- "b"
- "c"
192.168.1.103:6379> lrange lst1 1 3- "2"
- "1"
- "a"
- 彈出 (刪除) 資料
192.168.1.103:6379> lpop lst1
"3"
192.168.1.103:6379> rpop lst1
"c"
192.168.1.103:6379> lrange lst1 0 -1
- "2"
- "1"
- "a"
- "b"
- 獲取長度
192.168.1.103:6379> llen lst1
(integer) 4
API
import java.util
import redis.clients.jedis.Jedis
object ListValue {
def main(args: Array[String]): Unit = {
val jedis: Jedis = new Jedis("192.168.1.103", 6379)
jedis.auth("root")
jedis.lpush("lst1", "1", "2", "3")
jedis.rpush("lst1", "a", "b", "c")
jedis.lpop("lst1")
jedis.rpop("lst1")
val strings: util.List[String] = jedis.lrange("lst1", 0, -1)
import scala.collection.JavaConversions._
for(s <- strings){
println(s)
}
}
}
Set資料型別
命令列
- 建立
192.168.1.103:6379> sadd s1 1 3 3 5 a b c
(integer) 6
- 獲取
192.168.1.103:6379> smembers s1
- "a"
- "5"
- "3"
- "1"
- "c"
- "b"
- 刪除
192.168.1.103:6379> srem s1 3
(integer) 1
- 判斷是否存在
192.168.1.103:6379> sismember s1 3
(integer) 0
192.168.1.103:6379> sismember s1 5
(integer) 1
- 差集, 交集, 並集
192.168.1.103:6379> sadd s1 1 2 3
(integer) 3
192.168.1.103:6379> sadd s2 2 3 4
(integer) 3
192.168.1.103:6379> sdiff s1 s2
- "1"
192.168.1.103:6379> sinter s1 s2- "2"
- "3"
192.168.1.103:6379> sunion s1 s2- "1"
- "2"
- "3"
- "4"
API
import java.util
import redis.clients.jedis.Jedis
object SetValue {
def main(args: Array[String]): Unit = {
val jedis: Jedis = new Jedis("192.168.1.103", 6379)
jedis.auth("root")
jedis.sadd("s1", "1", "2", "3", "3", "5")
jedis.srem("s1", "2")
val boolean = jedis.sismember("s1", "2")
println(boolean)
val strings: util.Set[String] = jedis.smembers("s1")
import scala.collection.JavaConversions._
for(s <- strings){
println(s)
}
}
}
ZSet資料型別
命令列
- 建立獲取
192.168.1.103:6379> zadd z1 90 Aang 85 Katara 75 Sokka
(integer) 3
192.168.1.103:6379> zrange z1 0 -1
- "Sokka"
- "Katara"
- "Aang"
192.168.1.103:6379> zrange z1 0 -1 withscores- "Sokka"
- "75"
- "Katara"
- "85"
- "Aang"
- "90"
192.168.1.103:6379> zrevrange z1 0 -1- "Aang"
- "Katara"
- "Sokka"
- 增減score
192.168.1.103:6379> zincrby z1 10 Katara
"95"
192.168.1.103:6379> zrange z1 0 -1 withscores
- "Sokka"
- "75"
- "Aang"
- "90"
- "Katara"
- "95"
- 獲取元素排名
192.168.1.103:6379> zrank z1 Katara
(integer) 2192.168.1.103:6379> zrevrank z1 Katara
(integer) 0
API
import java.{lang, util}
import redis.clients.jedis.{Jedis, Tuple}
object ZSetValue {
def main(args: Array[String]): Unit = {
val jedis: Jedis = new Jedis("192.168.1.103", 6379)
jedis.auth("root")
jedis.zadd("z1", 90, "Aang")
jedis.zadd("z1", 85, "Katara")
jedis.zadd("z1", 75, "Sokka")
jedis.zincrby("z1", 10, "Katara")
val tuples: util.Set[Tuple] = jedis.zrangeWithScores("z1", 0, -1)
import scala.collection.JavaConversions._
for(t <- tuples){
println(t)
}
val long: lang.Long = jedis.zrank("z1", "Katara")
println(long)
}
}