Redis常用API操作有哪些


redis常用api操作有哪些

//设置键值对
//set key value [EX seconds] [PX milliseconds] [NX|XX]
//获取值
//get key
//删除键
//del key
//清空数据库
//flushdb
//获取list值
//lrange key start stop
//map类型
//hget key field
//hgetall key
//sortedset类型
package hgs.redislearn;

import java.io.IOException;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Set;

import org.codehaus.jackson.JsonGenerationException;

import org.codehaus.jackson.map.JsonMappingException;

import org.codehaus.jackson.map.ObjectMapper;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import redis.clients.jedis.BinaryClient.LIST_POSITION;

import redis.clients.jedis.Jedis;

/**
*
* <
p>
Description:Redis <
/p>

* @author guangshihao
* @date 2018年9月28日
*
*/
public class RedisMainTest {
Jedis jedis = null;

@Before
public void getConnect() {
jedis = new Jedis("
192.168.6.129"
, 6379);

}
@Test
public void sysoutConnect() {
System.out.println(jedis.ping());

}

@After
public void finaly() {
jedis.close();

}

//测试放入String类型的键值对,并进行获取删除修改等操作
@Test
public void TestStringKeys() {
jedis.set("
hgs.name"
, "
haoguangshi."
);

String name = jedis.get("
hgs.name"
);

System.out.println(name);

long affect = jedis.del("
hgs.name"
);

System.out.println(affect);

}

@Test
public void TestListKeys() {
//从左边插入
jedis.lpush("
test_list"
, "
1"
,"
2"
,"
100"
);

//从右边插入
jedis.rpush("
test_list"
, "
100"
,"
2"
,"
1"
,"
this"
);

//获取list
List<
String >
lis = jedis.lrange("
test_list"
, 0, -1);

System.out.println(lis);

//jedis.lpop lpush rpop rpush
//在this出现的第一个位置的前面插入is
jedis.linsert("
test_list"
, LIST_POSITION.BEFORE, "
this"
, "
is"
);


}

//map类型操作
@Test
public void TestMapKeys() throws JsonGenerationException, JsonMappingException, IOException {
/* Person p = new Person ();

p.setName("
hgs"
);

p.setAge(26);

p.setWeight(65);



Person p1 = new Person ();

p1.setName("
wd"
);

p1.setAge(23);

p1.setWeight(60);


ObjectMapper mapper = new ObjectMapper();

String sp = mapper.writer().writeValueAsString(p);

String sp1 = mapper.writer().writeValueAsString(p1);


Map<
String,String>
ps = new HashMap<
String, String>
();

ps.put("
hgs"
, sp);

ps.put("
wd"
, sp1);
*/
jedis.hset("
pseron:hgs"
, "
name"
, "
hgs"
);

jedis.hset("
pseron:hgs"
, "
age"
, "
24"
);

jedis.hset("
pseron:hgs"
, "
weigth"
, "
65kg"
);


jedis.hset("
pseron:wd"
, "
name"
, "
wd"
);

jedis.hset("
pseron:wd"
, "
age"
, "
24"
);

jedis.hset("
pseron:wd"
, "
weight"
, "
60kg"
);


}
@Test
public void loopMapKeys() {
//遍历一个map
Map<
String,String>
wd = jedis.hgetAll("
pseron:wd"
);

for(String type : wd.keySet()) {
System.out.println(type+"
: "
+wd.get(type));

}
//原子操作自增2
jedis.hincrBy("
pseron:hgs\"
"
, "
age"
, 2L);

}

@Test
public void delMapKeys() {
//删除属性
jedis.hdel("
pseron:wd"
, "
weight"
);

//打印集合的长度
System.out.println(jedis.hlen("
pseron:hgs"
));

}

//set类型数据结构,添加数据
@Test
public void setKeysTest() {
String[] tmp = new String[] {
"
tianmao"
,"
dingding"
,"
alimama"
,"
zhifubao"
,"
feizhu"

};

jedis.sadd("
apps"
, tmp);


String[] tmp1 = new String[] {
"
tianmao"
,"
dingdi"
,"
alima"
,"
zhifubao"
,"
feizhu"

};

jedis.sadd("
apps"
, tmp);

jedis.sadd("
apps1"
, tmp1);

}
//遍历
@Test
public void scanSetKeys() {
//判断某个记录是否存在
System.out.println(jedis.sismember("
apps"
, "
tianmao"
));

System.out.println(jedis.scard("
apps"
));

//计算交集
Set<
String>
sets = jedis.sinter("
apps"
,"
apps1"
);

for(String s : sets) {
System.out.println(s);

}

//并集
Set<
String>
sets1 = jedis.sunion("
apps"
,"
apps1"
);

for(String s : sets1) {
System.out.println(s);

}
System.out.println();

//差集
Set<
String>
sets2 = jedis.sdiff("
apps"
,"
apps1"
);

for(String s : sets2) {
System.out.println(s);

}
//遍历
Set<
String>
sets3 = jedis.smembers("
apps"
);

for(String s : sets3) {
System.out.println(s);

}
}
//SortedSet数据类型
//存数据
@Test
public void sortedSetAddTest() {
Map<
String,Double>
scoremem = new HashMap<
String, Double>
();

scoremem.put("
wd"
, (double) 88);

scoremem.put("
cm"
, (double) 87);

scoremem.put("
zz"
, (double) 90);

scoremem.put("
wzf"
, (double) 70);

scoremem.put("
xzh"
, (double) 66);

scoremem.put("
hgs"
, (double) 55);

scoremem.put("
hjh"
, (double) 88);

scoremem.put("
shk"
, (double) 100);

jedis.zadd("
roommeets_score"
, scoremem);

}
//sortedSet 遍历
@Test
public void scanSortedSet() {
//正序遍历
Set<
String>
members = jedis.zrange("
roommeets_score"
, 0, -1);

for(String mem : members) {
System.out.println("
name:"
+mem +"
,"
+"
score:"
+jedis.zscore("
roommeets_score"
, mem)+"
,"
+"
rank:"
+(jedis.zrank("
roommeets_score"
, mem)+1));

}
System.out.println();

//正序遍历
Set<
String>
members1 = jedis.zrevrange("
roommeets_score"
, 0, -1);

for(String mem : members1) {
System.out.println("
name:"
+mem +"
,"
+"
score:"
+jedis.zscore("
roommeets_score"
, mem)+"
,"
+"
rank:"
+(jedis.zrevrank("
roommeets_score"
, mem)+1L));

}
}
}
class Person{
String name ;

int age;

double weight;

public String getName() {
return name;

}
public void setName(String name) {
this.name = name;

}
public int getAge() {
return age;

}
public void setAge(int age) {
this.age = age;

}
public double getWeight() {
return weight;

}
public void setWeight(double weight) {
this.weight = weight;

}
@Override
public String toString() {
return "
Person [name="
+ name + "
, age="
+ age + "
, weight="
+ weight + "
]"
;

}



}

Redis是一个高性能的键值对存储系统,广泛应用于各种互联网应用,如缓存、消息队列、排行榜等。本文将介绍Redis常用API操作,帮助读者更好地使用Redis。
一、字符串类型(String)
字符串类型是Redis的最基本数据类型,可用于存储字符串、数字或二进制数据。以下为常用API操作:
1. SET key value:设置指定 key 的值为 value
2. GET key:获取指定 key 的值
3. DEL key:删除指定 key
4. INCR key:将指定 key 的值增加1
5. APPEND key value:将字符串 value 追加到指定 key 的末尾
二、哈希类型(Hash)
哈希类型是Redis的一种数据结构,可用于存储对象。以下为常用API操作:
1. HSET key field value:设置指定 key 下的 field 为 value
2. HGET key field:获取指定 key 下的 field 的值
3. HDEL key field1 field2 ...:删除指定 key 下的一个或多个 field
4. HGETALL key:获取指定 key 下所有 field 和 value
三、列表类型(List)
列表类型是Redis的一种数据结构,可用于存储有序的字符串列表。以下为常用API操作:
1. LPUSH key value1 value2 ...:在指定 key 的头部插入一个或多个值
2. RPUSH key value1 value2 ...:在指定 key 的尾部插入一个或多个值
3. LPOP key:删除并返回指定 key 的头部值
4. RPOP key:删除并返回指定 key 的尾部值
5. LLEN key:获取指定 key 的列表长度
本文介绍了Redis的常用API操作,涵盖了字符串类型、哈希类型和列表类型。对于读者来说,了解这些操作可以更好地使用Redis,在实际应用中提升性能。同时,本文还提供了一些常用操作的代码示例,让读者更好地理解和掌握Redis的使用方法。