/*
* 常用实例
* 字符操作:run1 -> run7
* Map操作:run8
* List操作:run9
* Set操作:run10
* 列表排序:run11
*/
public static void run1(){//存取
jedis.set("key" , "茶杯");
System.out.println(jedis.get("key"));
}
public static void run2(){//覆盖
jedis.set("key" , "茶杯");
jedis.set("key" , "玻璃杯");
System.out.println(jedis.get("key"));
}
public static void run3(){//删除
jedis.set("key" , "茶杯");
jedis.del("key");
System.out.println(jedis.get("key"));
}
public static void run4(){//拼加
jedis.set("key" , "茶杯");
jedis.append("key", "沏好茶");
System.out.println(jedis.get("key"));
}
public static void run5(){//加1操作
jedis.set("key" , "1");
//jredis.set("key" , "茶杯");//ERR value is not an integer or out of range
jedis.incr("key");
System.out.println(jedis.get("key"));
}
public static void run6(){//多值操作
jedis.mset("key1" , "茶杯", "key2", "沏好茶");
System.out.println(jedis.get("key1") + jedis.get("key2"));
}
public static void run7(){//是否存在key为"key"的记录
jedis.set("key" , "茶杯");
System.out.println(jedis.get("key"));
System.out.println(jedis.exists("key"));
}
/*
* Map操作的方法都是h开头:
* hmset:往redis中存map
* hmget:往redis中存map中的key对应的值
* hlen:map中元素的个数
* hkeys:map中key的集合
* hvals:map中value的集合
*/
public static void run8(){//存取map:hmset,hmget
Map map = new HashMap();
map.put("key1", "茶杯");
map.put("key2", "沏好茶");
map.put("key3", "润香");
jedis.hmset("map", map);
List rsmap = jedis.hmget("map", "key1", "key2", "key3", "key4");
System.out.println(rsmap);
jedis.hdel("map", "key2");//删除map中的某个键值 jredis.hdel("map", "key2", "key1");
System.out.println(jedis.hmget("map", "key1", "key2", "key3", "key4"));//因为删除了,所以返回的是null
System.out.println(jedis.hlen("map"));//map的键中存放的值的个数
System.out.println(jedis.hkeys("map"));//map对象中的所有key
System.out.println(jedis.hvals("map"));//map对象中的所有value
Object map1[] = jedis.hkeys("map").toArray();
for (Object key : map1) {
System.out.println(key + ":" + jedis.hmget("map", key.toString()));
}
}
/*
* List操作
* lpush:在key对应list的头部添加字符串元素
* lrange:返回存储在key列表的特定元素。偏移量开始和停止是从0开始的索引,0是第一元素(该列表的头部),1是列表的下一个元素。这些偏移量也可以是表示开始在列表的末尾偏移负数。例如,-1是该列表的最后一个元素,-2倒数第二个
* rpush:在key对应list的尾部添加字符串元素
* rpop:移除并返回列表 key 的尾元素
* lpop:移除并返回列表 key 的头元素
* 登陆:
* ./redis-cli -h IP地址 -a 密码 -p 6379
* 查看所有:
* keys *
* 查看List中有多少条数据:
* llen key -> 返回key对应list的长度
*/
public static void run9(){
jedis.del("list");
System.out.println(jedis.lrange("list", 0, -1));//从List中往外出取值
jedis.lpush("list", "茶杯");
jedis.lpush("list", "沏好茶");
jedis.lpush("list", "润香");
System.out.println(jedis.lrange("list", 0, -1));//从List中往外出取值
jedis.del("list");
jedis.rpush("list", "茶杯");
jedis.rpush("list", "沏好茶");
jedis.rpush("list", "润香");
System.out.println(jedis.lrange("list", 0, -1));//从List中往外出取值
System.out.println(jedis.rpop("list"));//移除并返回列表 key 的尾元素
System.out.println(jedis.lrange("list", 0, -1));//从List中往外出取值
}
1 . linsert -> 在key对应list的特定位置之前或之后添加字符串元素:
redis 127.0.0.1:6379> rpush mylist "one"
(integer) 1
redis 127.0.0.1:6379> rpush mylist "two"
(integer) 2
redis 127.0.0.1:6379> linsert mylist before "two" "there"
(integer) 3
redis 127.0.0.1:6379> lrange mylist 0 -1
1) "one"
2) "there"
3) "two"
redis 127.0.0.1:6379>
2 . lset -> 设置list中指定下标的元素值(下标从0开始):
redis 127.0.0.1:6379> del mylist
(integer) 1
redis 127.0.0.1:6379> rpush mylist "one"
(integer) 1
redis 127.0.0.1:6379> rpush mylist "two"
(integer) 2
redis 127.0.0.1:6379> rpush mylist "three"
(integer) 3
redis 127.0.0.1:6379> lset mylist 0 "1"
OK
redis 127.0.0.1:6379> lset mylist -2 "2"
OK
redis 127.0.0.1:6379> lrange mylist 0 -1
1) "1"
2) "2"
3) "three"
redis 127.0.0.1:6379>
3 . lrem -> 从key对应list中删除count个和value相同的元素:
① count>0时,按从头到尾的顺序删除
redis 127.0.0.1:6379> del mylist
(integer) 1
redis 127.0.0.1:6379> rpush mylist "one"
(integer) 1
redis 127.0.0.1:6379> rpush mylist "two"
(integer) 2
redis 127.0.0.1:6379> rpush mylist "three"
(integer) 3
redis 127.0.0.1:6379> rpush mylist "two"
(integer) 4
redis 127.0.0.1:6379> rpush mylist "two"
(integer) 5
redis 127.0.0.1:6379> lrem mylist 2 "two"
(integer) 2
redis 127.0.0.1:6379> lrange mylist 0 -1
1) "one"
2) "three"
3) "two"
redis 127.0.0.1:6379>
② count<0时,按从尾到头的顺序删除
redis 127.0.0.1:6379> del mylist
(integer) 1
redis 127.0.0.1:6379> rpush mylist "one"
(integer) 1
redis 127.0.0.1:6379> rpush mylist "two"
(integer) 2
redis 127.0.0.1:6379> rpush mylist "three"
(integer) 3
redis 127.0.0.1:6379> rpush mylist "two"
(integer) 4
redis 127.0.0.1:6379> rpush mylist "two"
(integer) 5
redis 127.0.0.1:6379> lrem mylist -2 "two"
(integer) 2
redis 127.0.0.1:6379> lrange mylist 0 -1
1) "one"
2) "two"
3) "three"
redis 127.0.0.1:6379>
③ count=0时,删除全部
redis 127.0.0.1:6379> del mylist
(integer) 1
redis 127.0.0.1:6379> rpush mylist "one"
(integer) 1
redis 127.0.0.1:6379> rpush mylist "two"
(integer) 2
redis 127.0.0.1:6379> rpush mylist "three"
(integer) 3
redis 127.0.0.1:6379> rpush mylist "two"
(integer) 4
redis 127.0.0.1:6379> rpush mylist "two"
(integer) 5
redis 127.0.0.1:6379> lrem mylist 0 "two"
(integer) 3
redis 127.0.0.1:6379> lrange mylist 0 -1
1) "one"
2) "three"
redis 127.0.0.1:6379>
4 . ltrim -> 保留指定key 的值范围内的数据:
redis 127.0.0.1:6379> del mylist
(integer) 1
redis 127.0.0.1:6379> rpush mylist "0"
(integer) 1
redis 127.0.0.1:6379> rpush mylist "1"
(integer) 2
redis 127.0.0.1:6379> rpush mylist "2"
(integer) 3
redis 127.0.0.1:6379> rpush mylist "3"
(integer) 4
redis 127.0.0.1:6379> rpush mylist "4"
(integer) 5
redis 127.0.0.1:6379> rpush mylist "5"
(integer) 6
redis 127.0.0.1:6379> ltrim mylist 2 -1
OK
redis 127.0.0.1:6379> lrange mylist 0 -1
1) "2"
2) "3"
3) "4"
4) "5"
redis 127.0.0.1:6379>
5. rpoplpush -> 从第一个list的尾部移除元素并添加到第二个list的头部,最后返回被移除的元素值,整个操作是原子的.如果第一个list是空或者不存在返回nil:
redis 127.0.0.1:6379> del mylist
(integer) 1
redis 127.0.0.1:6379> rpush mylist "0"
(integer) 1
redis 127.0.0.1:6379> rpush mylist "1"
(integer) 2
redis 127.0.0.1:6379> rpush mylist "2"
(integer) 3
redis 127.0.0.1:6379> lrange mylist 0 -1
1) "0"
2) "1"
3) "2"
redis 127.0.0.1:6379> rpoplpush mylist mylist01
"2"
redis 127.0.0.1:6379> lrange mylist 0 -1
1) "0"
2) "1"
redis 127.0.0.1:6379> lrange mylist01 0 -1
1) "2"
redis 127.0.0.1:6379>
6. lindex -> 返回名称为key的list中index位置的元素:
redis 127.0.0.1:6379> lrange mylist 0 -1
1) "0"
2) "1"
redis 127.0.0.1:6379> lindex mylist 0
"0"
redis 127.0.0.1:6379> lindex mylist 1
"1"
redis 127.0.0.1:6379> lindex mylist 2
(nil)
redis 127.0.0.1:6379>
/*
*set操作
* sadd往集合里加值
* srem从集合众删值
* smembers获取所有的值
* sismember判断是否为集的元素
* srandmember随机取值
* scard集合元素的个数
*/
public static void run10(){
jedis.del("set");
jedis.sadd("set", "茶杯");
jedis.sadd("set", "沏好茶");
jedis.sadd("set", "润香");
jedis.sadd("set", "删除");
jedis.srem("set", "删除");
System.out.println(jedis.smembers("set"));
System.out.println(jedis.sismember("set", "删除"));
System.out.println(jedis.srandmember("set"));
System.out.println(jedis.scard("set"));
}
/*
* 列表排序
*/
public static void run11(){
jedis.del("list");
jedis.rpush("list", "茶杯");
jedis.lpush("list", "沏好茶");
jedis.lpush("list", "润香");
System.out.println("取出所有值:" + jedis.lrange("list", 0, -1));//从List中往外出取值
// 排序
/*
* list中存字符串时必须指定参数为alpha,如果不使用SortingParams,而是直接使用sort("list"),
* 会出现"ERR One or more scores can't be converted into double"
*/
SortingParams sortingParameters = new SortingParams();
sortingParameters.alpha();
System.out.println("jredis.sort(\"list\",sortingParameters)=" + jedis.sort("list",sortingParameters));
// 修改列表中单个值
jedis.lset("list", 0, "hello list!");
// 获取列表指定下标的值
System.out.println("jredis.lindex(\"list\", 0)=" + jedis.lindex("list", 0));
// 删除列表指定下标的值
//System.out.println(jredis.lrem("list", 0, "hello list"));
System.out.println(jedis.lrem("list", 0, "hello list!"));
System.out.println("取出所有值:" + jedis.lrange("list", 0, -1));//从List中往外出取值
// 删除区间以外的数据
System.out.println("jredis.ltrim(\"list\", 0, 0)=" + jedis.ltrim("list", 0, 0));
System.out.println("取出所有值:" + jedis.lrange("list", 0, -1));//从List中往外出取值
}
希望对你有帮助,祝你有一个好心情,加油!
若有错误、不全、可优化的点,欢迎纠正与补充!